为什么 ThreadLocal 的 initialValue 不增加我的变量?
Why initialValue of ThreadLocal doesn't increment my variables?
我正在学习多线程;我有以下 ThreadID
class:
public class ThreadID {
private static volatile int nextID=0;
private static class ThreadLocalID extends ThreadLocal<Integer>{
protected synchronized Integer initialValue(){
return nextID ++;
}
}
private static ThreadLocalID threadID =new ThreadLocalID();
public static int get(){
return threadID.get();
}
public static void set (int index){
threadID.set(index);
}
}
和以下 Thread
class:
class MyThread1 extends Thread {
int x;
public ThreadID tID;
public int myid;
public MyThread1(String name) {
tID = new ThreadID();
myid = tID.get();
}
public void run() {
System.out.println("la thread =" + tID.get() + " myid= " + myid);
try {
this.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("la thread =" + tID.get() + " apres le sommeil ");
}
}
和我的 main
Class:
public static void main(String[] args) {
MyThread1 TH[] = new MyThread1[10];
for (int i = 0; i < 10; i++)
TH[i] = new MyThread1("nom" + i);
try {
for (int i = 0; i < 10; i++) TH[i].start();
for (int i = 0; i < 10; i++) TH[i].join();
} catch (InterruptedException e) {
}
}
问题:
我想要的是给每个Thread一个ID
;我发现当我在线程构造函数中初始化 id
时,值总是 0
(通常 initialValue
应该递增 nextID
)
la thread =1 myid= 0
la thread =3 myid= 0
la thread =2 myid= 0
但是当我在 Run
函数中初始化 id
时,它起作用了!
la thread =1 myid= 1
la thread =3 myid= 3
la thread =2 myid= 2
谁能解释为什么会这样?
What I want is to give each Thread an ID I found that when I init the
id in the thread constructor the value is always 0 (normally the
initialValue should increment the nextID)
所以在 MyThread1
class 构造函数中你做:
public MyThread1(String name) {
tID = new ThreadID();
myid = tID.get();
}
在这种情况下,实际调用tID.get();
的线程是main线程,即,正在调用的线程这些构造函数,来自主要 class:
MyThread1 TH[] = new MyThread1[10];
for (int i = 0; i < 10; i++)
TH[i] = new MyThread1("nom" + i);
第一次调用 tID.get()
将生成一个新的 ID
作为 0
,因为这是任何线程第一次调用 tID.get()
。来自同一线程(即主线程)的下一次调用不会生成新的ID
,而是始终return相同的ID,在在这种情况下,return 0
用于 main 线程。
but when I init the id inside the Run function it works!
run
方法内部:
public void run() {
System.out.println("la thread =" + tID.get() + " myid= " + myid);
try {
this.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("la thread =" + tID.get() + " apres le sommeil ");
}
tID.get()
将被不同的线程调用,这就是您获得新 ID 的原因。一个新 ID per 线程第一次调用 tID.get()
.
我正在学习多线程;我有以下 ThreadID
class:
public class ThreadID {
private static volatile int nextID=0;
private static class ThreadLocalID extends ThreadLocal<Integer>{
protected synchronized Integer initialValue(){
return nextID ++;
}
}
private static ThreadLocalID threadID =new ThreadLocalID();
public static int get(){
return threadID.get();
}
public static void set (int index){
threadID.set(index);
}
}
和以下 Thread
class:
class MyThread1 extends Thread {
int x;
public ThreadID tID;
public int myid;
public MyThread1(String name) {
tID = new ThreadID();
myid = tID.get();
}
public void run() {
System.out.println("la thread =" + tID.get() + " myid= " + myid);
try {
this.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("la thread =" + tID.get() + " apres le sommeil ");
}
}
和我的 main
Class:
public static void main(String[] args) {
MyThread1 TH[] = new MyThread1[10];
for (int i = 0; i < 10; i++)
TH[i] = new MyThread1("nom" + i);
try {
for (int i = 0; i < 10; i++) TH[i].start();
for (int i = 0; i < 10; i++) TH[i].join();
} catch (InterruptedException e) {
}
}
问题:
我想要的是给每个Thread一个ID
;我发现当我在线程构造函数中初始化 id
时,值总是 0
(通常 initialValue
应该递增 nextID
)
la thread =1 myid= 0
la thread =3 myid= 0
la thread =2 myid= 0
但是当我在 Run
函数中初始化 id
时,它起作用了!
la thread =1 myid= 1
la thread =3 myid= 3
la thread =2 myid= 2
谁能解释为什么会这样?
What I want is to give each Thread an ID I found that when I init the id in the thread constructor the value is always 0 (normally the initialValue should increment the nextID)
所以在 MyThread1
class 构造函数中你做:
public MyThread1(String name) {
tID = new ThreadID();
myid = tID.get();
}
在这种情况下,实际调用tID.get();
的线程是main线程,即,正在调用的线程这些构造函数,来自主要 class:
MyThread1 TH[] = new MyThread1[10];
for (int i = 0; i < 10; i++)
TH[i] = new MyThread1("nom" + i);
第一次调用 tID.get()
将生成一个新的 ID
作为 0
,因为这是任何线程第一次调用 tID.get()
。来自同一线程(即主线程)的下一次调用不会生成新的ID
,而是始终return相同的ID,在在这种情况下,return 0
用于 main 线程。
but when I init the id inside the Run function it works!
run
方法内部:
public void run() {
System.out.println("la thread =" + tID.get() + " myid= " + myid);
try {
this.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("la thread =" + tID.get() + " apres le sommeil ");
}
tID.get()
将被不同的线程调用,这就是您获得新 ID 的原因。一个新 ID per 线程第一次调用 tID.get()
.