在线程的构造方法中使用ThreadLocal.set存储线程变量,在run方法中获取的变量值会为空。若是须要在run方法使用变量,那么必须在run方法set。ide
public class Test { private static final ThreadLocal<String> TL = new ThreadLocal<String>(); public static void main(String[] args) throws InterruptedException { TestThread t1 = new TestThread("a"); t1.start(); Thread.sleep(1000); TestThread t2 = new TestThread("b"); t2.start(); } static class TestThread extends Thread { private String str; public TestThread(String str) { this.str = str; TL.set(str); init(); } public void init() { System.out.println("init:" + TL.get()); } @Override public void run() { System.out.println("run:" + TL.get()); TL.set(str); System.out.println("run2:" + TL.get()); } } }