How
to know the currently executing Thread
Thread
API provides a method class currentThread(), which returns the
reference of the currently executing thread
Syntax:
public static
Thread currentThread()
Since
currentThread() is a static method, we can call it by using Thread
class like
Thread.currentThread()
Example
class MyThread extends Thread{ public void run(){ System.out.println("In run method, My name is " + currentThread()); } public static void main(String args[]){ MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); t1.start(); t2.start(); System.out.println("In main method, My name is " + Thread.currentThread()); } }
Possible
Outcome
In run method, My name is Thread[Thread-0,5,main] In main method, My name is Thread[main,5,main] In run method, My name is Thread[Thread-1,5,main]
Observation
See
the format Thread[Thread-0,5,main]
Thread-0
: specifies the thread name, If you don't set a name to the Thread,
it will follow default naming conventions
5
: Priority of the Thread., If you don't set a priority to a thread,
by default it has 5, will
main
: specifies the group name of the thread
Setting
and get the name of a Thread
Every
thread has a name associated with it. Thread API provides getName()
and setName() methods to retrieve the thread name and set a name to
thread respectively.
If
user not set the name explicitly, then it will assign with a default
name at the time of construction.
Setting
a name to Thread
public
final void setName(String name)
Changes
the name of this thread to be equal to the argument name. Since the
setName() is a final method, so you can't override it.
Getting
the name of a Thread
public
final String getName()
Returns this
thread's name. Since the getName() is a final method, so you can't
override it.
class MyThread extends Thread{ public void run(){ System.out.println("Current Runing Thread is " + Thread.currentThread()); System.out.println("Current Thread name is " + Thread.currentThread().getName()); } public static void main(String args[]){ MyThread t1 = new MyThread(); t1.setName("MyThread1"); t1.start(); } }
Output
Current Runing Thread is Thread[MyThread1,5,main] Current Thread name is MyThread1
Points
to be noted while setting the Thread name
1.
Set the thread name before calling the start(). Of course you can
can set the name to a thread after calling thread start method, but
it is not preferable.
Example
class MyThread extends Thread{ public void run(){ System.out.println("Current Running Thread is " + Thread.currentThread()); System.out.println("Current Thread name is " + Thread.currentThread().getName()); } public static void main(String args[]){ MyThread t1 = new MyThread(); t1.setName("MyThread1"); t1.start(); for(int i=0; i < 10000; i++){ t1.setName("MyThread" + i); } System.out.println(t1.getName()); } }
Output
Current Running Thread is Thread[MyThread14,5,main] Current Thread name is MyThread328 MyThread9999
See
the above output in the bold letters, if thread name changed, when
you are trying to perform some operations based on Thread name, then
you will face problems.
2.
Do not change the name of main thread. It will cause unexpected
results
3.
You can give same name to multiple threads, but give unique name to
the threads, to identify differently
No comments:
Post a Comment