简体   繁体   English

识别两个不同的线程

[英]Identifying two different threads

I am learning multithreading in java.My doubt is,is there any way two identify two different threads if they have same name below is my code 我正在学习java中的多线程。我的疑问是,如果它们具有相同的名称,两个是否有办法识别两个不同的线程,这是我的代码

package com.rajeev.test2;

    public class Test11 extends Thread {
        public static void main(String[] args) {
            System.out.println(Thread.currentThread());
            new Test11().start();
            new Test12();
        }
        @Override
        public void run() {
            Thread.currentThread().setName("ram");
            System.out.println(Thread.currentThread());
        }
    }
    class Test12 extends Thread{

        static{
            new Test12().start();
        }
        @Override
        public void run() {
            Thread.currentThread().setName("ram");
            System.out.println(Thread.currentThread());
        }
    }

output 输出

Thread[main,5,main]
Thread[ram,5,main]
Thread[ram,5,main]

I know they are two different threads having same name,so how to know they are different thread without changing name ? 我知道它们是两个具有相同名称的不同线程,那么如何在不更改名称的情况下知道它们是不同的线程?

Well you can track the Threads having same name by their ID which will be unique. 好了,您可以通过唯一的ID跟踪具有相同名称的Threads

Every thread has a name for identification purposes. 每个线程都有一个名称供识别。 More than one thread may have the same name. 一个以上的线程可能具有相同的名称。 If a name is not specified when a thread is created, a new name is generated for it. 如果在创建线程时未指定名称,则会为其生成一个新名称。

The JVM tracks threads by their ID , not by their name. JVM通过其ID而不是其名称来跟踪线程。

long threadId = Thread.currentThread().getId();

You should not use the name of the thread in order to identify a unique thread. 您不应使用线程名称来标识唯一线程。 The Thread class has a getId() method that returns a long number generated when the thread was created, and that is unique to the thread. Thread类具有getId()方法,该方法返回创建线程时生成的long整数,并且该数字是线程唯一的。 Use this in order to know if they are different. 使用此为了知道他们是否不同。

Using getId method 使用getId方法

Use getId to identify the uniqueness of the Thread. 使用getId标识线程的唯一性。

Thread id is to be set while creating a new thread. 创建新线程时将设置线程ID。 Irrespective of the constructor which will be used. 与将使用的构造函数无关。

Thread() Thread(Runnable, String) Thread(Runnable) Thread(String) Thread(ThreadGroup, Runnable, String, long) Thread(ThreadGroup, Runnable, String) Thread(ThreadGroup, Runnable) Thread(ThreadGroup, String)

all the constructors used init method. 所有构造函数都使用init方法。 In this method has thread id generation. 在此方法中具有线程ID生成。

Also In clone method also has thread id generation. 另外在克隆方法中还具有线程ID生成。

If Thread gets cloned , new thread id set for new thread reference. 如果Thread被克隆,则为新线程引用设置新线程ID。

Using equals & Hashcode method 使用equals和Hashcode方法

Another way to identify thread uniqueness is using equals and hashcode. 标识线程唯一性的另一种方法是使用equals和hashcode。

Thread doesnt has same equals and hascode method. 线程不具有相同的equals和hascode方法。

So using hashcode to differentiate thread uniqueness. 因此,使用哈希码来区分线程唯一性。

您可以使用equals运算符简单地比较Test11和Test12的两个对象(即线程)。

Your example code is completely weird. 您的示例代码是完全奇怪的。

  • Never subclass Thread - there are very rare cases in which this is necessary. 从不子类化Thread在极少数情况下有必要这样做。
  • Usually a Thread gets its name from the code that creates it. 通常, Thread是从创建它的代码中获取其名称的。 This makes it easy to give every new thread another distinct name. 这使得为​​每个新线程赋予另一个不同的名称变得容易。 Do not set the name in the run method. 不要在run方法中设置名称。
  • Please don't start threads in the static constructor of a class. 请不要在类的static构造函数中启动线程。

Probably your example should look simliar to that: 可能您的示例应该与此类似:

public class ThreadExample {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread());
        new Thread(new PrintThreadNameTask(), "thread-1").start();
        new Thread(new PrintThreadNameTask(), "thread-2").start();
    }
}

class PrintThreadNameTask implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread());
    }
}

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

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