wait/notify实现生产消费者模式

存储类GoodStorage:

import java.util.ArrayList;
import java.util.List;

public class GoodStorage {
	private static final int SIZE = 2;
	private List<Good> storage = new ArrayList<Good>(SIZE);
	
	public void add(Good good){
		synchronized (storage){
			while (storage.size() >= SIZE){
				try {
					System.out.println(Thread.currentThread().getName() + "------>product wait.... now ListSIze:" +storage.size());
					storage.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			storage.add(good);
			System.out.println(Thread.currentThread().getName() + "------>product :"+ good.toString() + ", now ListSIze:" +storage.size());
			
			storage.notifyAll();
		}
	}
	
	public void remove(){
		synchronized (storage){
			while (storage.size() == 0){
				try {
					System.out.println(Thread.currentThread().getName() + "------>consume wait....  now ListSIze:" +storage.size());
					storage.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			Good good = storage.get(0);
			storage.remove(0);
			System.out.println(Thread.currentThread().getName() + "------------------------------------>consume :"+ good.toString()+ ", now ListSIze:" +storage.size());
			
			storage.notifyAll();
		}
	}
	
}

class Good {
	public long id;

	public Good(long id) {
		this.id = id;
	}

	public String toString() {
		return String.format("Good-%d", this.id);
	}
}

生产者:

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

class Producter implements Runnable {
	private static AtomicInteger ato = new AtomicInteger(0);
	
	private GoodStorage store;
	public Producter(GoodStorage store ){
		this.store = store;
	}
	
	
	public void run() {
		while(true){
			try {
				Thread.currentThread().sleep(new Random().nextInt(1000));
				// queue.put(new Good(System.currentTimeMillis()));
				store.add(new Good(ato.incrementAndGet()));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

消费者:

import java.util.Random;

class Consumer implements Runnable {
	private GoodStorage store;
	public Consumer(GoodStorage store ){
		this.store = store;
	}
	

	public void run() {
		while(true){
			try {
				Thread.currentThread().sleep(new Random().nextInt(1000));
				store.remove();
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

运行主类:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
	static ExecutorService pool = Executors.newCachedThreadPool();
	
	public static void main(String[] args) {
		GoodStorage store = new GoodStorage();
		pool.execute(new Producter(store));
		pool.execute(new Producter(store));
		pool.execute(new Producter(store));
		
		pool.execute(new Consumer(store));
		pool.execute(new Consumer(store));
	}
}

运行结果:

pool-1-thread-4------>consume wait....  now ListSIze:0
pool-1-thread-1------>product :Good-1, now ListSIze:1
pool-1-thread-4------------------------------------>consume :Good-1, now ListSIze:0
pool-1-thread-3------>product :Good-2, now ListSIze:1
pool-1-thread-1------>product :Good-3, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-2, now ListSIze:1
pool-1-thread-1------>product :Good-5, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-3, now ListSIze:1
pool-1-thread-1------>product :Good-7, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-5, now ListSIze:1
pool-1-thread-3------>product :Good-4, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-7, now ListSIze:1
pool-1-thread-1------>product :Good-9, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-4, now ListSIze:1
pool-1-thread-2------>product :Good-6, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-9, now ListSIze:1
pool-1-thread-3------>product :Good-8, now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-6, now ListSIze:1
pool-1-thread-3------>product :Good-10, now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-8, now ListSIze:1
pool-1-thread-2------>product :Good-11, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-10, now ListSIze:1
pool-1-thread-2------>product :Good-14, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-11, now ListSIze:1
pool-1-thread-1------>product :Good-12, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-14, now ListSIze:1
pool-1-thread-3------>product :Good-13, now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-12, now ListSIze:1
pool-1-thread-2------>product :Good-15, now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-13, now ListSIze:1
pool-1-thread-5------------------------------------>consume :Good-15, now ListSIze:0
pool-1-thread-3------>product :Good-16, now ListSIze:1
pool-1-thread-1------>product :Good-17, now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-16, now ListSIze:1
pool-1-thread-2------>product :Good-18, now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-17, now ListSIze:1
pool-1-thread-5------------------------------------>consume :Good-18, now ListSIze:0
pool-1-thread-3------>product :Good-19, now ListSIze:1
pool-1-thread-2------>product :Good-20, now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-19, now ListSIze:1
pool-1-thread-3------>product :Good-21, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-20, now ListSIze:1
pool-1-thread-1------>product :Good-22, now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-21, now ListSIze:1
pool-1-thread-4------------------------------------>consume :Good-22, now ListSIze:0
pool-1-thread-2------>product :Good-23, now ListSIze:1
pool-1-thread-3------>product :Good-24, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-23, now ListSIze:1
pool-1-thread-3------>product :Good-27, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-24, now ListSIze:1
pool-1-thread-3------>product :Good-28, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-27, now ListSIze:1
pool-1-thread-3------>product :Good-29, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-28, now ListSIze:1
pool-1-thread-1------>product :Good-25, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-29, now ListSIze:1
pool-1-thread-2------>product :Good-26, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-25, now ListSIze:1
pool-1-thread-1------>product :Good-31, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-26, now ListSIze:1
pool-1-thread-2------>product :Good-32, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-31, now ListSIze:1
pool-1-thread-1------>product :Good-33, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-32, now ListSIze:1
pool-1-thread-3------>product :Good-30, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-33, now ListSIze:1
pool-1-thread-3------>product :Good-35, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-30, now ListSIze:1
pool-1-thread-1------>product :Good-36, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-35, now ListSIze:1
pool-1-thread-2------>product :Good-34, now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-36, now ListSIze:1
pool-1-thread-1------>product :Good-37, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-34, now ListSIze:1
pool-1-thread-2------>product :Good-40, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-37, now ListSIze:1
pool-1-thread-3------>product :Good-38, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-40, now ListSIze:1
pool-1-thread-2------>product :Good-41, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-38, now ListSIze:1
pool-1-thread-1------>product :Good-39, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值