- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display thread's information in Java
In order to get the name of the Thread, we use the getName() method in Java. This method returns the name of the thread.
Declaration − The java.lang.Thread.getName() method is declared as follows −
public String getName()
In order to get the identity of the Thread, we use the getId() method in Java. This method returns the identifier of the thread. The thread ID is a positive long number produced during the creation of the thread.
Declaration − The java.lang.Thread.getId() method is declared as follows −
public long getId()
In order to get the state of the Thread, we use the getState() method in Java. This method returns the state of the thread.The thread state is designed for keeping a check on the state of the system and not for control of synchronization.
Declaration − The java.lang.Thread.getState() method is declared as follows −
public Thread.state getState()
In order to get the status of the Thread, we use the isAlive() method in Java. This method checks whether the thread is alive. A thread is said to be alive if it has started and has not terminated.
Declaration − The java.lang.Thread.isAlive() method is declared as follows −
public final boolean isAlive()
Let us see an example to get the information of the thread −
Example
public class Example extends Thread { public void run() { for (int k = 0; k <= 5; k++) { print(); } } public void print() { Thread t = Thread.currentThread(); String name = t.getName(); long id = t.getId(); boolean status = t.isAlive(); Thread.State state = t.getState(); System.out.print("Thread name = " + name); System.out.print(" Thread ID = " + id); System.out.print(" Thread State = " + state); System.out.println(" Thread Status = " + status); } public static void main(String[] args) { Example e = new Example(); e.start(); // calls the run method for (int i = 0; i <= 5; i++) { e.print(); } } }
Output
Thread name = main Thread ID = 1 Thread State = RUNNABLE Thread Status = true Thread name = main Thread ID = 1 Thread State = RUNNABLE Thread Status = true Thread name = main Thread ID = 1 Thread State = RUNNABLE Thread Status = true Thread name = main Thread ID = 1 Thread State = RUNNABLE Thread Status = true Thread name = main Thread ID = 1 Thread State = RUNNABLE Thread Status = true Thread name = main Thread ID = 1 Thread State = RUNNABLE Thread Status = true Thread name = Thread-0 Thread ID = 21 Thread State = RUNNABLE Thread Status = true Thread name = Thread-0 Thread ID = 21 Thread State = RUNNABLE Thread Status = true Thread name = Thread-0 Thread ID = 21 Thread State = RUNNABLE Thread Status = true Thread name = Thread-0 Thread ID = 21 Thread State = RUNNABLE Thread Status = true Thread name = Thread-0 Thread ID = 21 Thread State = RUNNABLE Thread Status = true Thread name = Thread-0 Thread ID = 21 Thread State = RUNNABLE Thread Status = true