java多线程之生存者与消费者(Java编程思想)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java多线程之生存者与消费者(Java编程思想),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4165字,纯文字阅读大概需要6分钟。
内容图文
1.Restaurant
package Produce; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; class Meal { private final int orderNum; public Meal(int orderNum) { this.orderNum = orderNum; } @Override public String toString() { return "Meal " + orderNum; } } class WaitPerson implements Runnable { private Restaurant restaurant; public WaitPerson(Restaurant r) { restaurant = r; } @Override publicvoid run() { try { while (!Thread.interrupted()) { synchronized (this) { while (restaurant.meal == null) wait(); // 等待chef生产meal } System.out.println("Waitperson got " + restaurant.meal); synchronized (restaurant.chef) { restaurant.meal = null; restaurant.chef.notifyAll();// 通知chef继续生产 } } } catch (InterruptedException e) { System.out.println("WaitPerson interrupted"); } } } class Chef implements Runnable { private Restaurant restaurant; privateint count = 0; public Chef(Restaurant r) { restaurant = r; } @Override publicvoid run() { try { while (!Thread.interrupted()) { synchronized (this) { while (restaurant.meal != null) wait();// 等待meal被拿走 } if (++count == 10) { System.out.println("Out of food,closing"); restaurant.exec.shutdownNow();//向每个线程发送Interruptreturn;//如果没有直接return 将多执行了下面的 Order up ,所以一般情况下都要直接return } System.out.println("Order up! "); synchronized (restaurant.waitPerson) { // 对notifyAll()的调用必须先获得waitPerson的锁 restaurant.meal = new Meal(count); restaurant.waitPerson.notifyAll(); } TimeUnit.MILLISECONDS.sleep(500);//休眠一下是为了给shutdownNow留出时间 } } catch (InterruptedException e) { System.out.println("Chef interrupted"); } } } publicclass 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); } publicstaticvoid main(String[] args) { new Restaurant(); } }
2.使用java.util.concurrent.locks.Condition进行同步操作
Lock和Condition对象只有在更加困难的多线程问题中才是必需的。
package Produce; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class Car { private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); privateboolean waxOn = false; publicvoid waxed() { lock.lock(); try { waxOn = true;// Ready to buff condition.signalAll(); } finally { lock.unlock(); } } publicvoid buffed() { lock.lock(); try { waxOn = false;// Ready for another coat of wax condition.signalAll(); } finally { lock.unlock(); } } publicvoid waitForWaxing() throws InterruptedException { lock.lock(); try { while (waxOn == false) condition.await(); } finally { lock.unlock(); } } publicvoid waitForBuffing() throws InterruptedException { lock.lock(); try { while (waxOn == true) condition.await(); } finally { lock.unlock(); } } } class WaxOn implements Runnable { private Car car; public WaxOn(Car c) { car = c; } @Override publicvoid run() { try { while (!Thread.interrupted()) // while (true) //与使用这行效果一样 { System.out.println("Wax On!"); TimeUnit.MILLISECONDS.sleep(200); car.waxed(); car.waitForBuffing(); } } catch (InterruptedException e) { System.out.println("Exiting via interrupt"); } System.out.println("Ending Wax On task"); } } class WaxOff implements Runnable { private Car car; public WaxOff(Car c) { car = c; } @Override publicvoid run() { try { while (!Thread.interrupted()) // while (true) { car.waitForWaxing(); System.out.println("Wax Off!"); TimeUnit.MILLISECONDS.sleep(200); car.buffed(); } } catch (InterruptedException e) { System.out.println("Exiting via interrupt"); } System.out.println("Ending Wax Off task"); } } publicclass WaxOMatic2 { publicstaticvoid main(String[] args) throws InterruptedException { Car car = new Car(); ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(new WaxOff(car)); exec.execute(new WaxOn(car)); TimeUnit.SECONDS.sleep(5); exec.shutdownNow(); } }
原文:http://www.cnblogs.com/zhuawang/p/3776987.html
内容总结
以上是互联网集市为您收集整理的java多线程之生存者与消费者(Java编程思想)全部内容,希望文章能够帮你解决java多线程之生存者与消费者(Java编程思想)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。