In this tutorial, we are using Thread.currentThread() method to find the current thread name.
Thread.currentThread() :
Thread class provides method to display the current running thread. It is static Thread method so that there is no need to call a reference to a Thread object. It returns the current running thread object reference.
public static Thread currentThread() : This method returns a reference to the currently running thread object.
Example : In this example we are displaying the current running thread by calling currentThread() method.
class NewCurrentThread implements Runnable {
NewCurrentThread(String name) {
Thread thread = new Thread(this, name);
thread.start();
}
public void run() {
/* Returns currently executing thread object. */
Thread thread = Thread.currentThread();
System.out.println("Welcome in Thread " + thread.getName());
}
}
public class CurrentThread {
public static void main(String args[]) {
new NewCurrentThread("One");
new NewCurrentThread("Two");
}
}
Output :
Welcome in Thread One Welcome in Thread Two
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.