[size=12.0000pt]1 [size=12.0000pt]2 [size=12.0000pt]3 [size=12.0000pt]4 [size=12.0000pt]5 [size=12.0000pt]6 [size=12.0000pt]7 [size=12.0000pt]8 | transient[size=12.0000pt] Node<K,V>[] table; [size=12.0000pt] static[size=12.0000pt] class[size=12.0000pt] Node<K,V> implements[size=12.0000pt] Map.Entry<K,V> { final[size=12.0000pt] int[size=12.0000pt] hash; final[size=12.0000pt] K key; V value; Node<K,V> next; [size=12.0000pt]} |
[size=12.0000pt]1 [size=12.0000pt]2 [size=12.0000pt]3 [size=12.0000pt]4 [size=12.0000pt]5 [size=12.0000pt]6 [size=12.0000pt]7 [size=12.0000pt]8 | //Hashtable Map<String, String> hashtable = new[size=12.0000pt] Hashtable<>(); [size=12.0000pt] //synchronizedMap Map<String, String> synchronizedHashMap = Collections.synchronizedMap(new[size=12.0000pt] HashMap<String, String>()); [size=12.0000pt] //ConcurrentHashMap Map<String, String> concurrentHashMap = new[size=12.0000pt] ConcurrentHashMap<>(); |
[size=12.0000pt]1 [size=12.0000pt]2 [size=12.0000pt]3 [size=12.0000pt]4 [size=12.0000pt]5 [size=12.0000pt]6 | public[size=12.0000pt] synchronized[size=12.0000pt] V get(Object key) { // 省略实现 } public[size=12.0000pt] synchronized[size=12.0000pt] V put(K key, V value) { // 省略实现 } |
[size=12.0000pt]1 [size=12.0000pt]2 [size=12.0000pt]3 [size=12.0000pt]4 [size=12.0000pt]5 [size=12.0000pt]6 [size=12.0000pt]7 [size=12.0000pt]8 [size=12.0000pt]9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | // synchronizedMap方法 public[size=12.0000pt] static[size=12.0000pt] <K,V> Map<K,V> synchronizedMap(Map<K,V> m) { return[size=12.0000pt] new[size=12.0000pt] SynchronizedMap<>(m); } // SynchronizedMap类 private[size=12.0000pt] static[size=12.0000pt] class[size=12.0000pt] SynchronizedMap<K,V> implements[size=12.0000pt] Map<K,V>, Serializable { private[size=12.0000pt] static[size=12.0000pt] final[size=12.0000pt] long[size=12.0000pt] serialVersionUID = 1978198479659022715L; [size=12.0000pt] private[size=12.0000pt] final[size=12.0000pt] Map<K,V> m; // Backing Map final[size=12.0000pt] Object mutex; // Object on which to synchronize [size=12.0000pt] SynchronizedMap(Map<K,V> m) { this.m = Objects.requireNonNull(m); mutex = this; } [size=12.0000pt] SynchronizedMap(Map<K,V> m, Object mutex) { this.m = m; this.mutex = mutex; } [size=12.0000pt] public[size=12.0000pt] int[size=12.0000pt] size() { synchronized[size=12.0000pt] (mutex) {return[size=12.0000pt] m.size();} } public[size=12.0000pt] boolean[size=12.0000pt] isEmpty() { synchronized[size=12.0000pt] (mutex) {return[size=12.0000pt] m.isEmpty();} } public[size=12.0000pt] boolean[size=12.0000pt] containsKey(Object key) { synchronized[size=12.0000pt] (mutex) {return[size=12.0000pt] m.containsKey(key);} } public[size=12.0000pt] boolean[size=12.0000pt] containsValue(Object value) { synchronized[size=12.0000pt] (mutex) {return[size=12.0000pt] m.containsValue(value);} } public[size=12.0000pt] V get(Object key) { synchronized[size=12.0000pt] (mutex) {return[size=12.0000pt] m.get(key);} } [size=12.0000pt] public[size=12.0000pt] V put(K key, V value) { synchronized[size=12.0000pt] (mutex) {return[size=12.0000pt] m.put(key, value);} } public[size=12.0000pt] V remove(Object key) { synchronized[size=12.0000pt] (mutex) {return[size=12.0000pt] m.remove(key);} } // 省略其他方法 } |
[size=12.0000pt]1 [size=12.0000pt]2 [size=12.0000pt]3 [size=12.0000pt]4 [size=12.0000pt]5 [size=12.0000pt]6 [size=12.0000pt]7 [size=12.0000pt]8 [size=12.0000pt]9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | public[size=12.0000pt] class[size=12.0000pt] CrunchifyConcurrentHashMapVsSynchronizedMap { [size=12.0000pt] public[size=12.0000pt] final[size=12.0000pt] static[size=12.0000pt] int[size=12.0000pt] THREAD_POOL_SIZE = 5; [size=12.0000pt] public[size=12.0000pt] static[size=12.0000pt] Map<String, Integer> crunchifyHashTableObject = null; public[size=12.0000pt] static[size=12.0000pt] Map<String, Integer> crunchifySynchronizedMapObject = null; public[size=12.0000pt] static[size=12.0000pt] Map<String, Integer> crunchifyConcurrentHashMapObject = null; [size=12.0000pt] public[size=12.0000pt] static[size=12.0000pt] void[size=12.0000pt] main(String[] args) throws[size=12.0000pt] InterruptedException { [size=12.0000pt] // Test with Hashtable Object crunchifyHashTableObject = new[size=12.0000pt] Hashtable<>(); crunchifyPerformTest(crunchifyHashTableObject); [size=12.0000pt] // Test with synchronizedMap Object crunchifySynchronizedMapObject = Collections.synchronizedMap(new[size=12.0000pt] HashMap<String, Integer>()); crunchifyPerformTest(crunchifySynchronizedMapObject); [size=12.0000pt] // Test with ConcurrentHashMap Object crunchifyConcurrentHashMapObject = new[size=12.0000pt] ConcurrentHashMap<>(); crunchifyPerformTest(crunchifyConcurrentHashMapObject); [size=12.0000pt] } [size=12.0000pt] public[size=12.0000pt] static[size=12.0000pt] void[size=12.0000pt] crunchifyPerformTest(final[size=12.0000pt] Map<String, Integer> crunchifyThreads) throws[size=12.0000pt] InterruptedException { [size=12.0000pt] System.out.println("Test started for: "[size=12.0000pt] + crunchifyThreads.getClass()); long[size=12.0000pt] averageTime = 0; for[size=12.0000pt] (int[size=12.0000pt] i = 0; i < 5; i++) { [size=12.0000pt] long[size=12.0000pt] startTime = System.nanoTime(); ExecutorService crunchifyExServer = Executors.newFixedThreadPool(THREAD_POOL_SIZE); [size=12.0000pt] for[size=12.0000pt] (int[size=12.0000pt] j = 0; j < THREAD_POOL_SIZE; j++) { crunchifyExServer.execute(new[size=12.0000pt] Runnable() { @SuppressWarnings("unused") @Override public[size=12.0000pt] void[size=12.0000pt] run() { [size=12.0000pt] for[size=12.0000pt] (int[size=12.0000pt] i = 0; i < 500000; i++) { Integer crunchifyRandomNumber = (int) Math.ceil(Math.random() * 550000); [size=12.0000pt] // Retrieve value. We are not using it anywhere Integer crunchifyValue = crunchifyThreads.get(String.valueOf(crunchifyRandomNumber)); [size=12.0000pt] // Put value crunchifyThreads.put(String.valueOf(crunchifyRandomNumber), crunchifyRandomNumber); } } }); } [size=12.0000pt] // Make sure executor stops crunchifyExServer.shutdown(); [size=12.0000pt] // Blocks until all tasks have completed execution after a shutdown request crunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); [size=12.0000pt] long[size=12.0000pt] entTime = System.nanoTime(); long[size=12.0000pt] totalTime = (entTime - startTime) / 1000000L; averageTime += totalTime; System.out.println("2500K entried added/retrieved in "[size=12.0000pt] + totalTime + " ms"); } System.out.println("For "[size=12.0000pt] + crunchifyThreads.getClass() + " the average time is "[size=12.0000pt] + averageTime / 5[size=12.0000pt] + " ms\n"); } [size=12.0000pt]} |
[size=12.0000pt]1 [size=12.0000pt]2 [size=12.0000pt]3 [size=12.0000pt]4 [size=12.0000pt]5 [size=12.0000pt]6 [size=12.0000pt]7 [size=12.0000pt]8 [size=12.0000pt]9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Test started for: class[size=12.0000pt] java.util.Hashtable 2500K entried added/retrieved in 2018[size=12.0000pt] ms 2500K entried added/retrieved in 1746[size=12.0000pt] ms 2500K entried added/retrieved in 1806[size=12.0000pt] ms 2500K entried added/retrieved in 1801[size=12.0000pt] ms 2500K entried added/retrieved in 1804[size=12.0000pt] ms For class[size=12.0000pt] java.util.Hashtable the average time is 1835[size=12.0000pt] ms [size=12.0000pt] Test started for: class[size=12.0000pt] java.util.Collections$SynchronizedMap 2500K entried added/retrieved in 3041[size=12.0000pt] ms 2500K entried added/retrieved in 1690[size=12.0000pt] ms 2500K entried added/retrieved in 1740[size=12.0000pt] ms 2500K entried added/retrieved in 1649[size=12.0000pt] ms 2500K entried added/retrieved in 1696[size=12.0000pt] ms For class[size=12.0000pt] java.util.Collections$SynchronizedMap the average time is 1963[size=12.0000pt] ms [size=12.0000pt] Test started for: class[size=12.0000pt] java.util.concurrent.ConcurrentHashMap 2500K entried added/retrieved in 738[size=12.0000pt] ms 2500K entried added/retrieved in 696[size=12.0000pt] ms 2500K entried added/retrieved in 548[size=12.0000pt] ms 2500K entried added/retrieved in 1447[size=12.0000pt] ms 2500K entried added/retrieved in 531[size=12.0000pt] ms For class[size=12.0000pt] java.util.concurrent.ConcurrentHashMap the average time is 792[size=12.0000pt] ms |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |