java.lang
类 ThreadLocal<T>
java.lang.Object
java.lang.ThreadLocal<T>
直接已知子类:
InheritableThreadLocal
public class ThreadLocal<T>extends Object该类提供了线程局部 (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 实例是可访问的;
在线程消失之后,其线程局部实例的所有副本都会被垃圾回收(除非存在对这些副本的其他引用)。
Thread-0 has put data :1365978062
A form Thread-0 get data :1365978062
Thread-1 has put data :-770297261
A form Thread-1 get data :-770297261
B form Thread-1 get data :-770297261
B form Thread-0 get data :1365978062
如果 线程范围内 有 N个变量 那应该怎么办呢??一个 threadLocal代表一个变量,
故其中里只能放一个数据。你有两个变量都要线程范围内共享,则要定义两个threadLocal对象,如果有一百个变量要线程共享呢?那请先定义一个对象来装这一百个变量,然后在threadLocal中存储这个对象。
package com.itm.thread; import java.util.Random; /************ * * 利用 ThreadLocal 。 * * @author * */ public class ThreadLocalTest { private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();// 泛型。 private static ThreadLocal<MyThreadScopeData> myThreadScopeData = new ThreadLocal<MyThreadScopeData>(); public static void main(String[] args) { for(int i=0;i<2;i++){ // 用来创建两个线程。 new Thread( new Runnable(){ // 创建好一个线程。 @Override public void run() { int data = new Random().nextInt(); // 准备好数据。 System.out.println(Thread.currentThread().getName() + " has put data :" + data); x.set(data);// 向当前线程里面放入一个数据。把数据 存在当前线程里面去了。。。 /*********** * * 多个变量。 * * 存数据。 * */ MyThreadScopeData myData = new MyThreadScopeData(); // new 一个这样的对象。 myData.setName("name " + data); myData.setAge(data); myThreadScopeData.set(myData); // 存数据。。。 new A().get(); // A 去取数据。 new B().get(); } }).start(); } } static class A{ public void get(){ int data = x.get();// 取出 存入当前线程里面的数据。。。 System.out.println("A form " + Thread.currentThread().getName() + " get data :" + data); MyThreadScopeData myData = myThreadScopeData.get(); System.out.println("A form " + Thread.currentThread().getName() + " getMyData : " + myData.getName() + "," + myData.getAge()); } } static class B{ public void get(){ int data = x.get(); System.out.println("B form " + Thread.currentThread().getName() + " get data :" + data); } } } class MyThreadScopeData { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Thread-0 has put data :-485439611
Thread-1 has put data :198643933
A form Thread-0 get data :-485439611
A form Thread-0 getMyData : name -485439611,-485439611
A form Thread-1 get data :198643933
A form Thread-1 getMyData : name 198643933,198643933
B form Thread-1 get data :198643933
B form Thread-0 get data :-485439611
一中 更加优雅的写法:
package com.itm.thread; import java.util.Random; /************ * * 利用 ThreadLocal 。自己线程范围内的对象。 * * @author * */ public class CopyOfThreadLocalTest2 { private static ThreadLocal<Integer> x = new ThreadLocal<Integer>(); private static ThreadLocal<MyThreadScopeDataTwo> myThreadScopeDataTwo = new ThreadLocal<MyThreadScopeDataTwo>(); public static void main(String[] args) { for(int i=0;i<2;i++){ // 用来创建两个线程。 new Thread( new Runnable(){ // 创建好一个线程。 @Override public void run() { int data = new Random().nextInt(); // 准备好数据。 System.out.println(Thread.currentThread().getName() + " has put data :" + data); x.set(data);// 向当前线程里面放入一个数据。 /*MyThreadScopeDataTwo myData = new MyThreadScopeDataTwo(); myData.setName("name " + data); myData.setAge(data); myThreadScopeDataTwo.set(myData);*/ // MyThreadScopeDataTwo.getThreadInstance() 拿到与 本线程相关的实例。 MyThreadScopeDataTwo.getThreadInstance().setName("name " + data); MyThreadScopeDataTwo.getThreadInstance().setAge(data); new A().get(); // A 去取数据。 new B().get(); } }).start(); } } static class A{ public void get(){ int data = x.get(); System.out.println("A form " + Thread.currentThread().getName() + " get data :" + data); MyThreadScopeDataTwo myData = MyThreadScopeDataTwo.getThreadInstance(); System.out.println("A form " + Thread.currentThread().getName() + " getMyData : " + myData.getName() + "," + myData.getAge()); } } static class B{ public void get(){ int data = x.get(); System.out.println("B form " + Thread.currentThread().getName() + " get data :" + data); MyThreadScopeDataTwo myData = MyThreadScopeDataTwo.getThreadInstance(); System.out.println("A form " + Thread.currentThread().getName() + " getMyData : " + myData.getName() + "," + myData.getAge()); } } } class MyThreadScopeDataTwo { /* * 饱汉式。:人家还没有调用我 我就把这个对象创建出来了。 private MyThreadScopeDataTwo(){} public static MyThreadScopeDataTwo getInstance(){ return instance; } private static MyThreadScopeDataTwo instance = new MyThreadScopeDataTwo(); */ /* 饿汉式: private MyThreadScopeDataTwo(){} // 加上 synchronized 时,这个对象就只能创建一个了。 public static synchronized MyThreadScopeDataTwo getInstance(){ if(instance == null){ instance = new MyThreadScopeDataTwo(); } return instance; } private static MyThreadScopeDataTwo instance = null; */ private MyThreadScopeDataTwo(){} public static /*synchronized*/ MyThreadScopeDataTwo getThreadInstance(){ MyThreadScopeDataTwo instance = map.get(); if(instance == null){ // 不用加上 synchronized,两个线程各是各的实例。 instance = new MyThreadScopeDataTwo(); map.set(instance); // 把对象 放进去。。 } return instance; } // 用来专门装对象的。。。 private static ThreadLocal<MyThreadScopeDataTwo> map = new ThreadLocal<MyThreadScopeDataTwo>(); private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
更多 threadLocal 借鉴: