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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© iceknc 中级黑马   /  2015-9-21 22:24  /  253 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.concurrent.locks.*;

  2. public class Goods {
  3.         private String name;
  4.         private int count = 0;
  5.         private boolean flag = false;
  6.         //定义一个锁
  7.         private final Lock lock = new ReentrantLock();
  8.         //定义生产者持的锁
  9.         private final Condition conditionPro = lock.newCondition();
  10.         //定义消费者持的锁
  11.         private final Condition conditionCon = lock.newCondition();
  12.        
  13.         public void in(String name) throws InterruptedException {
  14.                 lock.lock();
  15.                 try{
  16.                         while(flag)
  17.                                 conditionPro.await();
  18.                         this.name = name + "---" + count++;
  19.                         System.out.println(Thread.currentThread().getName() + " 生产 " + this.name);
  20.                         flag = true;
复制代码
  1. public class Producer implements Runnable {
  2.         private Goods g;
  3.        
  4.         public Producer(){}
  5.         public Producer(Goods g) {
  6.                 this.g = g;
  7.         }
  8.        
  9.         public void run(){
  10.                 while(true){
  11.                         try{
  12.                                 g.in("#商品#");
  13.                         }
  14.                         catch(InterruptedException e){
  15.                        
  16.                         }
  17.                 }               
  18.         }
  19. }
复制代码

  1. public class Consumer implements Runnable{
  2.         private Goods g;
  3.        
  4.         public Consumer(){}
  5.         public Consumer(Goods g) {
  6.                 this.g = g;
  7.         }
  8.        
  9.         public void run(){
  10.                 while(true){
  11.                         try{
  12.                                 g.out();
  13.                         }
  14.                         catch(InterruptedException e){
  15.                        
  16.                         }
  17.                 }       
  18.         }
  19. }
复制代码
  1. public class TestMain {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub
  7.                 Goods g = new Goods();
  8.                 Producer p = new Producer(g);
  9.                 Consumer c = new Consumer(g);
  10.                
  11.                 new Thread(p).start();
  12.                 new Thread(p).start();
  13.                 new Thread(c).start();
  14.                 new Thread(c).start();
  15.         }
  16. }
复制代码

重要的代码写三遍,今天终于可以自己背着写出来了。



1 个回复

倒序浏览
  1. import java.util.concurrent.locks.*;

  2. public class Goods {
  3.         private String name;
  4.         private int count = 0;
  5.         private boolean flag = false;
  6.         //定义一个锁
  7.         private final Lock lock = new ReentrantLock();
  8.         //定义生存者持的锁
  9.         private final Condition conditionPro = lock.newCondition();
  10.         //定义消费者持的锁
  11.         private final Condition conditionCon = lock.newCondition();
  12.        
  13.         public void in(String name) throws InterruptedException {
  14.                 lock.lock();
  15.                 try{
  16.                         while(flag)
  17.                                 conditionPro.await();
  18.                         this.name = name + "---" + count++;
  19.                         System.out.println(Thread.currentThread().getName() + " 生产 " + this.name);
  20.                         flag = true;
  21.                         conditionCon.signal();
  22.                 }
  23.                 finally{
  24.                         lock.unlock();
  25.                 }
  26.                        
  27.         }       
  28.         public void out()throws InterruptedException {
  29.                 lock.lock();
  30.                 try{
  31.                         while(!flag)
  32.                                 conditionCon.await();
  33.                         System.out.println(Thread.currentThread().getName() + " ...消费 " + this.name);
  34.                         flag = false;
  35.                         conditionPro.signal();
  36.                 }
  37.                 finally{
  38.                         lock.unlock();
  39.                 }
  40.         }
  41. }
复制代码


复制漏了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马