Java Reference
In-Depth Information
10 synchronized(mutex) {
11 mutex.notifyAll();
12 // mutex.notify();
13 }
14 }
15
16 public void run() {
17 try {
18 synchronized (mutex) {
19 System.out.println(getName() + " waiting");
20 mutex.wait();
21 System.out.println(getName() + " woken up");
22 mutex.wait(2000);
23 System.out.println(getName() + " waking up another thread");
24 mutex.notify();
25 }
26 } catch (InterruptedException ie) {
27 ie.printStackTrace();
28 }
29 }
30 }
Figure 4-4 shows the results of running NotifyVersusNotifyAll , first with line 12 and line
11 commented out (so that notify is called), then with line 11 and line 12 commented out (so
that notifyAll is called).
Figure 4-4. Notify returns one thread to the runnable state whereas notifyAll returns all threads to
Search WWH ::




Custom Search