SimpleThreadLocal import java.util.*;/** * 自定义ThreadLocal * Created by xuliugen on 2017/10/10. * @param */public class SimpleThreadLocal { /** * Key为线程对象,Value为传入的值对象 */ private Map valueMap = Collections.syn
import java.util.*; /** * 自定义ThreadLocal * Created by xuliugen on 2017/10/10. * @param*/ public class SimpleThreadLocal { /** * Key为线程对象,Value为传入的值对象 */ private Map valueMap = Collections.synchronizedMap(new HashMap ()); /** * 设值 * @param value Map键值对的value */ public void set(T value) { valueMap.put(Thread.currentThread(), value); } /** * 取值 * @return */ public T get() { Thread currentThread = Thread.currentThread(); //返回当前线程对应的变量 T t = valueMap.get(currentThread); //如果当前线程在Map中不存在,则将当前线程存储到Map中 if (t == null && !valueMap.containsKey(currentThread)) { t = initialValue(); valueMap.put(currentThread, t); } return t; } public void remove() { valueMap.remove(Thread.currentThread()); } public T initialValue() { return null; } public static void main(String[] args) { SimpleThreadLocal > threadLocal = new SimpleThreadLocal<>(); new Thread(() -> { List
params = new ArrayList<>(3); params.add("张三"); params.add("李四"); params.add("王五"); threadLocal.set(params); System.out.println(Thread.currentThread().getName()); threadLocal.get().forEach(param -> System.out.println(param)); }).start(); new Thread(() -> { try { Thread.sleep(1000); List params = new ArrayList<>(2); params.add("Chinese"); params.add("English"); threadLocal.set(params); System.out.println(Thread.currentThread().getName()); threadLocal.get().forEach(param -> System.out.println(param)); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); } }