ThreadLocal的大致作用是:让每个线程都有一个变量的副本。然后线程运行的时候只会使用属于该线程的副本,而不会去动那个原始变量。
ThreadLocal代码原理还没看
每个线程调用全局ThreadLocal对象的set方法,就相当于往其内部的map中增加一条记录,key分别是各自的线程,value是各自的set方法传进去的值,在线程结束的收可以调用ThreadLocal.clear()方法,这样可以更快的释放内存,不调用也可以,因为线程结束的的时候会自动释放相关的ThreadLocal变量。但是要注意如果使用的是线程池的话,用完之后一定要ThreadLocal.clear()不然的话可能内存会出问题。
结合单例的懒汉模式和ThreadLocal将一个线程所需要的多个变量封装成一个类,通过这种模式,每个线程可以得到一个只属于自己的多变量的类,线程之间互不干扰,优点是,在使用类的时候之间调用方法,不用考虑内部结构。
package Thread;
import java.util.Random;
public class ThreadLocalSingleInstanceTest {
public static void main(String[] args) {
for (int i = 0;i<3;i++) {
new Thread(new Runnable() {
@Override
public void run() {
int year = new Random().nextInt(100) + 1901;
int month = new Random().nextInt(12) + 1;
int day = new Random().nextInt(30) + 1;
ThreadLocalSingleInstance.getInstance().setYear(year);
ThreadLocalSingleInstance.getInstance().setMonth(month);
ThreadLocalSingleInstance.getInstance().setDay(day);
System.out.println(year+"年"+month+"月"+day+"日,创建:"+Thread.currentThread().getName());
new A().get();
new B().get();
}
}).start();
}
}
//主函数是静态方法,需要调用类A和B中的方法,那么类A,B中的方法也必须是静态的,,有静态方法,那么类也必须是静态的。
static class A{
public ThreadLocalSingleInstance ta = ThreadLocalSingleInstance.getInstance();
public void get() {
System.out.println(ta.getYear()+"年"+ta.getMonth()+"月"+ta.getDay()+"日,创建:The mother of A is "+
Thread.currentThread().getName());
}
}
static class B{
public ThreadLocalSingleInstance tb = ThreadLocalSingleInstance.getInstance();
public void get() {
System.out.println(tb.getYear()+"年"+tb.getMonth()+"月"+tb.getDay()+"日,创建:The mother of B is "+
Thread.currentThread().getName());
}
}
}
class ThreadLocalSingleInstance{
private int year;
private int month;
private int day;
private static ThreadLocal<ThreadLocalSingleInstance> tLocal = new ThreadLocal<ThreadLocalSingleInstance>();
public static ThreadLocalSingleInstance getInstance() {
ThreadLocalSingleInstance instance = tLocal.get();
if(instance == null)
{
instance = new ThreadLocalSingleInstance();
tLocal.set(instance);
}
return instance;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public void setYear(int year) {
this.year = year;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}