简体   繁体   English

为什么 ThreadLocal 的 initialValue 不增加我的变量?

[英]Why initialValue of ThreadLocal doesn't increment my variables?

I am learning about multithreading;我正在学习多线程; and I have the following ThreadID class:我有以下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);
    }
}

and the following Thread class:以及以下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 ");
    }
}

and my main Class:和我的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) {
    }
}

Question:题:

What I want is to give each Thread an ID ;我想要的是给每个 Thread 一个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 )我发现当我在线程构造函数中初始化id时,该值始终为0 (通常initialValue应该递增nextID

la thread =1 myid= 0
la thread =3 myid= 0
la thread =2 myid= 0

but when I init the id inside the Run function it works !但是当我在Run function 中初始化id时,它起作用了!

la thread =1 myid= 1
la thread =3 myid= 3
la thread =2 myid= 2

Can any one explain why this happens?谁能解释为什么会这样?

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)我想要的是给每个线程一个 ID,我发现当我在线程构造函数中初始化 id 时,该值始终为 0(通常 initialValue 应该递增 nextID)

So in the MyThread1 class constructor you do:因此,在MyThread1 class 构造函数中,您执行以下操作:

public MyThread1(String name) {
    tID = new ThreadID();
    myid = tID.get();
}

In this case, the thread actually calling tID.get();在这种情况下,线程实际上调用tID.get(); is the main thread, ie , the thread that is calling those constructors, from the main class:线程,调用这些构造函数的线程,来自主 class:

MyThread1 TH[] = new MyThread1[10];
for (int i = 0; i < 10; i++)
    TH[i] = new MyThread1("nom" + i); 

The first call to tID.get() will generate a new ID as 0 , since it is the first time that any thread is calling tID.get() .第一次调用tID.get()将生成一个新ID0 ,因为这是任何线程第一次调用tID.get() The next calls from the same thread ( ie, main thread) will not generate a new ID , but instead, it always returns the same ID, in this case, return 0 for the main thread.来自同一线程(主线程)的下一次调用不会生成新的ID ,而是始终返回相同的 ID,在这种情况下,线程返回0

but when I init the id inside the Run function it works!但是当我在 Run function 中初始化 id 时,它起作用了!

Inside the run method: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 ");
}

the tID.get() will be called by different threads, and that is why you get new IDs. tID.get()将被不同的线程调用,这就是您获得新 ID 的原因。 One new ID per thread calling for the first time tID.get() .每个线程一个新 ID 第一次调用tID.get()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM