You can assign a name to thread instance by using the setName()
method and get the name of the thread using the getName()
method. The naming support is also available as a constructor of the Thread
class such as Thread(String name)
and Thread(Runnable target, String name)
.
package org.kodejava.lang;
public class ThreadNameDemo extends Thread {
public ThreadNameDemo() {
}
public ThreadNameDemo(String name) {
super(name);
}
public static void main(String[] args) {
Thread thread1 = new ThreadNameDemo();
thread1.setName("FOX");
thread1.start();
Thread thread2 = new ThreadNameDemo("DOG");
thread2.start();
}
@Override
public void run() {
// Call getName() method to get the thread name of this
// thread object.
System.out.println("Running [" + this.getName() + "]");
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024