累加 map java_Java Map取值累加的线程安全问题

昨天在开发者头条上面看的一篇文章针对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{

HashMapmap = new HashMap();

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{

Hashtablemap = new Hashtable();

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{

ConcurrentHashMapmap = new ConcurrentHashMap();

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{

ConcurrentHashMapmap = new ConcurrentHashMap();

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------

这个核心思想就是线程安全的原子操作一定是线程安全的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值