Which method is used to identify a thread?

In Java, the getName() method of the Thread class is used to identify a thread. This method returns the name of the current thread as a string.

Key Details:

  • The name of a thread is an identifier that can be used to distinguish between different threads.
  • Each thread has a default name assigned by Java when it is created. The default name follows the format "Thread-N", where N is a unique number. For example, "Thread-0", "Thread-1", etc.
  • You can also assign a custom name to a thread when you create it.

Example of Identifying a Thread:

class MyThread extends Thread { public void run() { // Get and print the name of the current thread System.out.println("Thread name: " + Thread.currentThread().getName()); } public static void main(String[] args) { MyThread thread1 = new MyThread(); thread1.setName("MyCustomThread"); // Set a custom name thread1.start(); // Start the thread MyThread thread2 = new MyThread(); thread2.start(); // This will use the default name } }

Explanation:

  • The getName() method is used inside the run() method to print the name of the current thread.
  • In this example, thread1 is given a custom name, "MyCustomThread", while thread2 will use the default name assigned by Java (e.g., "Thread-1").

Output:

Thread name: MyCustomThread
Thread name: Thread-0

Additional Information:

  • Thread Identification: Besides the thread name, a thread is also identified by its thread ID (a unique identifier for each thread), which can be obtained using the getId() method.

    long threadId = Thread.currentThread().getId();
  • Setting Custom Thread Names: You can assign a custom name to a thread either through the constructor or by using the setName() method after creating the thread.

Conclusion:

  • The getName() method is used to identify and retrieve the name of the thread, which can be helpful for debugging or distinguishing between threads in a multithreaded application.

To deepen your understanding of thread management and other concurrency topics in Java, you can explore these courses from DesignGurus.io:

These courses will help you manage threads, synchronize tasks, and solve concurrency problems in Java.

TAGS
Coding Interview
System Design Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the passing score for Cisco?
What is the difference between __init__ and __call__?
What is asked in an Oracle interview?
What is the star answer method?
Which programming language is used in Adobe?
What do software interns do?
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.