我正在学习java Threads,我去了关于本地线程值的课程,我决定制作一个使用它们的程序。 我在构造函数中设置本地线程值(线程构造函数)在屏幕上显示它以检查它是否有效但是当我启动线程时,本地值变为NULL,因此我得到NullPointerException。 我错过了什么,或者我只能在start方法中设置本地线程值?
package practice;
import static java.lang.System.out;
import java.util.Scanner;
class Try
{
Thread a1,a2,a3;
int x=0;
synchronized void change(int who){
out.println("who called"+who);
out.println("x initial="+x);
x++;
out.println("x after="+x);
}
class now implements Runnable{
ThreadLocal<Integer> id=new ThreadLocal<Integer>();
public void run(){
for(int i=1;i<=25;i++){
out.println("im running id="+id.get());
change(id.get());
out.println("after call me="+id.get());
}
}
now(int givenid){
out.println("my givenid is "+givenid);
id.set(givenid);
out.println("my id is "+id.get());
}
}
public static void main(String[] args)
{
new Try();
}
Try(){
a1=new Thread(new now(1));
a2=new Thread(new now(2));
a1.start();
a2.start();
}
}
答案 0 :(得分:1)
我非常喜欢Banthar的答案,但我只想通过添加一些打印来说明您的代码会发生什么......
package threads;
import static java.lang.System.out;
class Main {
Thread a1, a2, a3;
int x = 0;
public Main() {
out.println("Current thread (in Main's constructor): " + Thread.currentThread().getName());
a1 = new Thread(new MyRunnable(1));
a2 = new Thread(new MyRunnable(2));
a1.start();
a2.start();
}
private synchronized void change(int who) {
out.println("Current thread (in change() method): " + Thread.currentThread().getName());
out.println("who called" + who);
out.println("x initial=" + x);
x++;
out.println("x after=" + x);
}
private class MyRunnable implements Runnable {
ThreadLocal<Integer> id = new ThreadLocal<Integer>();
public MyRunnable(int givenid) {
out.println("Current thread (in MyRunnable's constructor): " + Thread.currentThread().getName());
out.println("my givenid is " + givenid);
id.set(givenid);
out.println("my id is " + id.get());
}
public void run() {
out.println("Current thread (in run() method): " + Thread.currentThread().getName());
out.println("im running id=" + id.get());
change(id.get());
out.println("after call me=" + id.get());
}
}
public static void main(String[] args) {
new Main();
}
}
输出是:
Current thread (in Main's constructor): main
Current thread (in MyRunnable's constructor): main
my givenid is 1
my id is 1
Current thread (in MyRunnable's constructor): main
my givenid is 2
my id is 2
Current thread (in run() method): Thread-0
Current thread (in run() method): Thread-1
im running id=null
im running id=null
很抱歉更改名称。
正如您在实例化MyRunnable
(您的now
课程)时所看到的那样,您可以在主主题中设置ThreadLocal
。这就是为什么你不能通过其他线程访问它。
答案 1 :(得分:0)
这是线程本地人应该如何工作的方式。线程局部就像是从线程到对象的映射。每个线程都看到它自己的价值。您正在主线程中设置值,只有该线程会看到此值。
我不确定你想在这里实现什么目标。线程局部文件通常用于缓存非线程安全对象,这些对象分配起来很昂贵,并且通过参数传递它们是不可行的。它们通常存储在静态字段中。例如:
private static ThreadLocal<SimpleDateFormat> FORMAT = new ThreadLocal<SimpleDateFormat>();
public static Date parse(final String date) throws ParseException {
SimpleDateFormat format = FORMAT.get();
if (format == null) {
format = new SimpleDateFormat();
FORMAT.set(format);
}
return format.parse(date);
}
这将为每个线程缓存一个SimpleDateFormat
实例。
答案 2 :(得分:0)
您需要在ThreadLocal
内设置“已包装”值。看看javadocs。您需要先致电set()
或使用initialValue()
,然后ThreadLocal
将返回null
以外的任何内容。