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

© zly1992008 中级黑马   /  2014-6-11 18:16  /  1334 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 zly1992008 于 2014-6-12 13:45 编辑

大家都知道java多线程,以及多线程的线程间通信,那么同志们来一起复习下这个问题吧?{:3_64:}

5 个回复

倒序浏览
单生产和单消费只需要判断一次就行用if
多生产多消费需要判断多次,要while,就这么简单
回复 使用道具 举报
获取锁  释放锁    循环判断锁
回复 使用道具 举报
  1. class Resource
  2. {
  3.         private String name;
  4.         private int  count = 1;
  5.         private boolean flag = false;
  6.        
  7.         public synchronized void set(String name)
  8.         {        while(flag)
  9.                         try {this.wait();} catch(Exception e){}
  10.                 this.name = name+"....."+count++;
  11.                 System.out.println(Thread.currentThread().getName()+"---生产者--"+this.name);
  12.                 flag=true;
  13.                 this.notifyAll();
  14.         }
  15.        
  16.         public synchronized void out()
  17.         {        while(!flag)
  18.                         try {this.wait();} catch(Exception e){}
  19.                 System.out.println(Thread.currentThread().getName()+"---消费者--"+this.name);
  20.                 flag=false;
  21.                 this.notifyAll();
  22.         }
  23. }
  24. class Producer implements Runnable
  25. {
  26.         private Resource res;
  27.         Producer(Resource res)
  28.         {
  29.                 this.res = res ;
  30.         }
  31.         public void run()
  32.         {
  33.                 while(true)
  34.                 {
  35.                         res.set("+商品+");
  36.                 }
  37.         }
  38. }
  39. class Consumer implements Runnable
  40. {
  41.         private Resource res;
  42.         Consumer(Resource res)
  43.         {
  44.                 this.res = res;
  45.         }
  46.         public void run()
  47.         {
  48.                 while(true)
  49.                 {
  50.                         res.out();
  51.                 }
  52.         }
  53. }

  54. class InputOutputDemo04
  55. {
  56.         public  static void main(String[] args)
  57.         {
  58.                 Resource r = new Resource();
  59.                
  60.                 Producer p = new Producer(r);
  61.                 Consumer c = new Consumer(r);
  62.                
  63.                 Thread t1 = new Thread(p);
  64.                 Thread t2 = new Thread(p);
  65.                 Thread t3 = new Thread(c);
  66.                 Thread t4 = new Thread(c);
  67.                 t1.start();
  68.                 t2.start();
  69.                 t3.start();
  70.                 t4.start();
  71.                
  72.                 // new Thread(new Consumer(r)).start();
  73.                  //new Thread(new Producer(r)).start();
  74.                 //System.out.println("hello word");
  75.         }
  76. }
复制代码

代码一交,一目了然。。。哇卡卡卡卡卡{:3_51:}
回复 使用道具 举报
18353666072 发表于 2014-6-12 08:28
代码一交,一目了然。。。哇卡卡卡卡卡

1.5升级版你懂得的。。。。。爽就一个字
回复 使用道具 举报
zly1992008 发表于 2014-6-12 10:20
1.5升级版你懂得的。。。。。爽就一个字

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