51,397
社区成员




public static void main(String[] args) throws InterruptedException {
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.put("key", 1);
ExecutorService executorService = Executors.newFixedThreadPool(100);
// CountDownLatch countDownLatch = new CountDownLatch(100);
for (int i = 0; i < 1000; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
int key = map.get("key") + 1;
map.put("key", key);
System.out.println(key);
}
});
}
Thread.sleep(3000); //模拟等待执行结束
System.out.println("------" + map.get("key") + "------");
executorService.shutdown();
}
public static void main(String[] args) throws InterruptedException {
ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<>();
AtomicInteger value = new AtomicInteger(1);
map.put("key", value);
System.out.println("------" + map.get("key") + "------");
ExecutorService executorService = Executors.newFixedThreadPool(1000);
for (int i1 = 0; i1 < 100; i1++) {
for (int i = 0; i < 1000; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
map.get("key").incrementAndGet();
}
});
}
Thread.sleep(3000); //模拟等待执行结束
// Thread.yield();
System.out.println("------" + map.get("key") + "------");
}
executorService.shutdown();
}
public static void main(String[] args) throws InterruptedException {
ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<>();
AtomicInteger value = new AtomicInteger(1);
map.put("key", value);
System.out.println("------" + map.get("key") + "------");
ExecutorService executorService = Executors.newFixedThreadPool(1000);
for (int i1 = 0; i1 < 10; i1++) {
for (int i = 0; i < 1000; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
value.set(map.get("key").incrementAndGet());
// map.put("key",value);
// System.out.println(key);
}
});
}
Thread.sleep(3000); //模拟等待执行结束
// Thread.yield();
System.out.println("------" + map.get("key") + "------");
}
executorService.shutdown();
}
public static void main(String[] args) throws InterruptedException {
ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<>();
map.put("key", new AtomicInteger(1));
ExecutorService executorService = Executors.newFixedThreadPool(100);
for (int i = 0; i < 1000; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
int key = map.get("key").incrementAndGet();
map.put("key", new AtomicInteger(key));
System.out.println(key);
}
});
}
Thread.sleep(3000); //模拟等待执行结束
// Thread.yield();
System.out.println("------" + map.get("key") + "------");
executorService.shutdown();
}