併發--性能調優(三) 比較各種Map實現

synchronizedHashMap VS ConcurrentHashMap

package com21併發1;

import 生成器.CountingGenerator;
import 生成器.MapData;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Panda on 2018/5/29.
 */
abstract class MapTest extends Tester<Map<Integer, Integer>> {
    MapTest(String testId, int nReaders, int nWriters) {
        super(testId, nReaders, nWriters);
    }

    class Reader extends TestTask {
        long result = 0;

        @Override
        void test() {
            for (long i = 0; i < testCycles; i++) {
                for (int index = 0; index < containerSize; index++) {
                    result += testContainer.get(index);
                }
            }
        }

        @Override
        void putResults() {
            readResult += result;
            readTime += duration;
        }
    }

    class Writer extends TestTask {
        @Override
        void test() {
            for (long i = 0; i < testCycles; i++) {
                for (int index = 0; index < containerSize; index++) {
                    testContainer.put(index, writeData[index]);
                }
            }
        }

        @Override
        void putResults() {
            writeTime += duration;
        }
    }

    @Override
    void startReaderAndWriters() {
        for (int i = 0; i < nReaders; i++) {
            executorService.execute(new Reader());
        }
        for (int i = 0; i < nWriters; i++) {
            executorService.execute(new Writer());
        }
    }
}


class SynchronizedHashMapTest extends MapTest {
    @Override
    Map<Integer, Integer> containerInitializer() {
        return Collections.synchronizedMap(new HashMap<Integer, Integer>(MapData.map(new CountingGenerator.Integer(),
                new CountingGenerator.Integer(), containerSize)));
    }

    SynchronizedHashMapTest(int nReaders, int nWriters) {
        super("Synched HahsMap", nReaders, nWriters);
    }
}

class ConcurrentHashMapTest extends MapTest {
    @Override
    Map<Integer, Integer> containerInitializer() {
        return new ConcurrentHashMap<>(MapData.map(new CountingGenerator.Integer(),
                new CountingGenerator.Integer(), containerSize));
    }

    ConcurrentHashMapTest(int nReaders, int nWriters) {
        super("ConcurrentHashMapTest ", nReaders, nWriters);
    }
}

public class MapComparisons {
    public static void main(String[] args) {
        Tester.initMain(args);
        new SynchronizedHashMapTest(10, 0);
        new SynchronizedHashMapTest(9,1);
        new SynchronizedHashMapTest(5,5);
        new ConcurrentHashMapTest(10,0);
        new ConcurrentHashMapTest(9,1);
        new ConcurrentHashMapTest(5,5);
        Tester.executorService.shutdown();

    }

    /**
     * Type                             Read time     Write time
     Synched HahsMap 10r 0w         12341840583              0
     Synched HahsMap 9r 1w          10314500367     1260798506
     readTime+writeTime=            11575298873
     Synched HahsMap 5r 5w           6107539625     6102716718
     readTime+writeTime=            12210256343
     ConcurrentHashMapTest  10r 0w     1121064547              0
     ConcurrentHashMapTest  9r 1w      737168508       90881077
     readTime+writeTime=              828049585
     ConcurrentHashMapTest  5r 5w      230827578     1251006123    
     readTime+writeTime=             1481833701
      //比CopyOnWriteArrayList寫入快,因爲使用了不同的技術,可以明顯最小化寫入所帶來的影響
     */
}
發佈了64 篇原創文章 · 獲贊 1 · 訪問量 6730
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章