我正在学习Java线程,上了有关本地线程值的课程,因此决定制作一个使用它们的程序。
我在构造函数(线程构造函数)中设置了本地线程值,然后在屏幕上显示该值以检查其是否起作用,但是当我启动线程时,本地值变成了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();
}
}
最佳答案
我真的很喜欢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
。这就是为什么您无法通过其他线程访问它的原因。