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

代码如下:

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 exception
					e.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 stub
		this.storage = storage;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (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 stub
		this.storage = storage;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			for (int i = 0;i < 6;++i){
				this.storage.pop();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值