import java.util.concurrent.ConcurrentHashMap; public class Main { public static void main(String[] args) { ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>(); for (int i = 0; i < 10; i++) { final int index = i; new Thread(() -> { for (int j = 0; j < 100; j++) { map.put("Key" + index, map.getOrDefault("Key" + index, 0) + 1); } }).start(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } for (String key : map.keySet()) { System.out.println(key + ": " + map.get(key)); } } }


上一篇:
下一篇:
切换中文