- Trending Categories
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
Fetching the name of the current thread in Java
The name of the current thread can be obtained using the below code −
Example
import java.io.*; class name_a_thread extends Thread { @Override public void run() { System.out.println("Fetching the name of the current thread-"); System.out.println(Thread.currentThread().getName()); } } public class Demo { public static void main (String[] args) { name_a_thread thr_1 = new name_a_thread(); name_a_thread thr_2 = new name_a_thread(); thr_1.start(); thr_2.start(); } }
Output
Fetching the name of the current thread- Thread-0 Fetching the name of the current thread- Thread-1
A class named ‘name_a_thread’ extends to the parent class Thread. It overrides the ‘run’ method, and relevant message is displayed on the console when this function is called. A class named Demo contains the main function, where two instances of the class are created. Both these instances are called using the ‘start’ function.
- Related Articles
- How to display the name of the Current Thread in C#?
- Get current thread in Java
- C# Program to display name of Current Thread
- How to get current thread name in android?
- How to display all stack frames of the current thread in Java 9?
- Display the current method name in Java
- How to obtain Status of the Current Thread in C#?
- How to find the Current Context id of the Thread in C#?
- Getting the unique identifier for the current managed thread in C#
- The join() method of Thread Class in Java
- Controlling the main thread in Java
- How to get getAllStackTraces of current thread in android?
- Describe the life cycle of a thread in Java?
- C# Program to Check status of Current Thread
- What is the Thread class in Java?

Advertisements