ThreadLocal InitialValue 被调用两次

ThreadLocal InitialValue is called twice

我设置了所有线程通用的ThreadLocal初始值。调用构造函数时,我更改了值并正确打印。但是当启动线程时,它再次更改为初始值?这是预期的吗?若有,解释为何?下面是我的两个类。提前致谢。

  1. MyRunnableThreadLocal.java

    public class MyRunnableThreadLocal implements Runnable {
    
    private ThreadLocal<String> threadLocal = new ThreadLocal<String>(){
        @Override protected String initialValue() {
            return "Thread Name not set yet";
        }
    };
    public MyRunnableThreadLocal(String threadName) {
        System.out.println(threadLocal.get());
        threadLocal.set(threadName);
        System.out.println("Thread name: " + threadLocal.get());
    }
    @Override
    public void run() {
        System.out.println(threadLocal.get());
        threadLocal.set(threadLocal.get() + " " + (Math.random() * 100D));
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
    
        System.out.println(threadLocal.get());
    }
    
    
    }
    
  2. ThreadLocalTest.java

    public class ThreadLocalTest {
    
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnableThreadLocal("Thread 1"));
        Thread t2 = new Thread(new MyRunnableThreadLocal("Thread 2"));
    
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    
    }
    

输出:

Thread Name not set yet
Thread name: Thread 1
Thread Name not set yet
Thread name: Thread 2
Thread Name not set yet
Thread Name not set yet
Thread Name not set yet 20.24825634584746
Thread Name not set yet 59.67633602373702

ThreadLocal 对于运行代码的线程(以及与之关联的线程本地对象)是本地的。

Thread Name not set yet <= runs in main
Thread name: Thread 1   <= runs in main for the first MyRunnableThreadLocal object
Thread Name not set yet <= runs in main for the 2nd MyRunnableThreadLocal object
Thread name: Thread 2   <= runs in main for the 2nd MyRunnableThreadLocal object
Thread Name not set yet <= runs in the first thread for the first MyRunnableThreadLocal
Thread Name not set yet <= runs in the 2nd thread for the 2nd MyRunnableThreadLocal
Thread Name not set yet 20.24825634584746  <= runs in the first thread for the first MyRunnableThreadLocal
Thread Name not set yet 59.67633602373702  <= runs in the 2nd thread for the 2nd MyRunnableThreadLocal

所以你有三个线程和两个 ThreadLocal。您的 main 使用本地线程,并且您启动的两个线程各有一个值。

每次你有一个新的 ThreadLocal 对象,或者一个新的线程,它都有一个新的初始值。

What about setting the thread local value?

在创建另一个 ThreadLocal 或在另一个线程中使用它之前,您只读取一次设置的值。

If first four lines are run in main thread, then set in the line# 2 is not reflecting on line# 3?

第 3 行是不同 ThreadLocal 中的不同 MyRunnableThreadLocal

对象

If it is working on first MyRunnableThreadLocal object then why it is not reflecting on line# 5 or 6?

第 5 行和第 6 行是第一次在这些线程中使用 ThreadLocal,因此它们具有初始值。

注意:如果您使用 InheritableThreadLocal,它会如您所愿。这是因为新线程 "inherits" 其父线程设置的值。