currentThread()
method is the static method which returns a reference to the Thread object representing the currently executing thread (the code that called this method).
Thread.currentThread().setName("...")
).
Thread.currentThread().isInterrupted()
/ interrupt()
on self).
ThreadLocal
and other concurrency utilities to attach per-thread state.
public static native Thread currentThread()
class MyTask extends Thread
{
@Override
public void run()
{
// Identify current thread details
Thread t = Thread.currentThread();
System.out.println("Id : "+t.getId());
System.out.println("Name 1 : "+t.getName());
// Modify the current thread
t.setName("Deepak-Thread");
System.out.println("Name 2 : "+t.getName());
}
}
public class CurrentThreadDemo
{
public static void main(String[] args)
{
MyTask t1 = new MyTask();
t1.start();
}
}
Id : 22 Name 1 : Thread-0 Name 2 : Deepak-Thread
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.