Home » Java programming language

Java Thread Class long getId() method with Example

Java Thread Class long getId() method: Here, we are going to learn about the long getId() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 19, 2019

Thread Class long getId()

  • This method is available in package java.lang.Thread.getId().
  • This method is used to return an Id (Identifier) for this thread.
  • This method is not static so this method is accessible with Thread class object it is not accessible with the class name.
  • Every Thread has a unique id and creates id at the thread instantiation.
  • We cannot change Id of the Thread even when the thread terminates and Id may reuse again.
  • This method does not raise any exception.

Syntax:

    long getId(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the Thread.

Return value:

The return type of this method is long, it returns the Id for this thread and Id will be long integer type.

Java program to demonstrate example of getId() method

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/
import java.lang.Thread;

class GetThreadId extends Thread {
    // Override run() of Thread class
    public void run() {
        System.out.println("The priority of this thread is : " + Thread.currentThread().getPriority());
    }

    public static void main(String[] args) {
        // Creating an object of GetThreadId class
        GetThreadId gt_id = new GetThreadId();

        // Calling start() method with GetThreadId class 
        // object of Thread class
        gt_id.start();

        // Get name of a thread th and display on standard 
        // output stream
        System.out.println("The name of this thread is " + " " + gt_id.getName());

        // Get Id of a thread th and display on standard 
        // output stream
        System.out.println("The Id of this thread is " + " " + gt_id.getId());
    }
}

Output

E:\Programs>javac GetThreadId.java

E:\Programs>java GetThreadId
The priority of this thread is :5
The name of this thread is  Thread-0
The Id of this thread is  9






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.