admin 管理员组文章数量: 887021
生产者
线程之间,通过wait() notify() notifyAll()来协作编程;
1. 必须在synchronized方法或方法块中使用
2. 与Thread.sleep()的区别:wait()方法释放持有的对象的锁。
3. 属于Object类的方法
4. 替代类:java.util.concurrent.locks.Condition 接口
5. 经典举例,生产者/消费者:餐馆有一个厨师负责生产食物,一个服务员负责上菜(消费食物);服务员必须在厨师生产完食物后工作,未生产完成时等待;系统必须有序的停止。
a)食物类:
class Meal {private final int orderNum;public Meal(int orderNum) {this.orderNum = orderNum;}@Overridepublic String toString() {return "Meal : " + orderNum;}}
b)厨师:
class Chef implements Runnable {private Restaurant res;private int count = 0;public Chef(Restaurant res) {this.res = res;}@Overridepublic void run() {try {while(!Thread.interrupted()) {synchronized (this) {if(null != res.getMeal()) {wait();}}if(++count > 10) {System.out.println("Out of food, close! ");res.getExec().shutdownNow(); //通知wait收工throw(new InterruptedException()); //自己收工}System.out.print("Order up! ");synchronized (res.getWaitPerson()) {res.setMeal(new Meal(count));res.getWaitPerson().notifyAll();}TimeUnit.MICROSECONDS.sleep(100);}} catch(InterruptedException e) {System.out.println("Chef interrupted! ");}}}
c)服务员:
class WaitPerson implements Runnable {private Restaurant res;public WaitPerson(Restaurant res) {this.res = res;}@Overridepublic void run() {try {while(!Thread.interrupted()) {synchronized (this) {if(null == res.getMeal()) {wait();}}System.out.println("WaitPerson got " + res.getMeal());synchronized (res.getChef()) {res.setMeal(null); //上酸菜res.getChef().notifyAll(); //通知厨师准备下一份}}} catch(InterruptedException e) {System.out.println("WaitPerson interrupted! ");}}}
d)餐馆:
public class Restaurant {private Meal meal;private Chef chef = new Chef(this);private WaitPerson waitPerson = new WaitPerson(this);private ExecutorService exec = Executors.newCachedThreadPool();public Restaurant() {exec.execute(chef);exec.execute(waitPerson);}public Meal getMeal() {return meal;}public void setMeal(Meal meal) {this.meal = meal;}public Chef getChef() {return chef;}public WaitPerson getWaitPerson() {return waitPerson;}public ExecutorService getExec() {return exec;}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
Order up! WaitPerson got Meal : 10
Out of food, close!
Chef interrupted!
WaitPerson interrupted!
本文标签: 生产者
版权声明:本文标题:生产者 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1686722918h29419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论