java 多线程,生产者消费者实现。适合多个生产者消费者线程;打印线程编号便于查看正确性

来源:互联网 发布:网络出版 微信 编辑:程序博客网 时间:2024/06/07 22:55

代码如下:

package threadSleep;import java.util.ArrayList;class UsefullObj{}class Storage{ArrayList<UsefullObj> arrayUObj = new ArrayList<UsefullObj>(5);ArrayList<UsefullObj> getArrayUObj(){return arrayUObj;}public void push(UsefullObj usefullObj){synchronized (arrayUObj) {while(arrayUObj.size() >= 5){try{arrayUObj.wait();}catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}arrayUObj.add(usefullObj);//System.out.println(Thread.currentThread().getId());System.out.println(Thread.currentThread().getId()+" thread push storage size = "+arrayUObj.size());arrayUObj.notifyAll();}}public UsefullObj pop() throws InterruptedException{synchronized(arrayUObj){while(arrayUObj.size() <= 0){arrayUObj.wait();}UsefullObj uObj = arrayUObj.remove(0);//System.out.println(Thread.currentThread().getId());System.out.println(Thread.currentThread().getId()+" thread pop storage size = "+arrayUObj.size());arrayUObj.notifyAll();return uObj;}}}class Produce implements Runnable{private Storage storage;public Produce(Storage storage) {// TODO Auto-generated constructor stubthis.storage = storage;}@Overridepublic void run() {// TODO Auto-generated method stubfor (int i = 0;i < 6;++i){UsefullObj usefullObj = new UsefullObj();this.storage.push(usefullObj);}}}class Consumers implements Runnable{private Storage storage;public Consumers(Storage storage) {// TODO Auto-generated constructor stubthis.storage = storage;}@Overridepublic void run() {// TODO Auto-generated method stubtry {for (int i = 0;i < 6;++i){this.storage.pop();}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public class MyProduceComsume {public static void main(String[] args) {Storage gStorage = new Storage();//System.out.println("arrylist size"+gStorage.getArrayUObj().size());Produce objProduce1 = new Produce(gStorage);Produce objProduce2 = new Produce(gStorage);Consumers objConsume1 = new Consumers(gStorage);Consumers objConsume2 = new Consumers(gStorage);Thread tProduce1 = new Thread(objProduce1);Thread tConsumer1 = new Thread(objConsume1);Thread tProduce2 = new Thread(objProduce2);Thread tConsumer2 = new Thread(objConsume2);tProduce1.start();tConsumer1.start();tConsumer2.start();tProduce2.start();}}



结果:

11 thread push storage size = 1
11 thread push storage size = 2
11 thread push storage size = 3
11 thread push storage size = 4
11 thread push storage size = 5
14 thread pop storage size = 4
14 thread pop storage size = 3
14 thread pop storage size = 2
14 thread pop storage size = 1
14 thread pop storage size = 0
13 thread push storage size = 1
13 thread push storage size = 2
13 thread push storage size = 3
13 thread push storage size = 4
13 thread push storage size = 5
12 thread pop storage size = 4
12 thread pop storage size = 3
12 thread pop storage size = 2
12 thread pop storage size = 1
12 thread pop storage size = 0
11 thread push storage size = 1
14 thread pop storage size = 0
13 thread push storage size = 1
12 thread pop storage size = 0


1 0