I heard that JDK6 has optimized the performance of SynchronousQueue to avoid locking competitive resources. So I want to try whether to choose SynchronousQueue or other BlockingQueue at ordinary times.
For the comparison of the container class in the concurrent environment, the first is whether the thread is secure, and the second is how the concurrency performance is. The implementation of BlockingQueue is thread-safe, so it can only compare with their concurrency performance. In different application scenarios, the usage of containers is different. Some read operations have fewer modification and write operations, while others have more modification and write operations, which may affect the performance of containers. However, for the use of Queue, I personally think it is relatively consistent. The simple point is put and get, and the content of an element will not be modified and then read, and few read operations are performed, is there any best practice?
The code is long. I will put it later and draw a conclusion first. Unexpectedly, the performance of javasblockingqueue is far higher than that of ArrayBlcokingQueue, regardless of the number of threads or the length of the Queue, which is better than that of ArrayBlockingQueue. SynchronousQueue is stable, and SynchronousQueue has the best performance regardless of the length of the Queue in 20 threads. In fact, SynchronousQueue has nothing to do with the length of the Queue. If the capacity of the Queue is only 1, so there is no doubt that SynchronousQueue is selected. This is also the purpose of designing SynchronousQueue. However, we can also see that when there are more than 1000 threads, The SynchronousQueue performance will go down, only about half of the peak, and when the Queue is greater than 30, the LinkedBlockingQueue performance will exceed SynchronousQueue.
Conclusion:
More threads> 20), length of the Queue> 30), use the LinkedBlockingQueue
Less threads (<20), shorter Queue length (<30), use SynchronousQueue
Of course, when using SynchronousQueue, do not forget the application extension. If you need to extend it in the future, choose LinkedBlockingQueue. Try to restrict SynchronousQueue to special scenarios.
Finally, let's take a look at the test code and results: Win7 64bit + JDK7 + CPU4 + 4 GB)
import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.Callable;import java.util.concurrent.CompletionService;import java.util.concurrent.ExecutorCompletionService;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.SynchronousQueue;public class TestSynchronousQueue { private static int THREAD_NUM; private static int N = 1000000; private static ExecutorService executor; public static void main(String[] args) throws Exception { System.out.println("Producer\tConsumer\tcapacity \t LinkedBlockingQueue \t ArrayBlockingQueue \t SynchronousQueue"); for(int j = 0; j<10; j++){ THREAD_NUM = (int) Math.pow(2, j); executor = Executors.newFixedThreadPool(THREAD_NUM * 2); for (int i = 0; i < 10; i++) { int length = (i == 0) ? 1 : i * 10; System.out.print(THREAD_NUM + "\t\t"); System.out.print(THREAD_NUM + "\t\t"); System.out.print(length + "\t\t"); System.out.print(doTest2(new LinkedBlockingQueue<Integer>(length), N) + "/s\t\t\t"); System.out.print(doTest2(new ArrayBlockingQueue<Integer>(length), N) + "/s\t\t\t"); System.out.print(doTest2(new SynchronousQueue<Integer>(), N) + "/s"); System.out.println(); } executor.shutdown(); } } private static class Producer implements Runnable{ int n; BlockingQueue<Integer> q; public Producer(int initN, BlockingQueue<Integer> initQ){ n = initN; q = initQ; } public void run() { for (int i = 0; i < n; i++) try { q.put(i); } catch (InterruptedException ex) { } } } private static class Consumer implements Callable<Long>{ int n; BlockingQueue<Integer> q; public Consumer(int initN, BlockingQueue<Integer> initQ){ n = initN; q = initQ; } public Long call() { long sum = 0; for (int i = 0; i < n; i++) try { sum += q.take(); } catch (InterruptedException ex) { } return sum; } } private static long doTest2(final BlockingQueue<Integer> q, final int n) throws Exception { CompletionService<Long> completionServ = new ExecutorCompletionService<Long>(executor); long t = System.nanoTime(); for(int i=0; i<THREAD_NUM; i++){ executor.submit(new Producer(n/THREAD_NUM, q)); } for(int i=0; i<THREAD_NUM; i++){ completionServ.submit(new Consumer(n/THREAD_NUM, q)); } for(int i=0; i<THREAD_NUM; i++){ completionServ.take().get(); } t = System.nanoTime() - t; return (long) (1000000000.0 * N / t); // Throughput, items/sec }}
Program running result:
Producer Consumer capacity LinkedBlockingQueue ArrayBlockingQueue SynchronousQueue1 1 1 154567/s 154100/s 3655071/s1 1 10 1833165/s 1967491/s 3622405/s1 1 20 3011779/s 2558451/s 3744037/s1 1 30 3145926/s 2632099/s 3354525/s1 1 40 3289673/s 2879696/s 3581858/s1 1 50 3201828/s 3008838/s 3600100/s1 1 60 3171374/s 2541672/s 3922617/s1 1 70 3159786/s 2844493/s 3423066/s1 1 80 3042835/s 2536290/s 3443517/s1 1 90 3025808/s 3026241/s 3307096/s2 2 1 141555/s 135653/s 2897927/s2 2 10 1627066/s 785082/s 2908671/s2 2 20 2199668/s 1604847/s 2937085/s2 2 30 2309495/s 2115986/s 2922671/s2 2 40 2335737/s 2424491/s 2942621/s2 2 50 2394045/s 2405210/s 2918222/s2 2 60 2499445/s 2471052/s 2881591/s2 2 70 2368143/s 2454153/s 2914038/s2 2 80 2381024/s 2457910/s 2937337/s2 2 90 2509167/s 2461035/s 2789278/s4 4 1 138177/s 138101/s 2736238/s4 4 10 1654165/s 478171/s 2693045/s4 4 20 2443373/s 779452/s 2728493/s4 4 30 2646300/s 1169313/s 2787315/s4 4 40 2755774/s 1487883/s 2874789/s4 4 50 2774736/s 1579152/s 2804046/s4 4 60 2804725/s 1998602/s 2803680/s4 4 70 2797524/s 2388276/s 2936613/s4 4 80 2887786/s 2557358/s 2899823/s4 4 90 2878895/s 2539458/s 2839990/s8 8 1 140745/s 135621/s 2711703/s8 8 10 1650143/s 526018/s 2730710/s8 8 20 2477902/s 798799/s 2696374/s8 8 30 2658511/s 983456/s 2783054/s8 8 40 2694167/s 1185732/s 2677500/s8 8 50 2758267/s 1110716/s 2766695/s8 8 60 2831922/s 1003692/s 2762232/s8 8 70 2763751/s 1409142/s 2791901/s8 8 80 2771897/s 1654843/s 2838479/s8 8 90 2740467/s 1718642/s 2806164/s16 16 1 131843/s 137943/s 2694036/s16 16 10 1637213/s 491171/s 2725893/s16 16 20 2523193/s 660559/s 2709892/s16 16 30 2601176/s 899163/s 2689270/s16 16 40 2794088/s 1054763/s 2759321/s16 16 50 2777807/s 1111479/s 2663346/s16 16 60 2893566/s 931713/s 2778294/s16 16 70 2822779/s 1286067/s 2704785/s16 16 80 2828238/s 1430581/s 2724927/s16 16 90 2860943/s 1249650/s 2791520/s32 32 1 132098/s 130805/s 2676121/s32 32 10 1586372/s 402270/s 2674953/s32 32 20 2467754/s 886059/s 2580989/s32 32 30 2569709/s 772173/s 2599466/s32 32 40 2659883/s 963633/s 2677042/s32 32 50 2721213/s 910607/s 2677578/s32 32 60 2779272/s 861786/s 2676874/s32 32 70 2757921/s 1111937/s 2696416/s32 32 80 2915294/s 1323776/s 2655641/s32 32 90 2798313/s 1193225/s 2630231/s64 64 1 126035/s 123764/s 2526632/s64 64 10 1539034/s 394597/s 2582590/s64 64 20 2449850/s 703790/s 2598631/s64 64 30 2672792/s 758256/s 2529693/s64 64 40 2797081/s 661028/s 2573380/s64 64 50 2789848/s 1162143/s 2659469/s64 64 60 2726806/s 1145495/s 2567020/s64 64 70 2731554/s 1359939/s 2607615/s64 64 80 2871116/s 1305428/s 2494839/s64 64 90 2774416/s 1339611/s 2560153/s128 128 1 223305/s 112828/s 2390234/s128 128 10 1419592/s 404611/s 2401086/s128 128 20 2365301/s 793815/s 2516045/s128 128 30 2647136/s 915702/s 2463175/s128 128 40 2721664/s 1081728/s 2400299/s128 128 50 2688304/s 1149251/s 2489667/s128 128 60 2774212/s 1145298/s 2453444/s128 128 70 2782905/s 1165408/s 2403510/s128 128 80 2818388/s 1392486/s 2389275/s128 128 90 2738468/s 1546247/s 2425994/s256 256 1 160146/s 80530/s 2369297/s256 256 10 1214041/s 364460/s 2142039/s256 256 20 1915432/s 901668/s 2156774/s256 256 30 2371862/s 1124997/s 2237464/s256 256 40 2630812/s 1123016/s 2216475/s256 256 50 2666827/s 1239640/s 2267322/s256 256 60 2635269/s 1276851/s 2318122/s256 256 70 2663477/s 1333002/s 2188256/s256 256 80 2672080/s 1659850/s 2315438/s256 256 90 2804828/s 1497635/s 2194905/s512 512 1 123294/s 68426/s 1892168/s512 512 10 1028250/s 296454/s 1728199/s512 512 20 1545215/s 604512/s 1963526/s512 512 30 1968728/s 762240/s 2000386/s512 512 40 2273678/s 854483/s 1948188/s512 512 50 2295335/s 939350/s 1858429/s512 512 60 2419257/s 1056918/s 1884224/s512 512 70 2346088/s 980795/s 1852387/s512 512 80 2341964/s 928496/s 1867498/s512 512 90 2375789/s 1290064/s 1923461/s
This article is from "the power comes from the sincere love !" Blog, please be sure to keep this source http://stevex.blog.51cto.com/4300375/1287085