More JAVA Programs
Character/Alphabet Patterns in JAVA
Collections in JAVA
Applet Programming in JAVA
Thread.currentThread() Method in JAVA
The java.lang.Thread.currentThread() method returns the reference to the currently executing thread object.
The currentThread() method tells us which is the current thread executing.
Example of Thread.currentThread()
Program(Save as Main.java)
class threadCurrent implements Runnable { public void run() { System.out.println(Thread.currentThread()+" is running!"); } } class Main { public static void main(String args[]) { threadCurrent obj = new threadCurrent(); Thread t1 = new Thread(obj); t1.setName("MyThread"); t1.start(); } }
Output
Thread[MyThread,5,main] is running!
In the above program, we have created a thread(t1) and named it as "MyThread", so when we call Thread.currentThread() method we see that MyThread is currently executing.