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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张忠豹 中级黑马   /  2012-10-10 20:24  /  1440 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package day26;
  2. /**超过两个线程的
  3. * 生产者消费者模式*/
  4. public class ProducerAndCustomerTest {
  5.         public static void main(String args[]) {
  6.                 Resource4 rs = new Resource4();
  7.                 Producer producer = new Producer(rs);
  8.                 Customer customer = new Customer(rs);
  9.                 Thread thread1 = new Thread(producer);
  10.                 Thread thread2 = new Thread(producer);
  11.                 Thread thread3 = new Thread(customer);
  12.                 Thread thread4 = new Thread(customer);
  13.                 thread1.start();
  14.                 thread2.start();
  15.                 thread3.start();
  16.                 thread4.start();
  17.         }
  18. }
  19. class Resource4{
  20.         private String name;
  21.         private int count=1 ;
  22.         private boolean flag = false;
  23.         // t1  t2
  24.         public synchronized void set(String name){
  25.                 while(flag){
  26.                         try{this.wait();}catch(Exception e){}//t1(放弃资格) t2(获取资格)
  27.                 }
  28. //                if(flag){
  29. //                        System.out.println("生产者应该挂起");
  30. //                        try{this.wait();}catch(Exception e){}
  31. //                }
  32.                 System.out.println("生产线程挂起结束");
  33.                 this.name = name+"----"+(count++);
  34.                 System.out.println(Thread.currentThread().getName()+"^^^生产者"+this.name);
  35.                 flag = true;
  36.                 this.notifyAll();
  37.                 //this.notify();
  38.         }
  39.         public synchronized void out(){
  40.                 while(!flag){
  41.                         try{this.wait();}catch(Exception e){}//t3(放弃资格) t4(获取资格)
  42.                 }
  43. //                if(!flag){
  44. //                        System.out.println("消费者应该挂起");
  45. //                        try{this.wait();}catch(Exception e){}
  46. //                       
  47. //                }
  48.                 System.out.println("消费线程挂起结束");
  49.                 System.out.println(Thread.currentThread().getName()+"^^^消费者"+this.name);
  50.                 flag = false;
  51.                 this.notifyAll();
  52.                 //this.notify();
  53.         }
  54. }
  55. class Producer implements Runnable{
  56.         private Resource4 rs ;
  57.         public Producer(Resource4 rs){
  58.                 this.rs = rs;
  59.         }
  60.         public void run(){
  61.                 while(true){
  62.                         rs.set("商品");
  63.                 }
  64.         }
  65. }
  66. class Customer implements Runnable{
  67.         private Resource4 rs ;
  68.         public Customer(Resource4 rs){
  69.                 this.rs = rs;
  70.         }
  71.         public void run(){
  72.                 while(true){
  73.                         rs.out();
  74.                 }
  75.         }
  76. }
复制代码
为什么我的产品序号不对啊?

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

3 个回复

倒序浏览
你确定你的产品序号不对吗?
回复 使用道具 举报
曾浩 发表于 2012-10-10 21:49
你确定你的产品序号不对吗?

真的不对啊,我的序号都是从10000多开始,怎么错的一直搞不出来,对比视频上的代码,也没发现有什么不一样的……哎
回复 使用道具 举报
张忠豹 发表于 2012-10-11 21:15
真的不对啊,我的序号都是从10000多开始,怎么错的一直搞不出来,对比视频上的代码,也没发现有什么不一 ...

你能把你那个产品序号不对的地方 贴个图出来吗 我怎么运行你的程序没有问题呢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马