I/O Stream, File, Multithreading - Part 2
Synchronization, sleep/join/yield, wait/notify, thread priorities, and daemon threads.
Threads sharing data must synchronize; a synchronized method or block locks an object's monitor so only one thread enters at a time. sleep() pauses a thread, yield() lets others run, join() waits for a thread to finish, and wait()/notify()/notifyAll() coordinate waiting threads. Priority ranges 1-10 and hints the scheduler; equal-priority threads run round-robin. Daemon threads run in the background; the JVM exits when all user threads finish.
Example
public synchronized void increment() {
count++;
}
No comments yet.