昨天在开发者头条上面看的一篇文章针对Map相关的线程安全讲解说的很好,今天根据思路还原了场景(隔壁老王半夜为何尖叫?这例子说的有点让老王很忙)。
Java代码:
package com.boonya.concurrent;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author PJL
*
* @note 功能描述:Add值的多线程安全问题--最优解方式是ConcurrentHashMap+Atomic*级别的原子操作
* @package com.boonya.concurrent
* @filename AddConcurrent.java
* @date 2019年4月23日 下午1:36:42
*/
public class AddConcurrent {
/**
* HashMap非线程安全
* @throws InterruptedException
*/
public static void test0() throws InterruptedException{
HashMap<String, Integer> map = new HashMap<String,Integer>();
Integer integer = new Integer(1);
map.put("key", integer);
ExecutorService executorService = Executors.newFixedThreadPool(100);
for (int i = 0; i < 1000; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
map.put("key", map.get("key").intValue()+1) ;
}
});
}
Thread.sleep(3000); //模拟等待执行结束
System.out.println("test0()------" + map.get("key") + "------");
executorService.shutdown();
}
/**
* 严格线程安全的同步非原子操作--非线程安全
* @throws InterruptedException
*/
public static void test1() throws InterruptedException{
Hashtable<String, Integer> map = new Hashtable<String,Integer>();
Integer integer = new Integer(1);
map.put("key", integer);
ExecutorService executorService = Executors.newFixedThreadPool(100);
for (int i = 0; i < 1000; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
map.put("key", map.get("key").intValue()+1) ;
}
});
}
Thread.sleep(3000); //模拟等待执行结束
System.out.println("test1()------" + map.get("key") + "------");
executorService.shutdown();
}
/**
* 线程安全的非原子操作--非线程安全
* @throws InterruptedException
*/
public static void test2() throws InterruptedException{
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String,Integer>();
Integer integer = new Integer(1);
map.put("key", integer);
ExecutorService executorService = Executors.newFixedThreadPool(100);
for (int i = 0; i < 1000; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
map.put("key", map.get("key").intValue()+1) ;
}
});
}
Thread.sleep(3000); //模拟等待执行结束
System.out.println("test2()------" + map.get("key") + "------");
executorService.shutdown();
}
/**
* 线程安全的原子操作--线程安全
* @throws InterruptedException
*/
public static void test3() throws InterruptedException{
ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<String,AtomicInteger>();
AtomicInteger integer = new AtomicInteger(1);
map.put("key", integer);
ExecutorService executorService = Executors.newFixedThreadPool(100);
for (int i = 0; i < 1000; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
map.get("key").incrementAndGet();
}
});
}
Thread.sleep(3000); //模拟等待执行结束
System.out.println("test3()------" + map.get("key") + "------");
executorService.shutdown();
}
public static void main(String[] args) throws InterruptedException {
AddConcurrent.test0();
AddConcurrent.test1();
AddConcurrent.test2();
AddConcurrent.test3();
}
}
输出结果:
test0()------998------
test1()------998------
test2()------413------
test3()------1001------
这个核心思想就是线程安全的原子操作一定是线程安全的。