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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© java—family 中级黑马   /  2014-8-6 01:29  /  712 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. 想搞个停止生产,和消费的方法,      不知道从那下手,百度也没出来,会的提示一下。
复制代码


1 个回复

倒序浏览
  1. package prouderComsumer;

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

  5. //多线程间通信问题               <生产烤鸭问题>
  6. /*resoures  producer consumer  和主函数创建线程4个类。resoures定义生产和消费的方法,producer和consumer定义接受resoures的构造函数
  7. * ,并实现多线程。*/
  8. public class ProuderComsumerDemo {
  9.                         public static void main(String[] args) {
  10.                                 resoures r = new resoures();
  11.                                 producer pro = new producer(r);
  12.                                 consumer con =new consumer(r);
  13.                                 Thread t1 = new Thread(pro);
  14.                                 Thread t2 = new Thread(pro);
  15.                                 Thread t3 = new Thread(con);
  16.                                 Thread t4 = new Thread(con);
  17.                                 t1.start();
  18.                                 t2.start();
  19.                                 t3.start();
  20.                             t4.start();
  21. }
  22. }
  23. //资源
  24.   class resoures {
  25.           //定义变量:生产什么。生产多少了,生产与消费之间的切换变量
  26.           private String name;
  27.           private  int count=0;
  28.           private  boolean falg =false;
  29.           //生产方法
  30.        
  31.           public synchronized void set(String name){
  32.                   // 什么时候生产
  33.                
  34.                   while(falg)
  35.                         try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}//睡觉
  36.                    //生产      
  37.                      count++;
  38.                   if(count==1000){
  39.                          
  40.                   }
  41.                   this.name = name+count;
  42.                   System.out.println(Thread.currentThread().getName()+"生 产 了"+this.name);
  43.                   //生产完了要告诉消费者
  44.                   falg=true;
  45.                   notifyAll();
  46.                  
  47.          
  48.           }
  49.          //消费方法
  50.           public synchronized void out(){
  51.                       while(!falg)
  52.                                 try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}
  53.                      
  54.                     System.out.println(Thread.currentThread().getName()+"消 费 了"+this.name);
  55.                     falg=false;
  56.                           notifyAll();
  57.           }
  58.   }
  59. //生产者
  60. class producer implements Runnable{
  61.          private resoures r;
  62.          
  63.         public producer(resoures r) {
  64.                         super();
  65.                         this.r = r;
  66.                 }

  67.         @Override
  68.         public void run() {
  69.                    while(true){
  70.                            r.set("烤鸭");
  71.                           
  72.                    }
  73.                
  74.         }
  75.        
  76. }
  77. //消费者
  78. class consumer implements Runnable{
  79.     private resoures r;
  80.    
  81. public consumer(resoures r) {
  82.                 super();
  83.                 this.r = r;
  84.         }

  85. @Override
  86. public void run() {
  87.            while(true){
  88.                    r.out();
  89.                   
  90.                           
  91.                   
  92.            }
  93.        
  94. }

  95. }
复制代码

上面的问题怎么没代码  ,郁闷。。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马