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
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

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 to Expect in the Cursor System Design Interview
Cursor's design conversations live where ML serving meets editor experience: context retrieval over codebases, streaming completions, Tab prediction latency, and codebase indexing.
Why Amazon for freshers?
Which DocuSign interview questions to prepare leetcode?
What Is the Harvey Interview Process Like? (Round by Round)
Harvey's reported loop: a coding phone screen, then four onsite rounds covering coding, system design, behavior, and a project deep dive.
What questions and answers will I be asked in an interview?
What is a system design?
Related Courses
New
Grokking Agentic Design Patterns course cover
Grokking Agentic Design Patterns
Design agents that act safely, recover from failure, and justify their cost.
4.9
(561 learners)
Discounted price for Your Region

$98

New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$98

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

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.