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
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());
}
}
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.
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.
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. Use this in order to know if they are different.
Using getId method
Use getId to identify the uniqueness of the Thread.
Thread id is to be set while creating a new thread. 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. In this method has thread id generation.
Also In clone method also has thread id generation.
If Thread gets cloned , new thread id set for new thread reference.
Using equals & Hashcode method
Another way to identify thread uniqueness is using equals and hashcode.
Thread doesnt has same equals and hascode method.
So using hashcode to differentiate thread uniqueness.
您可以使用equals运算符简单地比较Test11和Test12的两个对象(即线程)。
Your example code is completely weird.
Thread
- there are very rare cases in which this is necessary. Thread
gets its name from the code that creates it. This makes it easy to give every new thread another distinct name. Do not set the name in the run
method. static
constructor of a class. 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());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.