gpt4 book ai didi

java - ThreadLocal InitialValue 被调用两次

转载 作者:行者123 更新时间:2023-12-02 03:38:45 25 4
gpt4 key购买 nike

我设置了所有线程通用的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com