TestBlockingQueue.java
package com.test.concurrent;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestBlockingQueue {
static void getKey(){
try {
(new BufferedReader(new InputStreamReader(System.in))).readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
static void getKey(String msg){
System.out.println(msg);
getKey();
}
static void test(String msg, BlockingQueue<LiftOff> queue){
System.out.print(msg);
LiftOffRunner runner=new LiftOffRunner(queue);
Thread t=new Thread(runner);
t.start();
for(int i=0;i<5;i++){
runner.add(new LiftOff(5));
}
getKey("Press 'Enter'("+msg+")");
t.interrupt();
System.out.println("finished "+msg+" test");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test("LinkedBlockingQueue",new LinkedBlockingQueue<LiftOff>());
test("ArrayBlockingQueue",new ArrayBlockingQueue<LiftOff>(3));
test("SynchronousQueue",new SynchronousQueue<LiftOff>());
}
}
class LiftOffRunner implements Runnable{
private BlockingQueue<LiftOff> rockets;
public LiftOffRunner(BlockingQueue<LiftOff> queue){
this.rockets=queue;
}
public void add(LiftOff lo){
try{
rockets.put(lo);
}catch(InterruptedException e){
System.out.println("interrupted during put()");
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(!Thread.interrupted()){
LiftOff rocket;
try {
rocket = rockets.take();
rocket.run();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("LiftOff Runner Exiting!!");
}
}
LiftOff.java
package com.test.concurrent;
public class LiftOff implements Runnable {
static int taskCount=0;
private final int id=taskCount++;
protected int countDown=10;
public LiftOff(){}
public LiftOff(int countDown){
this.countDown=countDown;
}
public String status(){
return "#"+id+"("+(countDown==0?"LiftOff":countDown)+"), ";
}
@Override
public void run() {
// TODO Auto-generated method stub
while(countDown-->0){
System.out.print(status());
Thread.yield();
}
}
}
输出为:
LinkedBlockingQueuePress 'Enter'(LinkedBlockingQueue)
#0(4), #0(3), #0(2), #0(1), #0(LiftOff), #1(4), #1(3), #1(2), #1(1), #1(LiftOff), #2(4), #2(3), #2(2), #2(1), #2(LiftOff), #3(4), #3(3), #3(2), #3(1), #3(LiftOff), #4(4), #4(3), #4(2), #4(1), #4(LiftOff),
finished LinkedBlockingQueue test
ArrayBlockingQueue#5(4), #5(3), #5(2), #5(1), #5(LiftOff), Press 'Enter'(ArrayBlockingQueue)
#6(4), #6(3), #6(2), #6(1), #6(LiftOff), #7(4), #7(3), #7(2), #7(1), #7(LiftOff), #8(4), #8(3), #8(2), #8(1), #8(LiftOff), #9(4), #9(3), #9(2), #9(1), #9(LiftOff), java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
at java.util.concurrent.LinkedBlockingQueue.take(Unknown Source)
at com.test.concurrent.LiftOffRunner.run(TestBlockingQueue.java:65)
at java.lang.Thread.run(Unknown Source)
java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
at java.util.concurrent.ArrayBlockingQueue.take(Unknown Source)
at com.test.concurrent.LiftOffRunner.run(TestBlockingQueue.java:65)
at java.lang.Thread.run(Unknown Source)
finished ArrayBlockingQueue test
SynchronousQueue#10(4), #10(3), #10(2), #10(1), #10(LiftOff), #11(4), #11(3), #11(2), #11(1), #11(LiftOff), #12(4), #12(3), #12(2), #12(1), #12(LiftOff), #13(4), #13(3), #13(2), #13(1), #13(LiftOff), Press 'Enter'(SynchronousQueue)
#14(4), #14(3), #14(2), #14(1), #14(LiftOff),
java.lang.InterruptedException
at java.util.concurrent.SynchronousQueue.take(Unknown Source)
at com.test.concurrent.LiftOffRunner.run(TestBlockingQueue.java:65)
at java.lang.Thread.run(Unknown Source)
finished SynchronousQueue test