黑马程序员技术交流社区

标题: 生产者消费者问题 [打印本页]

作者: yefeidd    时间: 2015-8-12 17:27
标题: 生产者消费者问题
  1. import java.util.concurrent.locks.Condition;
  2. import java.util.concurrent.locks.Lock;
  3. import java.util.concurrent.locks.ReentrantLock;

  4. /*
  5. * @jdk5.0以后的版本中,用lock()等方法替换了synchronized,用condition替换了wait,notify,notifyAll。
  6. * @同样的生产者消费者问题。
  7. */

  8. class Resource2// 资源
  9. {
  10.         private String name;
  11.         private int Count = 1;
  12.         private Boolean flag = false;

  13.         private Lock lock = new ReentrantLock();
  14.         private Condition condition_con = lock.newCondition();//消费者对象
  15.         private Condition condition_pro = lock.newCondition();//生产者对象
  16.         public void set(String name) throws InterruptedException// await未抛出异常(生产者)
  17.         {
  18.                 lock.lock();
  19.                 // if(flag)//将if替换为while可以避免多个线程生产消费的安全问题
  20.                 try {
  21.                         while (flag)
  22.                                 condition_pro.await();// await要抛出异常,因为没有写catch所有throw一下
  23.                         this.name = name + "-------" + Count++;
  24.                         System.out.println(Thread.currentThread().getName() + "---生产者"
  25.                                         + this.name);
  26.                         condition_con.signal();//消费者唤醒
  27.                         flag = true;
  28.                 } finally {
  29.                         lock.unlock();
  30.                 }
  31.         }

  32.         public void out() throws InterruptedException //消费者
  33.         {
  34.                 lock.lock();
  35.                 try {
  36.                         // if(!flag)//将if替换为while可以避免多个线程生产消费的安全问题
  37.                         while (!flag)
  38.                                 condition_con.await();
  39.                         System.out.println(Thread.currentThread().getName()
  40.                                         + "----------消费者" + this.name);
  41.                         flag = false;
  42.                         condition_pro.signal();//生产者唤醒
  43.                 } finally {
  44.                         lock.unlock();
  45.                 }
  46.         }
  47. }

  48. class producer2 implements Runnable {
  49.         private Resource2 r;

  50.         producer2(Resource2 r) {
  51.                 this.r = r;
  52.         }

  53.         public void run() {
  54.                 while (true) {
  55.                         try {
  56.                                 r.set("商品");
  57.                         } catch (InterruptedException e) {
  58.                         }
  59.                 }
  60.         }
  61. }

  62. class Consumer2 implements Runnable {
  63.         private Resource2 r;

  64.         Consumer2(Resource2 r) {
  65.                 this.r = r;
  66.         }

  67.         public void run() {
  68.                 while (true) {
  69.                         try {
  70.                                 r.out();
  71.                         } catch (InterruptedException e) {

  72.                         }
  73.                 }
  74.         }
  75. }

  76. public class ProducerConsumerDemo2 {

  77.         public static void main(String[] args) {

  78.                 Resource2 r = new Resource2();
  79.                 producer2 pro = new producer2(r);
  80.                 Consumer2 con = new Consumer2(r);

  81.                 Thread t1 = new Thread(pro);
  82.                 Thread t2 = new Thread(pro);
  83.                 Thread t3 = new Thread(con);
  84.                 Thread t4 = new Thread(con);

  85.                 t1.start();
  86.                 t2.start();
  87.                 t3.start();
  88.                 t4.start();
  89.         }

  90. }
复制代码



作者: 沉醉    时间: 2015-8-12 17:41
yefeidd 发表于 2015-8-12 17:27

不错,膜拜!!
作者: 星语千寻    时间: 2015-8-12 19:10
路过。。。
作者: yefeidd    时间: 2015-8-13 10:37
沉醉 发表于 2015-8-12 17:41
不错,膜拜!!

饿,,,笔记而已
作者: yefeidd    时间: 2015-8-13 10:38
星语千寻 发表于 2015-8-12 19:10
路过。。。

送你个酱油瓶
作者: 耀阳圣尊    时间: 2015-8-13 11:36
写的不错,赞一个
作者: 风华正茂    时间: 2015-8-13 12:59
楼主得笔记写得不错,赞一个




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2