简单使用BlockingQueue(阻塞队列)

1.需求分析
由于BlockingQueue很适合于构建 “生产者-消费者" 模型,所以此处我打算对该行为进行模拟–生产者每100毫秒生成一个商品,而消费者每50毫秒消费一个商品,并将put,take等操作的日志信息打印到控制台,来简单的体验一下BlockingQueue。
2.Product类
public class Product extends Thread{
	private BlockingQueue<Goods> bq ;
	public Product(BlockingQueue<Goods> bq,String name) {
		super(name);
		this.bq = bq;
	}
	@Override
	public void run() {
		Thread thread = Thread.currentThread();
		try {
			Thread.sleep(100);
			Goods goods = new Goods(thread.getName());
			long start = System.currentTimeMillis();
			System.out.println(thread.getName()+" start put ==> "+start);
			bq.put(goods);
			long end = System.currentTimeMillis();
			System.out.println(thread.getName()+" end put ==> "+end);

		} catch (InterruptedException e) {
			thread.interrupt();
		}
	}
}
3.Customer类
public class Customer extends Thread{
	private BlockingQueue<Goods> bq ;
	public Customer(BlockingQueue<Goods> bq,String name) {
		super(name);
		this.bq = bq;
	}
	@Override
	public void run() {
		Thread thread = Thread.currentThread();
		try {
			Thread.sleep(50);
			long start = System.currentTimeMillis();
			System.out.println(thread.getName()+" start take ==> "+start);
			bq.take().doSomeThing();
			long end = System.currentTimeMillis();
			System.out.println(thread.getName()+" end take ==> "+end);
		} catch (InterruptedException e) {
			thread.interrupt();
		}
	}
}
4.Goods类
public class Goods {
	private static long count = 0;
	private final String name;
	private final int id;
	public Goods(String name) {
		this.name = name;
		synchronized (Goods.class) {
			id = (int)count++;
		}
	}
	@Override
	public String toString() {
		return "Goods [name=" + name + ", id=" + id + "]";
	}
	public void doSomeThing(){
		System.out.println(Thread.currentThread().getName()+" have "+this);
	}
}
5.测试启动类
public class QueueTest {
	public static void main(String[] args) throws InterruptedException {
		BlockingQueue<Goods> bq = new ArrayBlockingQueue<>(10);
		
		for(int i=0;i<10;i++){
			new Customer(bq,"Customer "+i+" ").start();
		}
		
		for(int i=0;i<10;i++){
			new Product(bq,"Product "+i+" ").start();
		}
		
		Thread.sleep(500);
		System.exit(0);
	}
}
6.运行结果
Customer 1  start take ==> 1493209509175
Customer 6  start take ==> 1493209509176
Customer 5  start take ==> 1493209509176
Customer 3  start take ==> 1493209509176
Customer 8  start take ==> 1493209509176
Customer 9  start take ==> 1493209509177
Customer 0  start take ==> 1493209509175
Customer 4  start take ==> 1493209509175
Customer 2  start take ==> 1493209509175
Customer 7  start take ==> 1493209509178
Product 0  start put ==> 1493209509227
Product 1  start put ==> 1493209509228
Product 1  end put ==> 1493209509228
Product 2  start put ==> 1493209509228
Product 2  end put ==> 1493209509228
Product 3  start put ==> 1493209509227
Product 4  start put ==> 1493209509229
Product 0  end put ==> 1493209509228
Goods===>Customer 1  have Goods [name=Product 2 , id=3]
Goods===>Customer 6  have Goods [name=Product 0 , id=0]
Goods===>Customer 3  have Goods [name=Product 3 , id=1]
Customer 3  end take ==> 1493209509229
Product 4  end put ==> 1493209509229
Customer 1  end take ==> 1493209509229
Product 6  start put ==> 1493209509230
Goods===>Customer 5  have Goods [name=Product 1 , id=2]
Customer 5  end take ==> 1493209509231
Product 3  end put ==> 1493209509229
Product 5  start put ==> 1493209509231
Product 9  start put ==> 1493209509231
Product 8  start put ==> 1493209509230
Product 8  end put ==> 1493209509231
Product 6  end put ==> 1493209509230
Goods===>Customer 2  have Goods [name=Product 8 , id=6]
Customer 2  end take ==> 1493209509232
Goods===>Customer 0  have Goods [name=Product 6 , id=5]
Customer 0  end take ==> 1493209509232
Goods===>Customer 8  have Goods [name=Product 4 , id=4]
Customer 8  end take ==> 1493209509233
Product 7  start put ==> 1493209509233
Goods===>Customer 7  have Goods [name=Product 7 , id=9]
Customer 6  end take ==> 1493209509229
Customer 7  end take ==> 1493209509233
Goods===>Customer 9  have Goods [name=Product 9 , id=8]
Customer 9  end take ==> 1493209509234
Product 9  end put ==> 1493209509233
Goods===>Customer 4  have Goods [name=Product 5 , id=7]
Customer 4  end take ==> 1493209509234
Product 5  end put ==> 1493209509231
Product 7  end put ==> 1493209509233
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值