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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 不怕黑人 中级黑马   /  2015-7-22 22:19  /  176 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class Day12Test2 {

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

  3.                         Resouce r = new Resouce();
  4.                         new Thread(new Producer(r)).start();
  5.                         new Thread(new Producer(r)).start();
  6.                         new Thread(new Consumer(r)).start();
  7.                         new Thread(new Consumer(r)).start();
  8.                 }

  9.         }
  10.         class Resouce{
  11.                
  12.                 private String name;
  13.                 private int count = 1;
  14.                 private boolean flag = false;
  15.                 private ReentrantLock lock = new ReentrantLock();
  16.                 private Condition condition_pro = lock.newCondition();
  17.                 private Condition condition_con = lock.newCondition();
  18.                 public void set(String name) throws InterruptedException{
  19.                         lock.lock();
  20.                                 try{
  21.                                         while(flag)
  22.                                                 condition_con.await();
  23.                                         this.name = name + count++;
  24.                                         System.out.println(Thread.currentThread().getName()+"...生产者......"+this.name);
  25.                                         flag = true;
  26.                                         condition_pro.signal();
  27.                                 }
  28.                                 finally{
  29.                                         lock.unlock();
  30.                                 }
  31.                        
  32.                 }
  33.                 public void out() throws InterruptedException{
  34.                         lock.lock();
  35.                                 try{
  36.                                         while(!flag)
  37.                                                 condition_pro.await();
  38.                                         System.out.println(Thread.currentThread().getName()+"+消费者+"+this.name);
  39.                                         flag = false;
  40.                                         condition_con.signal();
  41.                                 }
  42.                                 finally{
  43.                                         lock.unlock();
  44.                                 }
  45.                 }
  46.         }
  47.         class Producer implements Runnable{
  48.                 private Resouce r;
  49.                 Producer(Resouce r){
  50.                         this.r = r;
  51.                 }
  52.                 public void run(){
  53.                         while(true){
  54.                                 try {
  55.                                         r.set("汉堡");
  56.                                 } catch (InterruptedException e) {
  57.                                 }
  58.                         }
  59.                 }
  60.         }
  61.         class Consumer implements Runnable{
  62.                 private Resouce r;
  63.                 Consumer(Resouce r){
  64.                         this.r = r;
  65.                 }
  66.                 public void run(){
  67.                         while(true){
  68.                                 try {
  69.                                         r.out();
  70.                                 } catch (InterruptedException e) {

  71.                                 }
  72.                         }
  73.                 }
  74.         }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马