Java.lang.Thread.currentThread() Method


Advertisements


Description

The java.lang.Thread.currentThread() method returns a reference to the currently executing thread object.

Declaration

Following is the declaration for java.lang.Thread.currentThread() method

public static Thread currentThread()

Parameters

NA

Return Value

This method returns the currently executing thread.

Exception

NA

Example

The following example shows the usage of java.lang.Thread.currentThread() method.

package com.tutorialspoint;

import java.lang.*;

public class ThreadDemo implements Runnable {

   ThreadDemo() {
      // main thread
      Thread currThread = Thread.currentThread();
      
      // thread created
      Thread t = new Thread(this, "Admin Thread");
   
      System.out.println("current thread = " + currThread);
      System.out.println("thread created = " + t);
      
      // this will call run() function
      t.start();
   }

   public void run() {
      System.out.println("This is run() method");
   }

   public static void main(String args[]) {
      new ThreadDemo();
   }
} 

Let us compile and run the above program, this will produce the following result −

current thread = Thread[main,5,main]
thread created = Thread[Admin Thread,5,main]
This is run() method

java_lang_thread.htm

Advertisements