Java多线程之详解ThreadLocal类(一)

ThreadLocal类是用来创建和管理线程的本地存储的类。线程的本地存储可以为相同变量的每个不同线程都创建不同的存储,根除了线程对变量的共享,从而防止多线程任务在共享资源上发生冲突。我们先看下列代码:

public class ThreadLocalTest {
    private static ThreadLocal<Integer> value = new ThreadLocal<Integer>(){
        private Random rand = new Random(47);
        protected synchronized Integer initialValue(){
            return rand.nextInt(10000);
        }
    };
    public static void increment(){
        value.set(value.get()+1);
    }
    public static int get(){
        return value.get();
    }
    public static void main(String[] args) throws InterruptedException {
        ExecutorService exe= Executors.newCachedThreadPool();
        for(int i = 0;i < 5; i++){
            exe.execute(new Accessor(i));
        }
        TimeUnit.SECONDS.sleep(1);
        exe.shutdown();
    }

}
class Accessor implements Runnable{
    private final int id;
    public Accessor(int idn){
        this.id = idn;
    }
    @Override
    public void run() {
        while(!Thread.currentThread().isInterrupted()){
            ThreadLocalTest.increment();
            System.out.println(this);
            Thread.yield();
        }
    }
    public String toString(){
        return "#"+id+":"+ThreadLocalTest.get();
    }
}
我们在构造ThreadLocal的时候重写了它的initialValue方法,这个方法是ThreadLocal的初始化方法,线程在调用get方法时如果发现得到的值为null则会调用本方法,通过查阅jdk我们可以发现,ThreadLocal类中只有initialValue,get,set和remove方法,这几个方法我们看名字就能够知道它们的作用。上述代码的执行结果如下图所示:

#3:9
#1:6
#0:4
#2:2
#4:2
#0:5
#2:3
#4:3
#0:6
#2:4

为了方便区分,我们给了每个线程一个final标识的id,通过查看结果我们发现结果确实和我们猜想的一样,每个线程只维护自有的属性。另外我在网上查找资料时发现有些人说ThreadLocal不能使用原子类,特意去写代码试了试,代码如下:

 private static ThreadLocal<AtomicInteger> value = new ThreadLocal<AtomicInteger>(){
        private Random rand = new Random(47);
        protected synchronized AtomicInteger initialValue(){
            return new AtomicInteger(rand.nextInt(5));
        }
    };
    public static void increment(){
        AtomicInteger ai = value.get();
        int c = ai.get()+1;
        ai.set(c);
        value.set(ai);
    }
    public static int get(){
        return value.get().get();
    }
    public static void main(String[] args) throws InterruptedException {
        ExecutorService exe= Executors.newCachedThreadPool();
        for(int i = 0;i < 5; i++){
            exe.execute(new Accessors(i));
        }
        TimeUnit.SECONDS.sleep(1);
        exe.shutdown();
    }

}
class Accessors implements Runnable{
    private final int id;
    public Accessors(int idn){
        this.id = idn;
    }
    @Override
    public void run() {
        while(!Thread.currentThread().isInterrupted()){
            ThreadLocalTest.increment();
            System.out.println(this);
            Thread.yield();
        }
    }
    public String toString(){
        return "#"+id+":"+ThreadLocalTest.get();
    }
}
运行结果如下:

#3:6
#1:10
#3:7
#1:11
#3:8
#1:12
#3:9
#1:13
#3:10

事实证明,ThreadLocal是可以使用原子类的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值