代码来自java 编程思想第4版 21章
- 代码1
//: concurrency/NotifyVsNotifyAll.java
package concurrency; /* Added by Eclipse.py */
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();
public void run() {
blocker.waitingCall();
}
}
class Task2 implements Runnable {
// A separate Blocker object:
static Blocker blocker = new Blocker();
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;
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
}
} /*
* Output: (Sample) 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
*/// :~
-
代码2
//: concurrency/Restaurant.java package concurrency; /* Added by Eclipse.py */ // The producer-consumer approach to task cooperation. import java.util.concurrent.*; import static net.mindview.util.Print.*; class Meal { private final int orderNum; public Meal(int orderNum) { this.orderNum = orderNum; } public String toString() { return "Meal " + orderNum; } } class WaitPerson implements Runnable { private Restaurant restaurant; public WaitPerson(Restaurant r) { restaurant = r; } public void run() { try { while (!Thread.interrupted()) { synchronized (this) { while (restaurant.meal == null) wait(); // ... for the chef to produce a meal } print("Waitperson got " + restaurant.meal); synchronized (restaurant.chef) { restaurant.meal = null; restaurant.chef.notifyAll(); // Ready for another //这里的notifyAll();/////////////////////// } } } catch (InterruptedException e) { print("WaitPerson interrupted"); } } } class Chef implements Runnable { private Restaurant restaurant; private int count = 0; public Chef(Restaurant r) { restaurant = r; } public void run() { try { while (!Thread.interrupted()) { synchronized (this) { while (restaurant.meal != null) wait(); // ... for the meal to be taken } if (++count == 10) { print("Out of food, closing"); restaurant.exec.shutdownNow(); } printnb("Order up! "); synchronized (restaurant.waitPerson) { restaurant.meal = new Meal(count); restaurant.waitPerson.notifyAll();-////////////// } TimeUnit.MILLISECONDS.sleep(100); } } catch (InterruptedException e) { print("Chef interrupted"); } } } public class Restaurant { Meal meal; ExecutorService exec = Executors.newCachedThreadPool(); WaitPerson waitPerson = new WaitPerson(this); Chef chef = new Chef(this); public Restaurant() { exec.execute(chef); exec.execute(waitPerson); } public static void main(String[] args) { new Restaurant(); } } /* * Output: Order up! Waitperson got Meal 1 Order up! Waitperson got Meal 2 Order * up! Waitperson got Meal 3 Order up! Waitperson got Meal 4 Order up! * Waitperson got Meal 5 Order up! Waitperson got Meal 6 Order up! Waitperson * got Meal 7 Order up! Waitperson got Meal 8 Order up! Waitperson * got Meal 9 * Out of food, closing WaitPerson interrupted Order up! Chef interrupted */// :~
-
问题
- notifyAll是Object的方法 ---public final native void notifyAll();但不是静态方法
这个在代码1中,为什么没有实例对象就可以直接调用该方法
而在代码2中为什么需要 restaurant.waitPerson.notifyAll() ;如果直接改成notifyAll(); 会报错
Order up! Exception in thread "pool-1-thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at concurrency.Chef.run(Restaurant.java:69)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
- 这2种调用的方法有什么区别