A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. import java.util.concurrent.locks.Condition;
  2. import java.util.concurrent.locks.Lock;
  3. import java.util.concurrent.locks.ReentrantLock;

  4. public class ProducerConcumer {
  5.         public static void main(String[] args) {
  6.                 Res res = new Res();
  7.                 Producer pro = new Producer(res);
  8.                 Concumer con = new Concumer(res);
  9.                 Thread t1 = new Thread(pro);
  10.                 Thread t2 = new Thread(pro);
  11.                 Thread t3 = new Thread(con);
  12.                 Thread t4 = new Thread(con);
  13.                 t1.start();
  14.                 t2.start();
  15.                 t3.start();
  16.                 t4.start();
  17.         }
  18. }

  19. class Res {
  20.         private String name;
  21.         private int count = 1;
  22.         boolean flag = false;
  23.         private Lock lock = new ReentrantLock();
  24.         Condition condition_pro = lock.newCondition();
  25.         Condition condition_con = lock.newCondition();

  26.         public void set(String name) throws InterruptedException {
  27.                 lock.lock();
  28.                 try {
  29.                         while (flag) {

  30.                                 condition_pro.await();

  31.                         }
  32.                         this.name = name + "..." + count++;
  33.                         System.out.println(Thread.currentThread().getName() + "生产者"
  34.                                         + this.name);
  35.                         flag = true;
  36.                         condition_con.signal();
  37.                 } finally {
  38.                         lock.unlock();
  39.                 }

  40.         }

  41.         public void out() throws InterruptedException {
  42.                 lock.lock();
  43.                 try {
  44.                         while (!flag) {
  45.                                 condition_con.await();
  46.                         }

  47.                         System.out.println(Thread.currentThread().getName() + "......."
  48.                                         + this.name);
  49.                         flag = false;
  50.                         condition_pro.signal();
  51.                 } finally {
  52.                         lock.unlock();
  53.                 }
  54.         }
  55. }

  56. class Producer implements Runnable {
  57.         private Res res;

  58.         public Producer(Res res) {
  59.                 this.res = res;
  60.         }

  61.         public void run() {
  62.                 // TODO Auto-generated method stub
  63.                 try {
  64.                         res.set("");
  65.                 } catch (InterruptedException e) {
  66.                         // TODO Auto-generated catch block
  67.                         e.printStackTrace();
  68.                 }

  69.         }

  70. }

  71. class Concumer implements Runnable {
  72.         private Res res;

  73.         public Concumer(Res res) {
  74.                 this.res = res;
  75.         }

  76.         public void run() {
  77.                 // TODO Auto-generated method stub
  78.                 try {
  79.                         res.out();
  80.                 } catch (InterruptedException e) {
  81.                         // TODO Auto-generated catch block
  82.                         e.printStackTrace();
  83.                 }
  84.         }

  85. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 淡定

查看全部评分

2 个回复

倒序浏览
你的代码没问题啊,每个线程任务都可以执行一次,你是不是觉得不能像视频里那样生产很多啊?如果是的话原因就是你在生产消费时都没加循环
回复 使用道具 举报
刘张朋 发表于 2013-7-19 23:28
你的代码没问题啊,每个线程任务都可以执行一次,你是不是觉得不能像视频里那样生产很多啊?如果是的话原因 ...

哦哦,是啊,就是出来四个。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马