解决HashMap多线程并发下死循环的方法

import java.util.Collections;
public class CollectionsDemo {
   public static void main(String[] args) {
      // create map
      Map<String,String> map = new HashMap<String,String>();
      
      // populate the map
      map.put("1","a"); 
      map.put("2","b");
      map.put("3","c");
      
      // create a synchronized map
      Map<String,String> synmap = Collections.synchronizedMap(map);//当map需要get的时候加上Collections.synchronizedMap就可以了
     
      System.out.println("Synchronized map is :"+synmap);
   }
}

猜你喜欢

转载自blog.csdn.net/qq_40591332/article/details/81982712