Thread-0
, Thread-1
, Thread-2
and so on.
public final String getName()
public final void setName(String name)
class MyThread extends Thread
{
@Override
public void run()
{
System.out.println("Thread running: " + getName());
// Change the thread name
setName("Updated-Thread");
System.out.println("Thread renamed to: " + getName());
}
}
public class ThreadNameDemo
{
public static void main(String[] args)
{
MyThread t1 = new MyThread();
t1.setName("Initial-Thread");
System.out.println("Before start: " + t1.getName());
t1.start();
// Main thread
System.out.println("Main thread: " + Thread.currentThread().getName());
}
}
Before start: Initial-Thread Main thread: main Thread running: Initial-Thread Thread renamed to: Updated-Thread
setName()
or constructor Thread(String name)
.Thread-0
, Thread-1
, etc., if not set explicitly.
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.