The article directories

  • Introduction of the three
  • HashMap
  • HashTable
  • ConcurrentHashMap

Introduction of the three

What is the difference between a HashMap and a ConcurrentHashMap? HashMap is thread unsafe, and when you have multithreaded operations, there’s a security risk. We might think of HashTable, and yes, this is thread safe, but HashTable uses a method lock, locks the entire PUT method, which is inefficient, If the put method is likened to a courtyard with many rooms, the HathTable lock is equivalent to locking the courtyard door. ConcurrentHashMap, on the other hand, uses a block lock, which locks a potentially unsafe room in the yard so that people who go to other rooms don’t have to wait.

HashMap

HashMap is thread safe, to put in the source code method to do the processing of lock, when released a multi-threaded, there will be a thread safety problem, through a simple example to demonstrate below, to create three threads, and started, in the run method through the for loop to map 100 values, then output the size of the map as normal, The map should have a size of 100, but here 176 is printed.

class Demo implements Runnable{ static Map<String,String> map = new HashMap<>(); @Override public void run() { for (int i = 0; i < 100; i ++) { map.put(i + "","value"); } } public static void main(String[] args) { new Thread(new Demo()).start(); new Thread(new Demo()).start(); new Thread(new Demo()).start(); CurrentThread = thread.currentThread (); Currentthread.sleep (2000); currentThread.sleep(2000); } catch (Exception e) { e.getMessage(); } system.out.println (map.size()); }}Copy the code

HashTable

Put () {put () {put () {put () {put () {put () {put () {put ();

class Demo implements Runnable{ static Map<String,String> map = new Hashtable<>(); @Override public void run() { long startTime = System.currentTimeMillis(); For (int I = 0; i < 10000; i ++) { map.put(i + "","value"); } long endTime = System.currentTimeMillis(); System.out.println((endtime-startTime) + "ms"); } public static void main(String[] args) { new Thread(new Demo()).start(); new Thread(new Demo()).start(); new Thread(new Demo()).start(); CurrentThread = thread.currentThread (); Currentthread.sleep (2000); currentThread.sleep(2000); } catch (Exception e) { e.getMessage(); } system.out.println (map.size()); }}Copy the code

ConcurrentHashMap

ConcurrentHashMap uses a block lock, which is not secure to lock which block, can not lock, can not lock all, then I block lock! See if the block lock is faster or slower than the method lock.

class Demo implements Runnable{ static Map<String,String> map = new ConcurrentHashMap<>(); @Override public void run() { long startTime = System.currentTimeMillis(); For (int I = 0; i < 10000; i ++) { map.put(i + "","value"); } long endTime = System.currentTimeMillis(); System.out.println((endtime-startTime) + "ms"); } public static void main(String[] args) { new Thread(new Demo()).start(); new Thread(new Demo()).start(); new Thread(new Demo()).start(); CurrentThread = thread.currentThread (); Currentthread.sleep (2000); currentThread.sleep(2000); } catch (Exception e) { e.getMessage(); } system.out.println (map.size()); }}Copy the code



As you can see from the results, we have improved from 20ms and 22ms to 17ms and 18ms now