public class ThreadLocalTrxt { static ThreadLocal<Object> x1 = new ThreadLocal<Object>(); static ThreadLocal<Object> x2 = new ThreadLocal<Object>(); static ThreadLocal<Object> x3 = new ThreadLocal<Object>(); static ThreadLocal<Object> x4 = new ThreadLocal<Object>(); static ThreadLocal<Object> x5 = new ThreadLocal<Object>(); public static void main(String[] args) { for (int i = 0; i < 2; i++) { new Thread(new Runnable() { @Override public void run() { for(int i = 0; i<5; i++) { x1.set(i); //把x1設置到這個線程的threadLocals裏面的table裏面的Entry裏面的referent裏面去,還有i=0 x2.set(i); x3.set(i); x4.set(i); x5.set(i); } Thread t = Thread.currentThread(); ThreadLocal<Object> x6 = new ThreadLocal<Object>(); x6.remove(); x1.remove(); x2.remove(); x3.remove(); x4.remove(); x5.remove(); } }).start(); } } }
public class Test { public static void main(String[] args) { AtomicInteger hashCode = new AtomicInteger(); int hash_increment = 0x61c88647; int size = 32; List <Integer> list = new ArrayList <> (); for (int i = 0; i < size; i++) { System.out.println(hashCode); hashCode.getAndAdd(hash_increment); System.out.println(hashCode); System.out.println(hashCode.intValue() & (size - 1)); list.add(hashCode.intValue() & (size - 1)); } System.out.println("original:" + list); Collections.sort(list); System.out.println("sort: " + list); System.out.println(hash_increment); } }
public class ThreadLocalTrxt { static ThreadLocal<Integer> x = new ThreadLocal<Integer>(); public static void main(String[] args) { for (int i = 0; i < 2; i++) { new Thread(new Runnable() { @Override public void run() { int s = new Random().nextInt(); x.set(s); // ThreadLocal 設置值的時候,自動跟線程關聯 System.out.println(x.get()); // ThreadLocal 取值的時候,自動跟線程關聯 } }).start(); } } }
public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); // 返回null if (map != null) map.set(this, value); else createMap(t, value); } threadLocals是ThreadLocalMap在線程Thread裏面 ThreadLocalMap getMap(Thread t) { return t.threadLocals; // 返回線程的threadLocals,是一個ThreadLocalMap類型,只不過這個類型在ThreadLocal裏面定義 }
void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); //this是ThreadLocal,調用x.set(s);的x } 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(); }
ThreadLocal設置值是設置到這個線程的threadLocals屬性裏面去了,而且ThreadLocal本身也在threadLocals裏面。多個線程共用這個ThreadLocal。安全
ThreadLocalMap的key是ThreadLocal,Value是set方法設置進來的值。
Entry是ThreadLocalMap的內部類,ThreadLocal和value2個屬性組成。dom
線程初始化時候:ide
public class ThreadLocalTrxt { static ThreadLocal<Object> x1 = new ThreadLocal<Object>(); static ThreadLocal<Object> x2 = new ThreadLocal<Object>(); static ThreadLocal<Object> x3 = new ThreadLocal<Object>(); static ThreadLocal<Object> x4 = new ThreadLocal<Object>(); static ThreadLocal<Object> x5 = new ThreadLocal<Object>(); static ThreadLocal<Object> x6 = new ThreadLocal<Object>(); public static void main(String[] args) { for (int i = 0; i < 2; i++) { new Thread(new Runnable() {//2個線程調用,則2個線程共享x1,x2,x3,x4,x5,x6。可是ThreadLocal裏面沒有屬性(只是充當一個key),2個線程修改的時候, //修改的是線程內部的threadLocals。Thread和ThreadLocal是在一個包下的。不一樣線程相同的key(ThreadLocal)獲得的能夠是不一樣的value。 @Override public void run() { for(int i = 0; i<5; i++) { x1.set(i); //把x1設置到這個線程的threadLocals裏面的table裏面的Entry裏面的referent裏面去,還有i=0 x2.set(i); // 重複調用會覆蓋 x3.set(i); x4.set(i); x5.set(i); x6.set(i); } Thread t = Thread.currentThread(); System.out.println(x1.get()); x1.remove(); } }).start(); } } }
ThreadLocal沒法解決線程安全問題。也就是說ThreadLocal是被各個線程共享的。 this