并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue在入队操作高并发性能比较

测试结果显示:阻塞队列和并发队列在高并发情况下,性能相差不大。

 

 

1 Test.java代码参考http://blog.csdn.net/arkblue/archive/2011/01/14/6138598.aspx

 

 

  1. public class ConcurrentQueueTest {  
  2.     private static int COUNT = 100000;  
  3.     private static int THREAD_NUM = 10;  
  4.     private static CyclicBarrierThread cyclicBarrierThread = new CyclicBarrierThread();  
  5.   
  6.     private static ConcurrentLinkedQueue conQueue = new ConcurrentLinkedQueue();  
  7.     private static LinkedBlockingQueue linkQueue = new LinkedBlockingQueue();  
  8.   
  9.     static class ConcurrentLinkedQueueProducer extends Test {  
  10.   
  11.         public ConcurrentLinkedQueueProducer(String id, CyclicBarrier barrier,  
  12.                 long count, int threadNum, ExecutorService executor) {  
  13.             super(id, barrier, count, threadNum, executor);  
  14.         }  
  15.   
  16.         @Override  
  17.         protected void test() {  
  18.             conQueue.add(1);  
  19.         }  
  20.     }  
  21.   
  22.     static class LinkedBlockingQueueProducer extends Test {  
  23.   
  24.         public LinkedBlockingQueueProducer(String id, CyclicBarrier barrier,  
  25.                 long count, int threadNum, ExecutorService executor) {  
  26.             super(id, barrier, count, threadNum, executor);  
  27.         }  
  28.   
  29.         @Override  
  30.         protected void test() {  
  31.             conQueue.add(1);  
  32.         }  
  33.     }  
  34.   
  35.     static class CyclicBarrierThread extends Thread {  
  36.         @Override  
  37.         public void run() {  
  38.             conQueue.clear();  
  39.             linkQueue.clear();  
  40.         }  
  41.     }  
  42.   
  43.     public static void test(String id, long count, int threadNum,  
  44.             ExecutorService executor) {  
  45.   
  46.         final CyclicBarrier barrier = new CyclicBarrier(threadNum + 1,  
  47.                 cyclicBarrierThread);  
  48.   
  49.         System.out.println("==============================");  
  50.         System.out.println("count = " + count + "/t" + "Thread Count = "  
  51.                 + threadNum);  
  52.   
  53.         concurrentTotalTime += new ConcurrentLinkedQueueProducer(  
  54.                 "ConcurrentLinkedQueueProducer", barrier, COUNT, threadNum,  
  55.                 executor).startTest();  
  56.         linkedBlockingTotalTime += new LinkedBlockingQueueProducer(  
  57.                 "LinkedBlockingQueueProducer ", barrier, COUNT, threadNum,  
  58.                 executor).startTest();  
  59.   
  60.         totalThreadCount += threadNum;  
  61.         executor.shutdownNow();  
  62.   
  63.         System.out.println("==============================");  
  64.     }  
  65.   
  66.     static long concurrentTotalTime = 0;  
  67.     static long linkedBlockingTotalTime = 0;  
  68.   
  69.     static long totalThreadCount = 0;  
  70.   
  71.     public static void main(String[] args) throws InterruptedException {  
  72.         for (int i = 1; i < 20; i++) {  
  73.             ExecutorService executor = Executors.newFixedThreadPool(THREAD_NUM  
  74.                     * i);  
  75.             test("", COUNT, 10 * i, executor);  
  76.         }  
  77.   
  78.         System.out.println("ConcurrentLinkedQueue Avg Time = "  
  79.                 + concurrentTotalTime / totalThreadCount);  
  80.   
  81.         System.out.println("LinkedBlockingQueue Avg Time = "  
  82.                 + linkedBlockingTotalTime / totalThreadCount);  
  83.     }  
  84. }  

 

结果,执行100,000次的并发入队操作,并发队列需要49毫秒,阻塞队列需要53毫秒,比阻塞队列平均快4毫秒。这种平均值的算法可能不准确,因为随着线程数量的增加,线程之间切换的开销会逐渐增大。 

  1. ==============================  
  2. count = 100000  Thread Count = 10  
  3. ConcurrentLinkedQueueProducer = 422  
  4. LinkedBlockingQueueProducer  = 390  
  5. ==============================  
  6. ==============================  
  7. count = 100000  Thread Count = 20  
  8. ConcurrentLinkedQueueProducer = 782  
  9. LinkedBlockingQueueProducer  = 828  
  10. ==============================  
  11. ==============================  
  12. count = 100000  Thread Count = 30  
  13. ConcurrentLinkedQueueProducer = 1468  
  14. LinkedBlockingQueueProducer  = 1422  
  15. ==============================  
  16. ==============================  
  17. count = 100000  Thread Count = 40  
  18. ConcurrentLinkedQueueProducer = 2188  
  19. LinkedBlockingQueueProducer  = 1297  
  20. ==============================  
  21. ==============================  
  22. count = 100000  Thread Count = 50  
  23. ConcurrentLinkedQueueProducer = 2609  
  24. LinkedBlockingQueueProducer  = 2625  
  25. ==============================  
  26. ==============================  
  27. count = 100000  Thread Count = 60  
  28. ConcurrentLinkedQueueProducer = 3078  
  29. LinkedBlockingQueueProducer  = 2969  
  30. ==============================  
  31. ==============================  
  32. count = 100000  Thread Count = 70  
  33. ConcurrentLinkedQueueProducer = 4046  
  34. LinkedBlockingQueueProducer  = 3594  
  35. ==============================  
  36. ==============================  
  37. count = 100000  Thread Count = 80  
  38. ConcurrentLinkedQueueProducer = 4047  
  39. LinkedBlockingQueueProducer  = 4078  
  40. ==============================  
  41. ==============================  
  42. count = 100000  Thread Count = 90  
  43. ConcurrentLinkedQueueProducer = 4469  
  44. LinkedBlockingQueueProducer  = 5109  
  45. ==============================  
  46. ==============================  
  47. count = 100000  Thread Count = 100  
  48. ConcurrentLinkedQueueProducer = 5125  
  49. LinkedBlockingQueueProducer  = 4922  
  50. ==============================  
  51. ==============================  
  52. count = 100000  Thread Count = 110  
  53. ConcurrentLinkedQueueProducer = 5531  
  54. LinkedBlockingQueueProducer  = 5969  
  55. ==============================  
  56. ==============================  
  57. count = 100000  Thread Count = 120  
  58. ConcurrentLinkedQueueProducer = 5172  
  59. LinkedBlockingQueueProducer  = 6188  
  60. ==============================  
  61. ==============================  
  62. count = 100000  Thread Count = 130  
  63. ConcurrentLinkedQueueProducer = 7187  
  64. LinkedBlockingQueueProducer  = 6594  
  65. ==============================  
  66. ==============================  
  67. count = 100000  Thread Count = 140  
  68. ConcurrentLinkedQueueProducer = 6937  
  69. LinkedBlockingQueueProducer  = 7782  
  70. ==============================  
  71. ==============================  
  72. count = 100000  Thread Count = 150  
  73. ConcurrentLinkedQueueProducer = 6921  
  74. LinkedBlockingQueueProducer  = 8875  
  75. ==============================  
  76. ==============================  
  77. count = 100000  Thread Count = 160  
  78. ConcurrentLinkedQueueProducer = 8047  
  79. LinkedBlockingQueueProducer  = 9250  
  80. ==============================  
  81. ==============================  
  82. count = 100000  Thread Count = 170  
  83. ConcurrentLinkedQueueProducer = 8235  
  84. LinkedBlockingQueueProducer  = 9656  
  85. ==============================  
  86. ==============================  
  87. count = 100000  Thread Count = 180  
  88. ConcurrentLinkedQueueProducer = 9062  
  89. LinkedBlockingQueueProducer  = 9719  
  90. ==============================  
  91. ==============================  
  92. count = 100000  Thread Count = 190  
  93. ConcurrentLinkedQueueProducer = 9422  
  94. LinkedBlockingQueueProducer  = 9609  
  95. ==============================  
  96. ConcurrentLinkedQueue Avg Time = 49  
  97. LinkedBlockingQueue Avg Time = 53  

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值