生活中,我们常遇到需要等待的场景,例如去银行办事,在没轮到自己之前需要一直等待,但是如果需要自己每隔几秒钟就去柜台前看看状况,那肯定是种非常低效和令人恼火的体验。而实际的情况是,接待员会让您拿个号,说"请稍等一会"(wait); 当排到时,语言和大屏幕会提示"请XXX号到N号柜台办理"(notify)。
wait/notify机制也正是处理这样的场景:线程继续执行需要等待某个条件的变化,这个条件由另一个任务改变,如果一直空循环检查条件变化,是一种不良的CPU使用方式,这时候可以调用wait()将任务挂起,在其他线程调用了notify()或notifyAll()时,任务被唤醒并检查条件的变化。
这个过程中,锁的持有发生了变化。介绍wait/notify最常用的例子是生产者和消费者,设想你去饭馆吃饭,叫来服务员说,把我的宫保鸡丁端上来吧。这时候你获得了服务员的锁,在解决你的事情前,服务员不能去做别的事。(同一时间,厨师可能已经做好了宫保鸡丁,等服务员来端,但是服务员在和你说话,厨师束手无策(等待锁)。)服务员没有宫保鸡丁,只能对你说:您稍等一下,我去厨房催催。服务员调用了wait()方法,你只好释放锁,服务员回到厨房,厨师怒气冲冲的喊(获得锁),宫保鸡丁好了,端走。
下面用程序演示这一场景:
- public class Waiter {
- private String dishes = null;
- public synchronized String getDishes() {
- System.out.printf("顾客获得服务员锁%n");
- while(this.dishes == null) {
- try {
- System.out.printf("顾客取菜,没有菜...顾客线程等待(释放锁)%n");
- wait();
- } catch(InterruptedException ex) {
- ex.printStackTrace();
- }
- }
- String d = this.dishes;
- System.out.printf("顾客取走: %s%n", this.dishes);
- this.dishes = null;
- notifyAll();
- System.out.printf("服务员通知正在等待的线程%n");
- return d;
- }
- public synchronized void setDishes(String dishes) {
- System.out.printf("厨师获得服务员锁%n");
- while(this.dishes != null) {
- try {
- System.out.printf("厨师交菜,服务员已经端了另一份菜...厨师线程等待(释放锁)%n");
- wait();
- } catch(InterruptedException ex) {
- ex.printStackTrace();
- }
- }
- this.dishes = dishes;
- System.out.printf("厨师交菜: %s%n", this.dishes);
- notifyAll();
- System.out.printf("服务员通知正在等待的线程(顾客)%n");
- }
- public static void main(String[] args) throws InterruptedException {
- Waiter busy = new Waiter();
- for(int i = 0; i < 10; i++) {
- Thread consumer = new Thread() {
- public void run() {
- busy.getDishes();
- }
- };
- consumer.start();
- }
- Thread.sleep(100);
- for(int i = 0; i < 10; i++) {
- Thread chef = new Thread() {
- public void run() {
- String dishes = "宫保鸡丁";
- busy.setDishes(dishes);
- }
- };
- chef.start();
- }
- }
- }
运行结果:
- 顾客获得服务员锁
- 顾客取菜,没有菜...顾客线程等待(释放锁)
- 厨师获得服务员锁
- 厨师交菜: 宫保鸡丁
- 服务员通知正在等待的线程(顾客)
- 顾客取走: 宫保鸡丁
- 服务员通知正在等待的线程(厨师)
下面来说明notifyAll的作用。
修改下代码,把厨师和顾客都增加到10个
- public static void main(String[] args) {
- Waiter busy = new Waiter();
- for(int i = 0; i < 10; i++) {
- Thread consumer = new Thread() {
- public void run() {
- busy.getDishes();
- }
- };
- consumer.start();
- }
- for(int i = 0; i < 10; i++) {
- Thread chef = new Thread() {
- public void run() {
- String dishes = "宫保鸡丁";
- busy.setDishes(dishes);
- }
- };
- chef.start();
- }
- }
执行后会发现程序会陷入永久的等待无法结束,这是因为notify()方法只唤醒众多等待的线程中的一个,拿到菜后本应唤醒顾客取走,但是有可能随机唤醒了另一个等待的厨师,没有顾客能取走服务员手中的菜,这时候程序就无法继续下去了。
解决的方法有两种:
1 把notify()改成notifyAll(),唤醒所有等待的线程
2 使用Java.util.concurrent库中的Condition,把等待的线程分为厨师和顾客两个集合,代码如下:
- public class ConditionWaiter {
- private String dishes = null;
- private Lock lock = new ReentrantLock();
- private Condition conConsumer = lock.newCondition();
- private Condition conChef = lock.newCondition();
- public String getDishes() {
- try {
- lock.lock();
- System.out.printf("顾客获得服务员锁%n");
- while(this.dishes == null) {
- try {
- System.out.printf("顾客取菜,没有菜...顾客线程等待%n");
- conConsumer.await();
- } catch(InterruptedException ex) {
- ex.printStackTrace();
- }
- }
- String d = this.dishes;
- System.out.printf("顾客取走:%s%n", this.dishes);
- this.dishes = null;
- conChef.signal();
- System.out.printf("服务员通知正在等待的线程(厨师)%n");
- return d;
- } finally {
- lock.unlock();
- }
- }
- public void setDishes(String dishes) {
- try {
- lock.lock();
- System.out.printf("厨师获得服务员锁%n");
- while(this.dishes != null) {
- try {
- System.out.printf("厨师交菜,服务员已经端了另一份菜...厨师线程等待%n");
- conChef.await();
- } catch(InterruptedException ex) {
- ex.printStackTrace();
- }
- }
- this.dishes = dishes;
- System.out.printf("厨师交菜:%s%n", this.dishes);
- conConsumer.signal();
- System.out.printf("服务员通知正在等待的线程(顾客)%n");
- } finally {
- lock.unlock();
- }
- }
- public static void main(String[] args) throws InterruptedException {
- ConditionWaiter busy = new ConditionWaiter();
- for(int i = 0; i < 10; i++) {
- Thread consumer = new Thread() {
- public void run() {
- busy.getDishes();
- }
- };
- consumer.start();
- }
- Thread.sleep(100);
- for(int i = 0; i < 10; i++) {
- Thread chef = new Thread() {
- public void run() {
- String dishes = "宫保鸡丁";
- busy.setDishes(dishes);
- }
- };
- chef.start();
- }
- }
- }
事实上,wait/notify机制编程模型复杂也运行低效,通常我们应该采取更高级的类库实现类似场景。以下代码是使用BlockingQueue实现线程协作的示例:
- public class BlockingQueueWaiter {
- static BlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
- public static void main(String[] args) throws InterruptedException {
- for(int i = 0; i < 10; i++) {
- Thread consumer = new Thread() {
- public void run() {
- String dishes;
- try {
- System.out.printf("顾客尝试取菜%n");
- dishes = queue.take();
- System.out.printf("顾客取走:%s%n", dishes);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- };
- consumer.start();
- }
- Thread.sleep(100);
- for(int i = 0; i < 10; i++) {
- Thread chef = new Thread() {
- public void run() {
- String dishes = "宫保鸡丁";
- try {
- System.out.printf("厨师尝试交菜%n");
- queue.put(dishes);
- System.out.printf("厨师交菜:%s%n", dishes);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- };
- chef.start();
- }
- }
- }