The Thread-Specific Storage 线程保险箱
官方解释
ThreadLocal的初始化设定值和set设置值
package com.dwz.concurrency.chapter33; public class ThreadLocalSimpleTest { private static ThreadLocal<String> threadlocal = new ThreadLocal<String>() { @Override protected String initialValue() { return "dandan"; }; }; public static void main(String[] args) throws InterruptedException { threadlocal.set("Alex"); Thread.sleep(1000); System.out.println(threadlocal.get()); } }
模拟ThreadLocal的独立性
package com.dwz.concurrency.chapter33; import java.util.Random; public class ThreadLocalComplexTest { private final static ThreadLocal<String> threadlocal = new ThreadLocal<>(); private final static Random random = new Random(System.currentTimeMillis()); public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(()->{ threadlocal.set("T1"); try { Thread.sleep(random.nextInt(1000)); System.out.println(Thread.currentThread().getName() + "&&" + threadlocal.get()); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread t2 = new Thread(()->{ threadlocal.set("T2"); try { Thread.sleep(random.nextInt(1000)); System.out.println(Thread.currentThread().getName() + "&&" + threadlocal.get()); } catch (InterruptedException e) { e.printStackTrace(); } }); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("=========================="); System.out.println(Thread.currentThread().getName() + "&&" + threadlocal.get()); } }
模拟线程保险箱
package com.dwz.concurrency.chapter33; import java.util.HashMap; import java.util.Map; /** * The Thread-Specific Storage 模拟线程保险箱 * 始终以当前线程作为key值 * @param <T> */ public class ThreadLocalSimulator<T> { private final Map<Thread, T> storage = new HashMap<>(); public void set(T t) { synchronized (this) { Thread key = Thread.currentThread(); storage.put(key, t); } } public T get() { synchronized (this) { Thread key = Thread.currentThread(); T value = storage.get(key); if(null == value) { return initialValue(); } return value; } } public T initialValue() { return null; } }
测试自定义线程保险箱
package com.dwz.concurrency.chapter33; import java.util.Random; public class ThreadLocalSimulatorTest { private final static ThreadLocalSimulator<String> threadlocal = new ThreadLocalSimulator<String>() { @Override public String initialValue() { return "No value"; }; }; //seed private final static Random random = new Random(System.currentTimeMillis()); public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(()->{ threadlocal.set("T1"); try { Thread.sleep(random.nextInt(1000)); System.out.println(Thread.currentThread().getName() + "&&" + threadlocal.get()); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread t2 = new Thread(()->{ threadlocal.set("T2"); try { Thread.sleep(random.nextInt(1000)); System.out.println(Thread.currentThread().getName() + "&&" + threadlocal.get()); } catch (InterruptedException e) { e.printStackTrace(); } }); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("=========================="); System.out.println(Thread.currentThread().getName() + "&&" + threadlocal.get()); } }
原文:https://www.cnblogs.com/zheaven/p/12153939.html