2015-09-07 11:44:36 -06:00
|
|
|
// concurrency/NotifyVsNotifyAll.java
|
2015-12-15 11:47:04 -08:00
|
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.concurrent.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
class Blocker {
|
|
|
|
synchronized void waitingCall() {
|
|
|
|
try {
|
|
|
|
while(!Thread.interrupted()) {
|
|
|
|
wait();
|
|
|
|
System.out.print(Thread.currentThread() + " ");
|
|
|
|
}
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
// OK to exit this way
|
|
|
|
}
|
|
|
|
}
|
|
|
|
synchronized void prod() { notify(); }
|
|
|
|
synchronized void prodAll() { notifyAll(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
class Task implements Runnable {
|
|
|
|
static Blocker blocker = new Blocker();
|
|
|
|
@Override
|
|
|
|
public void run() { blocker.waitingCall(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
class Task2 implements Runnable {
|
|
|
|
// A separate Blocker object:
|
|
|
|
static Blocker blocker = new Blocker();
|
|
|
|
@Override
|
|
|
|
public void run() { blocker.waitingCall(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class NotifyVsNotifyAll {
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
ExecutorService exec = Executors.newCachedThreadPool();
|
|
|
|
for(int i = 0; i < 5; i++)
|
|
|
|
exec.execute(new Task());
|
|
|
|
exec.execute(new Task2());
|
|
|
|
Timer timer = new Timer();
|
|
|
|
timer.scheduleAtFixedRate(new TimerTask() {
|
|
|
|
boolean prod = true;
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if(prod) {
|
|
|
|
System.out.print("\nnotify() ");
|
|
|
|
Task.blocker.prod();
|
|
|
|
prod = false;
|
|
|
|
} else {
|
|
|
|
System.out.print("\nnotifyAll() ");
|
|
|
|
Task.blocker.prodAll();
|
|
|
|
prod = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 400, 400); // Run every .4 second
|
|
|
|
TimeUnit.SECONDS.sleep(5); // Run for a while...
|
|
|
|
timer.cancel();
|
|
|
|
System.out.println("\nTimer canceled");
|
|
|
|
TimeUnit.MILLISECONDS.sleep(500);
|
|
|
|
System.out.print("Task2.blocker.prodAll() ");
|
|
|
|
Task2.blocker.prodAll();
|
|
|
|
TimeUnit.MILLISECONDS.sleep(500);
|
|
|
|
System.out.println("\nShutting down");
|
|
|
|
exec.shutdownNow(); // Interrupt all tasks
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
notify() Thread[pool-1-thread-1,5,main]
|
|
|
|
notifyAll() Thread[pool-1-thread-1,5,main]
|
|
|
|
Thread[pool-1-thread-5,5,main]
|
|
|
|
Thread[pool-1-thread-4,5,main]
|
|
|
|
Thread[pool-1-thread-3,5,main]
|
|
|
|
Thread[pool-1-thread-2,5,main]
|
|
|
|
notify() Thread[pool-1-thread-1,5,main]
|
|
|
|
notifyAll() Thread[pool-1-thread-1,5,main]
|
|
|
|
Thread[pool-1-thread-2,5,main]
|
|
|
|
Thread[pool-1-thread-3,5,main]
|
|
|
|
Thread[pool-1-thread-4,5,main]
|
|
|
|
Thread[pool-1-thread-5,5,main]
|
|
|
|
notify() Thread[pool-1-thread-1,5,main]
|
|
|
|
notifyAll() Thread[pool-1-thread-1,5,main]
|
|
|
|
Thread[pool-1-thread-5,5,main]
|
|
|
|
Thread[pool-1-thread-4,5,main]
|
|
|
|
Thread[pool-1-thread-3,5,main]
|
|
|
|
Thread[pool-1-thread-2,5,main]
|
|
|
|
notify() Thread[pool-1-thread-1,5,main]
|
|
|
|
notifyAll() Thread[pool-1-thread-1,5,main]
|
|
|
|
Thread[pool-1-thread-2,5,main]
|
|
|
|
Thread[pool-1-thread-3,5,main]
|
|
|
|
Thread[pool-1-thread-4,5,main]
|
|
|
|
Thread[pool-1-thread-5,5,main]
|
|
|
|
notify() Thread[pool-1-thread-1,5,main]
|
|
|
|
notifyAll() Thread[pool-1-thread-1,5,main]
|
|
|
|
Thread[pool-1-thread-5,5,main]
|
|
|
|
Thread[pool-1-thread-4,5,main]
|
|
|
|
Thread[pool-1-thread-3,5,main]
|
|
|
|
Thread[pool-1-thread-2,5,main]
|
|
|
|
notify() Thread[pool-1-thread-1,5,main]
|
|
|
|
notifyAll() Thread[pool-1-thread-1,5,main]
|
|
|
|
Thread[pool-1-thread-2,5,main]
|
|
|
|
Thread[pool-1-thread-3,5,main]
|
|
|
|
Thread[pool-1-thread-4,5,main]
|
|
|
|
Thread[pool-1-thread-5,5,main]
|
|
|
|
Timer canceled
|
|
|
|
Task2.blocker.prodAll() Thread[pool-1-thread-6,5,main]
|
|
|
|
Shutting down
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|