gpt4 book ai didi

java - 将 ThreadLocal 类传递给另一个线程?

转载 作者:行者123 更新时间:2023-11-30 02:19:56 25 4
gpt4 key购买 nike

我在 Java 程序中使用 ThreadLocal,以便通过线程获取和设置某些数据成员。

我有一堂包含以下内容的类(class)

public class ThreadLocalManager {
public static final ThreadLocal<String> accountUsername = new ThreadLocal<String>();
public static final ThreadLocal<String> accountPassword = new ThreadLocal<String>();
public static final ThreadLocal<Long> accountId = new ThreadLocal<Long>();
.....................
.....................
.....................

}

我一直在我的多线程应用程序中使用它,没有出现任何问题。现在每个线程将创建另一个线程,我希望将 ThreadLocalManager 传递给创建的新线程。这将如何完成?

最佳答案

编辑:我根据最初的建议(如下)修改了示例代码以使用InheritableThreadLocal,并且我发现它要简单得多,因此添加更新的代码。

这是更新后的 ThreadLocalManager:

package inheritableThreadLocal;

public class ThreadLocalManager {
public static final InheritableThreadLocal<String> accountUsername = new InheritableThreadLocal<String>();
public static final InheritableThreadLocal<String> accountPassword = new InheritableThreadLocal<String>();
public static final InheritableThreadLocal<Long> accountId = new InheritableThreadLocal<Long>();
}

我还更新了 Runner (参见下面的原文),但现在它的唯一目的是打印变量的值:

package inheritableThreadLocal;

public class Runner implements Runnable{

@Override
public void run() {
System.out.println("Inside Runner's run");
System.out.println(ThreadLocalManager.accountId.get());
System.out.println(ThreadLocalManager.accountUsername.get());
System.out.println(ThreadLocalManager.accountPassword.get());
}

}

最后更新的类包含主方法:

package inheritableThreadLocal;

public class ThreadLocalMain {

public static void main(String[] args) {
System.out.println("At start of main");
System.out.println(ThreadLocalManager.accountId.get());
System.out.println(ThreadLocalManager.accountUsername.get());
System.out.println(ThreadLocalManager.accountPassword.get());

Thread t1 = new Thread(new Runnable(){

@Override
public void run() {
ThreadLocalManager.accountId.set(new Long(12345));
ThreadLocalManager.accountUsername.set("user1");
ThreadLocalManager.accountPassword.set("pass1");

System.out.println("In t1 run");
System.out.println(ThreadLocalManager.accountId.get());
System.out.println(ThreadLocalManager.accountUsername.get());
System.out.println(ThreadLocalManager.accountPassword.get());

Thread t2 = new Thread(new Runner());
t2.start();
}

});
t1.start();
}

}

原始建议:

因此,您可以使用 Runnable 对象创建新的 Thread 对象。如果这样做,您可以创建实现 Runnable 的自定义对象,并可以保存 Thr​​eadLocal 变量的值,直到新的 Thread 启动。一旦新的Thread启动(即调用run方法),这些自定义的Runnable对象就会设置ThreadLocal变量。

这是一个带有我为此创建的 main 的类:

package threadLocal;

public class ThreadLocalMain {

public static void main(String[] args) {
System.out.println("At start of main");
System.out.println(ThreadLocalManager.accountId.get());
System.out.println(ThreadLocalManager.accountUsername.get());
System.out.println(ThreadLocalManager.accountPassword.get());

Thread t1 = new Thread(new Runnable(){

@Override
public void run() {
ThreadLocalManager.accountId.set(new Long(12345));
ThreadLocalManager.accountUsername.set("user1");
ThreadLocalManager.accountPassword.set("pass1");

System.out.println("In t1 run");
System.out.println(ThreadLocalManager.accountId.get());
System.out.println(ThreadLocalManager.accountUsername.get());
System.out.println(ThreadLocalManager.accountPassword.get());

Thread t2 = new Thread(new Runner(ThreadLocalManager.accountUsername.get(),
ThreadLocalManager.accountPassword.get(),
ThreadLocalManager.accountId.get()));
t2.start();
}

});
t1.start();
}

}

为了完整起见,这是我使用的 ThreadLocalManager:

package threadLocal;

public class ThreadLocalManager {
public static final ThreadLocal<String> accountUsername = new ThreadLocal<String>();
public static final ThreadLocal<String> accountPassword = new ThreadLocal<String>();
public static final ThreadLocal<Long> accountId = new ThreadLocal<Long>();
}

最后,这是我创建的自定义Runnable:

package threadLocal;

public class Runner implements Runnable{

private String userName, password;
private long acctId;

public Runner(String user, String pword, long accountId){
System.out.println("In Runner constructor");
System.out.println(ThreadLocalManager.accountId.get());
System.out.println(ThreadLocalManager.accountUsername.get());
System.out.println(ThreadLocalManager.accountPassword.get());

userName = user;
password = pword;
acctId = accountId;
}

@Override
public void run() {
System.out.println("Inside Runner's run");
System.out.println(ThreadLocalManager.accountId.get());
System.out.println(ThreadLocalManager.accountUsername.get());
System.out.println(ThreadLocalManager.accountPassword.get());

ThreadLocalManager.accountId.set(acctId);
ThreadLocalManager.accountUsername.set(userName);
ThreadLocalManager.accountPassword.set(password);

System.out.println("Inside Runner's run - after setting");
System.out.println(ThreadLocalManager.accountId.get());
System.out.println(ThreadLocalManager.accountUsername.get());
System.out.println(ThreadLocalManager.accountPassword.get());
}

}

此代码将 ThreadLocal 变量的值传递到 Runner 实例,然后使用 Runner 实例创建一个新的 >Thread(如果您运行代码,您将看到,首先没有 ThreadLocal 变量的值)。然后,Runner 设置 ThreadLocal 变量,正如您从控制台输出中看到的,它们与 t1 的内容匹配。

希望这有帮助!

关于java - 将 ThreadLocal 类传递给另一个线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47170557/

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