The test results show that the performance of blocking and concurrent queues is not much different in the case of high concurrency.
1 test. Java code reference http://blog.csdn.net/arkblue/archive/2011/01/14/6138598.aspx
View plain
- Public class concurrentqueuetest {
- Private Static int COUNT = 100000;
- Private Static int thread_num = 10;
- Private Static javasicbarrierthread = new javasicbarrierthread ();
- Private Static concurrent1_queue conqueue = new concurrent1_queue ();
- Private Static jsonblockingqueue linkqueue = new jsonblockingqueue ();
- 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 extends blockingqueueproducer extends test {
- Public writable blockingqueueproducer (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 extends icbarrierthread extends thread {
- @ Override
- Public void run (){
- Conqueue. Clear ();
- Linkqueue. Clear ();
- }
- }
- Public static void test (string ID, Long Count, int threadnum,
- Executorservice executor ){
- Final required icbarrier barrier = new required icbarrier (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 wait blockingtotaltime = 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 ("concurrent1_queue AVG time ="
- + Concurrenttotaltime/totalthreadcount );
- System. Out. println ("blocked blockingqueue AVG time ="
- + Define blockingtotaltime/totalthreadcount );
- }
- }
As a result, it takes 49 milliseconds to run 100,000 concurrent queue operations and 53 milliseconds to block the queue, which is 4 milliseconds faster than the blocking queue on average. The average value algorithm may be inaccurate because the overhead for switching between threads increases as the number of threads increases.
View plain
- ====================================
- Count = 100000 thread count = 10
- Concurrent1_queueproducer = 422
- Export blockingqueueproducer = 390
- ====================================
- ====================================
- Count = 100000 thread count = 20
- Concurrent1_queueproducer = 782
- Export blockingqueueproducer = 828
- ====================================
- ====================================
- Count = 100000 thread count = 30
- Concurrent1_queueproducer = 1468
- Export blockingqueueproducer = 1422
- ====================================
- ====================================
- Count = 100000 thread count = 40
- Concurrent1_queueproducer = 2188
- Export blockingqueueproducer = 1297
- ====================================
- ====================================
- Count = 100000 thread count = 50
- Concurrent1_queueproducer = 2609
- Export blockingqueueproducer = 2625
- ====================================
- ====================================
- Count = 100000 thread count = 60
- Concurrent1_queueproducer = 3078
- Export blockingqueueproducer = 2969
- ====================================
- ====================================
- Count = 100000 thread count = 70
- Concurrent1_queueproducer = 4046
- Export blockingqueueproducer = 3594
- ====================================
- ====================================
- Count = 100000 thread count = 80
- Concurrent1_queueproducer = 4047
- Export blockingqueueproducer = 4078
- ====================================
- ====================================
- Count = 100000 thread count = 90
- Concurrent1_queueproducer = 4469
- Export blockingqueueproducer = 5109
- ====================================
- ====================================
- Count = 100000 thread count = 100
- Concurrent1_queueproducer = 5125
- Export blockingqueueproducer = 4922
- ====================================
- ====================================
- Count = 100000 thread count = 110
- Concurrent1_queueproducer = 5531
- Export blockingqueueproducer = 5969
- ====================================
- ====================================
- Count = 100000 thread count = 120
- Concurrent1_queueproducer = 5172
- Export blockingqueueproducer = 6188
- ====================================
- ====================================
- Count = 100000 thread count = 130
- Concurrent1_queueproducer = 7187
- Export blockingqueueproducer = 6594
- ====================================
- ====================================
- Count = 100000 thread count = 140
- Concurrent1_queueproducer = 6937
- Export blockingqueueproducer = 7782
- ====================================
- ====================================
- Count = 100000 thread count = 150
- Concurrent1_queueproducer = 6921
- Export blockingqueueproducer = 8875
- ====================================
- ====================================
- Count = 100000 thread count = 160
- Concurrent1_queueproducer = 8047
- Export blockingqueueproducer = 9250
- ====================================
- ====================================
- Count = 100000 thread count = 170
- Concurrent1_queueproducer = 8235
- Export blockingqueueproducer = 9656
- ====================================
- ====================================
- Count = 100000 thread count = 180
- Concurrent1_queueproducer = 9062
- Export blockingqueueproducer = 9719
- ====================================
- ====================================
- Count = 100000 thread count = 190
- Concurrent1_queueproducer = 9422
- Export blockingqueueproducer = 9609
- ====================================
- Concurrent1_queue AVG time = 49
- Required blockingqueue AVG time = 53