Last Updated : 23 Jul, 2025
The getId() method of Thread class returns the identifier of the invoked thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.
Java allows concurrent execution of different parts of a program with the help of threads. Multithreading in Java is achieved by extending the Thread class or implementing the Runnable Interface. Since multiple inheritance is not allowed in Java it is recommended to implement the Runnable interface for thread creation so that if required the class implementing the Runnable interface can extend some other class. In this article, we have demonstrated both the methods of creating a thread. The first approach shows thread creation by extending the Thread class and the second approach shows thread creation by implementing the Runnable Interface.
Declaration
public long getId()
Return value: This method returns the ID of a thread.
Approach 1: Following are the steps to create a thread by extending the Thread class.
Example:
Java
// Java program to get the id of a
// thread
import java.util.*;
public class ThreadDemo1 extends Thread {
public void run()
{
// gets the name of current thread
System.out.println(
"Current Thread Name: "
+ Thread.currentThread().getName());
// gets the ID of the current thread
System.out.println(
"Current Thread ID: "
+ Thread.currentThread().getId());
}
public static void main(String[] args)
throws InterruptedException
{
Scanner s = new Scanner(System.in);
// creating first thread
ThreadDemo1 t1 = new ThreadDemo1();
// creating second thread
ThreadDemo1 t2 = new ThreadDemo1();
// Starting the thread
t1.start();
t2.start();
// t2 does not start execution until t1 completes
// execution
t1.join();
}
}
Current Thread Name: Thread-0 Current Thread Name: Thread-1 Current Thread ID: 11 Current Thread ID: 12
Approach 2: In the second approach, the thread is created by implementing the Runnable Interface.
Example:
Java
// Java program to get the id of a
// thread
public class ThreadDemo2 implements Runnable {
public void run()
{
// gets the name of current thread
System.out.println(
"Current Thread Name: "
+ Thread.currentThread().getName());
// gets the ID of the current thread
System.out.println(
"Current Thread ID: "
+ Thread.currentThread().getId());
}
public static void main(String[] args)
{
// Runnable target
ThreadDemo2 t = new ThreadDemo2();
// create threads
Thread t1 = new Thread(t, "First Thread");
Thread t2 = new Thread(t, "Second Thread");
// start threads
t1.start();
t2.start();
}
}
Current Thread Name: First Thread Current Thread Name: Second Thread Current Thread ID: 11 Current Thread ID: 12
Note: The output may vary for the second approach as it is not synchronized and the threads execute concurrently. So the order in which the thread name or thread id is printed may vary. To prevent this problem we used join() method in the first approach. If the order of the printed output is to be maintained then the user can use the join() method.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4