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