本文共 824 字,大约阅读时间需要 2 分钟。
//为线程局部变量设置初始值 static ThreadLocal threadLocal = new ThreadLocal() { @Override protected Object initialValue() { return "初始值"; } }; /** * 如果一个线程是从其他某个线程中创建的,这个类将提供继承的值 */ static ThreadLocal threadLocalII = new InheritableThreadLocal() { @Override protected Object childValue(Object o) { return "覆盖继承的值"; } }; public static void main(String args[]){ threadLocalII.set(new Integer(123)); Thread thread = new MyThread(); thread.start(); System.out.println("main = " + threadLocalII.get()); } static class MyThread extends Thread{ @Override public void run(){ System.out.println("MyThread = " + threadLocalII.get()); } }
转载于:https://my.oschina.net/u/2552286/blog/1921772