notify() 和notifyAll()区别
来源:互联网 发布:淘宝女鞋店铺logo 编辑:程序博客网 时间:2024/05/21 08:55
引用网上的例子,在明知到生产者 一个生产者,几个抢食者,如果通知所有的抢食者执行的操作会发生异常的时候,使用notify
import java.util.ArrayList;import java.util.List;class Widget{}class WidgetMaker extends Thread{ List finishedWidgets = new ArrayList(); public void run(){ try{ while(true){ Thread.sleep(5000); Widget w = new Widget(); Widget i = new Widget(); Widget d = new Widget(); synchronized (finishedWidgets) { finishedWidgets.add(w); finishedWidgets.add(i); finishedWidgets.add(d); finishedWidgets.notifyAll(); } } }catch(InterruptedException e) {} } public Widget waitForWiget() { synchronized (finishedWidgets) { if (finishedWidgets.size() == 0) { try { finishedWidgets.wait(); } catch (InterruptedException e) { } } return (Widget) finishedWidgets.remove(0); } }}public class WidgetUser extends Thread{ private WidgetMaker maker; public WidgetUser(String name,WidgetMaker maker){ super(name); this.maker = maker; } public void run(){ Widget w = maker.waitForWiget(); System.out.println(getName()+"got a widget"); } public static void main(String[] args){ WidgetMaker maker = new WidgetMaker(); maker.start(); new WidgetUser("Lenny", maker).start(); new WidgetUser("Moe", maker).start(); new WidgetUser("Curly", maker).start(); }}
- notify() 和notifyAll()区别
- notify和notifyAll区别
- notify()和notifyAll()区别
- notify()和notifyAll()主要区别
- notify() 和 notifyAll() 的区别
- notify()和notifyAll()主要区别
- notify和notifyAll的区别
- notify()和notifyAll()主要区别
- notify()和notifyAll()的区别
- notify()和notifyAll()的区别
- notify()和notifyAll()主要区别
- notify()和notifyAll()主要区别
- wait, notify 和 notifyAll区别
- notify和notifyAll的区别和相同
- notify()和notifyAll()的区别---小笔记
- java中notify()和notifyall的区别
- notify()和notifyAll() 本质区别 --- 示例 (转)
- java notify和notifyAll的区别
- 键盘代码 KEYCODE
- GetBuffer ReleaseBuffer
- linux下svn使用指南
- C语言中多维数组的理解
- 使用7zip的SDK
- notify() 和notifyAll()区别
- AES加密算法
- 硬件解码
- Android的MediaPlayer架构介绍 + 补充
- 静心
- 扬尼斯定律:程序员的开发效率真相(每6年提高一倍)
- 关于后验和似然
- galcore.ko
- 从CPU看嵌入式系统发展方向