Thread
class in JavaFrom the java.lang*
package, Thread
is a major class in Java on which a multithreading system is based.
In Java, we can use this class to process and work with threads and runnable interface.
getid()
methodThe getid()
function from the Thread
class is used to extract a unique thread ID. This unique identifier is assigned when we create a new thread in our program.
long getId()
This function does not accept parameter values.
Thread_ID
: It returns this
thread ID of primitive long type.In the code snippet below, we have two threads: one is EdPresso
(main thread), and another is thread
in line 10.
We are using a runnable interface for the proper execution of threads. These threads have their unique IDs associated with them.
Execute this code to check unique identifiers or thread IDs on the console.
// Load Thread class from// java.lang packageimport java.lang.Thread;// main programpublic class EdPresso {public static void main(String[] args) {System.out.println("Main thread ID: "+ Thread.currentThread().getId());Thread thread = new Thread(new EdPresso().new Runnabledemo());thread.start();}// runnable interface classprivate class Runnabledemo implements Runnable {public void run(){System.out.println("In run() method: " + Thread.currentThread().getId());}}}