1. 程式人生 > >SynchronousQueue、LinkedBlockingQueue、ArrayBlockingQueue效能測試

SynchronousQueue、LinkedBlockingQueue、ArrayBlockingQueue效能測試

對於容器類在併發環境下的比較,一是是否執行緒安全,二是併發效能如何。BlockingQueue的實現都是執行緒安全的,所以只能比比它們的併發效能了。在不同的應用場景中,對容器的使用情況不同,有的讀取操作多修改寫入操作少,有的修改寫入操作多,這對容器的效能會造成不同的影響。但對於Queue的使用,個人認為是比較一致的,簡單點就是put和get,不會修改某個元素的內容再被讀取,也很少只讀取的操作,那是不是有最佳實踐了?

程式碼比較長,我還是放在後面,先說結論。沒有想到的是LinkedBlockingQueue效能表現遠超ArrayBlcokingQueue,不管執行緒多少,不管Queue長短,LinkedBlockingQueue都勝過ArrayBlockingQueue。SynchronousQueue表現很穩定,而且在20個執行緒之內不管Queue長短,SynchronousQueue效能表現是最好的,(其實SynchronousQueue跟Queue長短沒有關係),如果Queue的capability只能是1,那麼毫無疑問選擇SynchronousQueue,這也是設計SynchronousQueue的目的吧。但大家也可以看到當超過1000個執行緒時,SynchronousQueue效能就直線下降了,只有最高峰的一半左右,而且當Queue大於30時,LinkedBlockingQueue效能就超過SynchronousQueue。

結論:

  • 執行緒多(>20),Queue長度長(>30),使用LinkedBlockingQueue

  • 執行緒少 (<20) ,Queue長度短 (<30) , 使用SynchronousQueue

當然,使用SynchronousQueue的時候不要忘記應用的擴充套件,如果將來需要進行擴充套件還是選擇LinkedBlockingQueue好,儘量把SynchronousQueue限制在特殊場景中使用。

  • 少用ArrayBlcokingQueue,似乎沒找到它的好處,高手給給建議吧!

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
    }
}
程式執行結果:(Win7 64bit + JDK7 + CPU4 + 4GB)
Producer    Consumer    capacity     LinkedBlockingQueue     ArrayBlockingQueue      SynchronousQueue
1       1       1       154567/s            154100/s            3655071/s
1       1       10      1833165/s           1967491/s           3622405/s
1       1       20      3011779/s           2558451/s           3744037/s
1       1       30      3145926/s           2632099/s           3354525/s
1       1       40      3289673/s           2879696/s           3581858/s
1       1       50      3201828/s           3008838/s           3600100/s
1       1       60      3171374/s           2541672/s           3922617/s
1       1       70      3159786/s           2844493/s           3423066/s
1       1       80      3042835/s           2536290/s           3443517/s
1       1       90      3025808/s           3026241/s           3307096/s
2       2       1       141555/s            135653/s            2897927/s
2       2       10      1627066/s           785082/s            2908671/s
2       2       20      2199668/s           1604847/s           2937085/s
2       2       30      2309495/s           2115986/s           2922671/s
2       2       40      2335737/s           2424491/s           2942621/s
2       2       50      2394045/s           2405210/s           2918222/s
2       2       60      2499445/s           2471052/s           2881591/s
2       2       70      2368143/s           2454153/s           2914038/s
2       2       80      2381024/s           2457910/s           2937337/s
2       2       90      2509167/s           2461035/s           2789278/s
4       4       1       138177/s            138101/s            2736238/s
4       4       10      1654165/s           478171/s            2693045/s
4       4       20      2443373/s           779452/s            2728493/s
4       4       30      2646300/s           1169313/s           2787315/s
4       4       40      2755774/s           1487883/s           2874789/s
4       4       50      2774736/s           1579152/s           2804046/s
4       4       60      2804725/s           1998602/s           2803680/s
4       4       70      2797524/s           2388276/s           2936613/s
4       4       80      2887786/s           2557358/s           2899823/s
4       4       90      2878895/s           2539458/s           2839990/s
8       8       1       140745/s            135621/s            2711703/s
8       8       10      1650143/s           526018/s            2730710/s
8       8       20      2477902/s           798799/s            2696374/s
8       8       30      2658511/s           983456/s            2783054/s
8       8       40      2694167/s           1185732/s           2677500/s
8       8       50      2758267/s           1110716/s           2766695/s
8       8       60      2831922/s           1003692/s           2762232/s
8       8       70      2763751/s           1409142/s           2791901/s
8       8       80      2771897/s           1654843/s           2838479/s
8       8       90      2740467/s           1718642/s           2806164/s
16      16      1       131843/s            137943/s            2694036/s
16      16      10      1637213/s           491171/s            2725893/s
16      16      20      2523193/s           660559/s            2709892/s
16      16      30      2601176/s           899163/s            2689270/s
16      16      40      2794088/s           1054763/s           2759321/s
16      16      50      2777807/s           1111479/s           2663346/s
16      16      60      2893566/s           931713/s            2778294/s
16      16      70      2822779/s           1286067/s           2704785/s
16      16      80      2828238/s           1430581/s           2724927/s
16      16      90      2860943/s           1249650/s           2791520/s
32      32      1       132098/s            130805/s            2676121/s
32      32      10      1586372/s           402270/s            2674953/s
32      32      20      2467754/s           886059/s            2580989/s
32      32      30      2569709/s           772173/s            2599466/s
32      32      40      2659883/s           963633/s            2677042/s
32      32      50      2721213/s           910607/s            2677578/s
32      32      60      2779272/s           861786/s            2676874/s
32      32      70      2757921/s           1111937/s           2696416/s
32      32      80      2915294/s           1323776/s           2655641/s
32      32      90      2798313/s           1193225/s           2630231/s
64      64      1       126035/s            123764/s            2526632/s
64      64      10      1539034/s           394597/s            2582590/s
64      64      20      2449850/s           703790/s            2598631/s
64      64      30      2672792/s           758256/s            2529693/s
64      64      40      2797081/s           661028/s            2573380/s
64      64      50      2789848/s           1162143/s           2659469/s
64      64      60      2726806/s           1145495/s           2567020/s
64      64      70      2731554/s           1359939/s           2607615/s
64      64      80      2871116/s           1305428/s           2494839/s
64      64      90      2774416/s           1339611/s           2560153/s
128     128     1       223305/s            112828/s            2390234/s
128     128     10      1419592/s           404611/s            2401086/s
128     128     20      2365301/s           793815/s            2516045/s
128     128     30      2647136/s           915702/s            2463175/s
128     128     40      2721664/s           1081728/s           2400299/s
128     128     50      2688304/s           1149251/s           2489667/s
128     128     60      2774212/s           1145298/s           2453444/s
128     128     70      2782905/s           1165408/s           2403510/s
128     128     80      2818388/s           1392486/s           2389275/s
128     128     90      2738468/s           1546247/s           2425994/s
256     256     1       160146/s            80530/s         2369297/s
256     256     10      1214041/s           364460/s            2142039/s
256     256     20      1915432/s           901668/s            2156774/s
256     256     30      2371862/s           1124997/s           2237464/s
256     256     40      2630812/s           1123016/s           2216475/s
256     256     50      2666827/s           1239640/s           2267322/s
256     256     60      2635269/s           1276851/s           2318122/s
256     256     70      2663477/s           1333002/s           2188256/s
256     256     80      2672080/s           1659850/s           2315438/s
256     256     90      2804828/s           1497635/s           2194905/s
512     512     1       123294/s            68426/s         1892168/s
512     512     10      1028250/s           296454/s            1728199/s
512     512     20      1545215/s           604512/s            1963526/s
512     512     30      1968728/s           762240/s            2000386/s
512     512     40      2273678/s           854483/s            1948188/s
512     512     50      2295335/s           939350/s            1858429/s
512     512     60      2419257/s           1056918/s           1884224/s
512     512     70      2346088/s           980795/s            1852387/s
512     512     80      2341964/s           928496/s            1867498/s
512     512     90      2375789/s           1290064/s           1923461/s