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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 宋超2356 中级黑马   /  2014-3-27 22:19  /  870 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

过去写过的一个生产者消费者的操作,最近把它写完实现了,发现最早生产的那几个总是最后才被消费~问题在哪呢,不是很明白
还有就是上面如果有三个生产者,运行出来就就是一塌糊涂该怎么改呢
  1. public class Test {
  2.         public static void main(String[] args) {
  3.                 SyncStack ss = new SyncStack();
  4.                 Producer p = new Producer(ss);
  5.                 Consumer c = new Consumer(ss);
  6.                 //new Thread(p).start();    这里面的两句
  7.                 //new Thread(p).start();
  8.                 new Thread(p).start();
  9.                 new Thread(c).start();
  10.         }
  11. }

  12. class Produce {
  13.         int id;
  14.         Produce(int id) {
  15.                 this.id = id;
  16.         }
  17.         public String toString() {
  18.                 return "Produc : " + id;
  19.         }
  20. }

  21. class SyncStack {
  22.         int index = 0;
  23.         Produce[] arr = new Produce[6];
  24.        
  25.         public synchronized void push(Produce pd) {
  26.                 while(index == arr.length) {
  27.                         try {
  28.                                 this.wait();
  29.                         } catch (InterruptedException e) {
  30.                                 e.printStackTrace();
  31.                         }
  32.                 }
  33.                 this.notifyAll();               
  34.                 arr[index] = pd;
  35.                 index ++;
  36.         }
  37.        
  38.         public synchronized Produce pop() {
  39.                 while(index == 0) {
  40.                         try {
  41.                                 this.wait();
  42.                         } catch (InterruptedException e) {
  43.                                 e.printStackTrace();
  44.                         }
  45.                 }
  46.                 this.notifyAll();
  47.                 index--;
  48.                 return arr[index];
  49.         }
  50. }

  51. class Producer implements Runnable {
  52.         SyncStack ss = null;
  53.         Producer(SyncStack ss) {
  54.                 this.ss = ss;
  55.         }
  56.        
  57.         public void run() {
  58.                 for(int i=0; i<20; i++) {
  59.                         Produce pd = new Produce(i);
  60.                         ss.push(pd);
  61. System.out.println("生产了:" + pd);
  62.                         try {
  63.                                 Thread.sleep((int)(Math.random() * 200));
  64.                         } catch (InterruptedException e) {
  65.                                 e.printStackTrace();
  66.                         }                       
  67.                 }
  68.         }
  69. }

  70. class Consumer implements Runnable {
  71.         SyncStack ss = null;
  72.         Consumer(SyncStack ss) {
  73.                 this.ss = ss;
  74.         }
  75.        
  76.         public void run() {
  77.                 for(int i=0; i<20; i++) {
  78.                         Produce pd = ss.pop();
  79. System.out.println("消费了: " + pd);
  80.                         try {
  81.                                 Thread.sleep((int)(Math.random() * 1000));
  82.                         } catch (InterruptedException e) {
  83.                                 e.printStackTrace();
  84.                         }                       
  85.                 }
  86.         }
  87. }
复制代码



评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

5 个回复

倒序浏览
这不是操作系统么?好厉害的样子
回复 使用道具 举报
本帖最后由 霍振鹏 于 2014-3-28 00:07 编辑

我注释的1111111111111的是解决你的第一个问题:先生产的应该先消费
注释22222222222222222的是解决三个生产者问题
  1. package aaaa;
  2. public class Test {
  3.         public static void main(String[] args) {
  4.                 SyncStack ss = new SyncStack();
  5.                 Producer p = new Producer(ss);
  6.                 Consumer c = new Consumer(ss);
  7.                 new Thread(p).start();   // 这里面的两句
  8.                 new Thread(p).start();
  9.                 new Thread(p).start();
  10.                 new Thread(c).start();
  11.         }
  12. }

  13. class Produce {
  14.         int id;
  15.         Produce(int id) {
  16.                 this.id = id;
  17.         }
  18.         public String toString() {
  19.                 return "Produc : " + id;
  20.         }
  21. }

  22. class SyncStack {
  23.         int index = 0;
  24.         int currentputindex=0;///11111111111111111111111111111111111111111111111111111111
  25.         int currentpopindex=0;
  26.         Produce[] arr = new Produce[6];
  27.         
  28.         public synchronized void push(Produce pd) {
  29.                 while(index == arr.length) {//////11111111111111111111111111111111111111111111111111111/
  30.                         try {
  31.                                 this.wait();
  32.                         } catch (InterruptedException e) {
  33.                                 e.printStackTrace();
  34.                         }
  35.                 }
  36.                 this.notifyAll();               
  37.                 arr[(currentputindex++)%arr.length] = pd;//1111111111111111111111111111
  38.                 index ++;
  39.         }
  40.         
  41.         public synchronized Produce pop() {
  42.                 while(index == 0) {
  43.                         try {
  44.                                 this.wait();
  45.                         } catch (InterruptedException e) {
  46.                                 e.printStackTrace();
  47.                         }
  48.                 }
  49.                 this.notifyAll();
  50.                 index--;
  51.                 return arr[(currentpopindex++)%arr.length];///11111111111111111111111111111111111111111111
  52.         }
  53. }

  54. class Producer implements Runnable {
  55.         SyncStack ss = null;
  56.         int i=0;
  57.         Producer(SyncStack ss) {
  58.                 this.ss = ss;
  59.         }
  60.         
  61.         public void run() {
  62.                 for(; i<20; ) {////////22222222222222222222
  63.                        
  64.                         synchronized (ss.getClass()) {////////////////222222222222222222222222222222222222222
  65.                                 if(i<20)
  66.                                 {
  67.                                 Produce pd = new Produce(i);
  68.                                 ss.push(pd);
  69.                                  i++;
  70.                                  System.out.println("生产了:" + pd);
  71.                                 }
  72.                                                 }
  73.                        

  74.                         try {
  75.                                 Thread.sleep((int)(Math.random() * 200));
  76.                         } catch (InterruptedException e) {
  77.                                 e.printStackTrace();
  78.                         }                        
  79.                 }
  80.         }
  81. }

  82. class Consumer implements Runnable {
  83.         SyncStack ss = null;
  84.         Consumer(SyncStack ss) {
  85.                 this.ss = ss;
  86.         }
  87.         
  88.         public void run() {
  89.                 for(int i=0; i<20; i++) {
  90.                         Produce pd = ss.pop();
  91. System.out.println("消费了: " + pd);
  92.                         try {
  93.                                 Thread.sleep((int)(Math.random() * 1000));
  94.                         } catch (InterruptedException e) {
  95.                                 e.printStackTrace();
  96.                         }                        
  97.                 }
  98.         }
  99. }
复制代码

QQ2.png (74.41 KB, 下载次数: 12)

QQ2.png

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报
霍振鹏 发表于 2014-3-28 00:05
我注释的1111111111111的是解决你的第一个问题:先生产的应该先消费
注释22222222222222222的是解决三个生 ...

真心是大神啊~谢谢啦
回复 使用道具 举报
呵呵 互相学习,,,先别结贴,得个技术分先,,,呵呵
回复 使用道具 举报
宋超2356 发表于 2014-3-28 17:49
真心是大神啊~谢谢啦


呵呵 互相学习,,,先别结贴,得个技术分先,,,呵呵
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马