线程封闭
避免使用同步的方式就是不共享数据,它是实现线程安全的最简单方式之一,当某对象被封闭在一个线程中时,将自动实现线程安全性;局部变量能达到这个效果,但更规范的做法是ThreadLocal
ThreadLocal提供了get、set方法,为每个使用该方法的线程都存一份独立的副本,get总是返回当前线程中set设置的最新值。
方法摘要
ThreadLocal()
创建一个线程本地变量。
方法摘要
T get()
返回此线程局部变量的当前线程副本中的值。
protected T initialValue()
返回此线程局部变量的当前线程的“初始值”。
void remove()
移除此线程局部变量当前线程的值。
void set(T value)
将此线程局部变量的当前线程副本中的值设置为指定值。
使用方式
该类提供了线程局部 (thread-local) 变量。这些变量不同于它们的普通对应物,因为访问某个变量(通过其 get 或 set 方法)的每个线程都有自己的局部变量,它独立于变量的初始化副本。ThreadLocal 实例通常是类中的 private static 字段,它们希望将状态与某一个线程(例如,用户 ID 或事务 ID)相关联。
例如,以下类生成对每个线程唯一的局部标识符。 线程 ID 是在第一次调用 UniqueThreadIdGenerator.getCurrentThreadId() 时分配的,在后续调用中不会更改。
import java.util.concurrent.atomic.AtomicInteger;
public class UniqueThreadIdGenerator {
private static final AtomicInteger uniqueId = new AtomicInteger(0);
private static final ThreadLocal < Integer > uniqueNum =
new ThreadLocal < Integer > () {
@Override protected Integer initialValue() {
return uniqueId.getAndIncrement();
}
};
public static int getCurrentThreadId() {
return uniqueId.get();
}
} // UniqueThreadIdGenerator
每个线程都保持对其线程局部变量副本的隐式引用,只要线程是活动的并且 ThreadLocal 实例是可访问的;在线程消失之后,其线程局部实例的所有副本都会被垃圾回收(除非存在对这些副本的其他引用)。
举个栗子
public class Test2 implements Runnable {
public static final ThreadLocal<Integer> local=new ThreadLocal<Integer>(){
protected Integer initialValue() {
return 100;
}
};
public void run() {
System.out.println(Thread.currentThread().getName()+"==="+local.get());
local.set(local.get()+10);
System.out.println(Thread.currentThread().getName()+"==="+local.get());
}
public static void main(String[] args) {
new Thread(new Test2()).start();
new Thread(new Test2()).start();
new Thread(new Test2()).start();
}
}
本文介绍了线程封闭的概念及其实现线程安全的方式之一——ThreadLocal。详细解释了ThreadLocal提供的get、set等方法,并通过示例展示了如何利用ThreadLocal为每个线程提供独立的变量副本。
1359

被折叠的 条评论
为什么被折叠?



