欢迎使用CSDN-markdown编辑器

ThreadLocal在并发中的使用

Thread类中有这么一句 ThreadLocal.ThreadLocalMap threadLocals = null;

ThreadLocal类中咋们知道他有两个方法:
get方法
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings(“unchecked”)
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
set方法
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}

而在并发编程中仅仅使用Thread是会出现问题的,多次执行如下代码:
package org.spring_test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;

public class AppTest implements Runnable{
// Runnable r1 = () -> { System.out.println(this); };
// 加了synchronized 的 Integer
final AtomicInteger number = new AtomicInteger();
volatile boolean bol = false;
//news是我自己随便建的
news n ;
ThreadLocal count = new ThreadLocal();
@Override
public void run() {
//news是我自己随便建的
n = new news();
// setcount();
// System.out.println(Thread.currentThread().getName()+” : “+count.get());
System.out.println(Thread.currentThread().getName()+” : “+n);
synchronized (this) {
try {
if (!bol) {
bol = true;
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(“并发数量为” + number.intValue());
}
}
@Test
public void test() {
//线程池模拟高并发
ExecutorService pool = Executors. newCachedThreadPool();
AppTest test = new AppTest( );
for (int i=0;i<10;i++) {
pool.execute(test); Thread
}
}
public void setcount(){
count.set(n);
}

}
多次执行结果会出现如下结果:
pool-1-thread-2 : org.spring_test.news@497c84d9
pool-1-thread-1 : org.spring_test.news@497c84d9
pool-1-thread-3 : org.spring_test.news@6f0f4e10
pool-1-thread-4 : org.spring_test.news@324bc6ce
pool-1-thread-5 : org.spring_test.news@70a0b3f3
pool-1-thread-6 : org.spring_test.news@3ce56f17
pool-1-thread-7 : org.spring_test.news@508f404b
pool-1-thread-8 : org.spring_test.news@70170fb6
pool-1-thread-9 : org.spring_test.news@497a030d
pool-1-thread-10 : org.spring_test.news@734a16c4

注意看thread-1和thread-2出现了相同的引用,这个是我反复测试执行出的结果,多运行几次就出现了,这个问题怎么办?
上边代码改一下,调用threadlocal试试:
setcount();
System.out.println(Thread.currentThread().getName()+” : “+count.get());
//System.out.println(Thread.currentThread().getName()+” : “+n);

反复测试,未出现上边重复引用对象的情况,问题得到解决!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值