Get Thread Id in Java

Get Thread Id in Java

In this article, we will learn to get thread id of a running thread in Java.

An Id is a unique positive number generated at the time of thread creation. This id remains unchanged during the lifetime of the thread. When a thread gets terminated its id can be used to refer another thread, but two threads can not have same id number at the same time.

To get thread id, Java Thread class provides a method getId() that returns a long type value as id number.
The syntax of the method is given below:

Get Thread Id in Java

In this example, we created and started a thread. After that, by using getId() method, we get thread id. See the example below.

Output

0
1
2
3
4
Thread id : 11

Get Thread Id of Current Running Thread

If we want to get id of current runnig thread, then by using the currentThread() method we can call getId() method. See the example below.

Output

0
1
2
3
4
Thread id : 1
Thread id : 11

Get Thread id of Multiple Threads

Java allows to create multiple threads, in that case we can get threat id of individual threat by calling getId() method. See the example below.

Output

0
1
2
3
4
Thread1 id : 11
Thread2 id : 12
Thread3 id : 13

That’s all about how to get Thread Id in Java.


import_contacts

You may also like:


import_contacts

You may also like:

Related Posts

  • ArrayBlockingQueue in java
    19 September

    ArrayBlockingQueue in java

    Table of ContentsWhat is BlockingQueueArrayBlockingQueueFeaturesConstructorsMethodsUsage ScenariosImplementation CodeSummary In this article, we will understand the Java concurrent queue, BlockingQueue. We will then go deep into it’s one of the implementation, ArrayBlockingQueue. What is BlockingQueue BlockingQueue interface was introduced in Java 5 under concurrent APIs and it represents a thread-safe queue in which elements can be added […]

  • Delay java program by few seconds
    11 December

    Java wait seconds or delay Java program for few secs

    Table of ContentsUsing Thread.sleepUsing TimeUnit.XXX.sleep methodUsing ScheduledExecutorServiceFrequently asked questions on Java wait secondsHow to wait for 5 seconds in java?How to wait for 1 seconds in java?How to pause for 5 seconds in java? In this post, we will see how to delay java program for few secs or wait for seconds for java program […]

  • 29 May

    How to print even and odd numbers using threads in java

    Table of ContentsProblemSolution 1Print even and odd numbers using threads in java Solution 2: Using remainder In this post, we will see how to print even and odd numbers using threads in java. see also: How to print sequence using 3 threads in java Problem You are given two threads. You need to print odd […]

  • wait(),notify() and notifyAll() in java
    22 October

    Why wait(), notify() And notifyAll() methods are in Object Class

    In this post, we will see why wait(), notify() And notifyAll() methods are in Object Class And Not in Thread Class. This is one of the most asked java multithreading interview questions. You might know that wait(), notify() And notifyAll() Methods are in Object class and do you know the reason for the same? Let’s […]

  • Custom BlockingQueue in java
    16 October

    Custom BlockingQueue implementation in java

    In this post, we will see how to create your own custom BlockingQueue. This is one of the most asked java interview questions. You need to implement your own BlockingQueue. This question helps interviewer to get your understanding of multithreading concepts. Here is simple implementation of BlockingQueue. We will use array to store elements in […]

  • Difference between Runnable and Callable in java
    16 December

    Difference between Runnable and Callable in java

    Runnable and Callable interface both are used in the multithreading environment.Callable is available in java.util.concurrent.Callable package and Runnable in java.lang.Thread. Difference between Runnable and Callable interface in java Runnable was introduced in java 1.0 version While Callable is an extended version of Runnable and introduced in java 1.5 to address the limitation of Runnable. Runnable […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.