测试结果显示:阻塞队列和并发队列在高并发情况下,性能相差不大。
1 Test.java代码参考http://blog.csdn.net/arkblue/archive/2011/01/14/6138598.aspx
- public class ConcurrentQueueTest {
- private static int COUNT = 100000;
- private static int THREAD_NUM = 10;
- private static CyclicBarrierThread cyclicBarrierThread = new CyclicBarrierThread();
- private static ConcurrentLinkedQueue conQueue = new ConcurrentLinkedQueue();
- private static LinkedBlockingQueue linkQueue = new LinkedBlockingQueue();
- static class ConcurrentLinkedQueueProducer extends Test {
- public ConcurrentLinkedQueueProducer(String id, CyclicBarrier barrier,
- long count, int threadNum, ExecutorService executor) {
- super(id, barrier, count, threadNum, executor);
- }
- @Override
- protected void test() {
- conQueue.add(1);
- }
- }
- static class LinkedBlockingQueueProducer extends Test {
- public LinkedBlockingQueueProducer(String id, CyclicBarrier barrier,
- long count, int threadNum, ExecutorService executor) {
- super(id, barrier, count, threadNum, executor);
- }
- @Override
- protected void test() {
- conQueue.add(1);
- }
- }
- static class CyclicBarrierThread extends Thread {
- @Override
- public void run() {
- conQueue.clear();
- linkQueue.clear();
- }
- }
- public static void test(String id, long count, int threadNum,
- ExecutorService executor) {
- final CyclicBarrier barrier = new CyclicBarrier(threadNum + 1,
- cyclicBarrierThread);
- System.out.println("==============================");
- System.out.println("count = " + count + "/t" + "Thread Count = "
- + threadNum);
- concurrentTotalTime += new ConcurrentLinkedQueueProducer(
- "ConcurrentLinkedQueueProducer", barrier, COUNT, threadNum,
- executor).startTest();
- linkedBlockingTotalTime += new LinkedBlockingQueueProducer(
- "LinkedBlockingQueueProducer ", barrier, COUNT, threadNum,
- executor).startTest();
- totalThreadCount += threadNum;
- executor.shutdownNow();
- System.out.println("==============================");
- }
- static long concurrentTotalTime = 0;
- static long linkedBlockingTotalTime = 0;
- static long totalThreadCount = 0;
- public static void main(String[] args) throws InterruptedException {
- for (int i = 1; i < 20; i++) {
- ExecutorService executor = Executors.newFixedThreadPool(THREAD_NUM
- * i);
- test("", COUNT, 10 * i, executor);
- }
- System.out.println("ConcurrentLinkedQueue Avg Time = "
- + concurrentTotalTime / totalThreadCount);
- System.out.println("LinkedBlockingQueue Avg Time = "
- + linkedBlockingTotalTime / totalThreadCount);
- }
- }
结果,执行100,000次的并发入队操作,并发队列需要49毫秒,阻塞队列需要53毫秒,比阻塞队列平均快4毫秒。这种平均值的算法可能不准确,因为随着线程数量的增加,线程之间切换的开销会逐渐增大。
- ==============================
- count = 100000 Thread Count = 10
- ConcurrentLinkedQueueProducer = 422
- LinkedBlockingQueueProducer = 390
- ==============================
- ==============================
- count = 100000 Thread Count = 20
- ConcurrentLinkedQueueProducer = 782
- LinkedBlockingQueueProducer = 828
- ==============================
- ==============================
- count = 100000 Thread Count = 30
- ConcurrentLinkedQueueProducer = 1468
- LinkedBlockingQueueProducer = 1422
- ==============================
- ==============================
- count = 100000 Thread Count = 40
- ConcurrentLinkedQueueProducer = 2188
- LinkedBlockingQueueProducer = 1297
- ==============================
- ==============================
- count = 100000 Thread Count = 50
- ConcurrentLinkedQueueProducer = 2609
- LinkedBlockingQueueProducer = 2625
- ==============================
- ==============================
- count = 100000 Thread Count = 60
- ConcurrentLinkedQueueProducer = 3078
- LinkedBlockingQueueProducer = 2969
- ==============================
- ==============================
- count = 100000 Thread Count = 70
- ConcurrentLinkedQueueProducer = 4046
- LinkedBlockingQueueProducer = 3594
- ==============================
- ==============================
- count = 100000 Thread Count = 80
- ConcurrentLinkedQueueProducer = 4047
- LinkedBlockingQueueProducer = 4078
- ==============================
- ==============================
- count = 100000 Thread Count = 90
- ConcurrentLinkedQueueProducer = 4469
- LinkedBlockingQueueProducer = 5109
- ==============================
- ==============================
- count = 100000 Thread Count = 100
- ConcurrentLinkedQueueProducer = 5125
- LinkedBlockingQueueProducer = 4922
- ==============================
- ==============================
- count = 100000 Thread Count = 110
- ConcurrentLinkedQueueProducer = 5531
- LinkedBlockingQueueProducer = 5969
- ==============================
- ==============================
- count = 100000 Thread Count = 120
- ConcurrentLinkedQueueProducer = 5172
- LinkedBlockingQueueProducer = 6188
- ==============================
- ==============================
- count = 100000 Thread Count = 130
- ConcurrentLinkedQueueProducer = 7187
- LinkedBlockingQueueProducer = 6594
- ==============================
- ==============================
- count = 100000 Thread Count = 140
- ConcurrentLinkedQueueProducer = 6937
- LinkedBlockingQueueProducer = 7782
- ==============================
- ==============================
- count = 100000 Thread Count = 150
- ConcurrentLinkedQueueProducer = 6921
- LinkedBlockingQueueProducer = 8875
- ==============================
- ==============================
- count = 100000 Thread Count = 160
- ConcurrentLinkedQueueProducer = 8047
- LinkedBlockingQueueProducer = 9250
- ==============================
- ==============================
- count = 100000 Thread Count = 170
- ConcurrentLinkedQueueProducer = 8235
- LinkedBlockingQueueProducer = 9656
- ==============================
- ==============================
- count = 100000 Thread Count = 180
- ConcurrentLinkedQueueProducer = 9062
- LinkedBlockingQueueProducer = 9719
- ==============================
- ==============================
- count = 100000 Thread Count = 190
- ConcurrentLinkedQueueProducer = 9422
- LinkedBlockingQueueProducer = 9609
- ==============================
- ConcurrentLinkedQueue Avg Time = 49
- LinkedBlockingQueue Avg Time = 53