java - ThreadLocal InitialValue 被调用两次

标签 java multithreading thread-local

我设置了所有线程通用的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 行是不同 MyRunnableThreadLocal 中的不同 ThreadLocal 对象

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

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

注意:如果您使用InheritableThreadLocal,它将执行您所期望的操作。这是因为新线程“继承”其父线程设置的值。

关于java - ThreadLocal InitialValue 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37105234/

相关文章:

java - Sametime Java 工具包 loginByPassword 已弃用

java - 引用的 URL 显示为 null(Java、JSP)

Java线程没有清理

java - 如何为声明为 ThreadLocal 的变量的多个副本保持一致性?

java - 如果线程局部映射包含对线程局部对象的弱引用,那么为什么它没有被垃圾回收?

java - InputMethodService 中的 startActivity 后 EditorInfo 为 null

java - JNDI 与 spring mvc3 集成

c# - 多线程中的数学类给出错误

java - 如何在单独的线程上将旋转动画添加到背景图像?

java - 线程局部仅初始化为零