Home » Java programming language

Java Thread Class static Thread currentThread() method with Example

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

Thread Class static Thread currentThread()

  • This method is available in package java.lang.Thread.currentThread().
  • This method is used to return a reference of the currently executing thread object.
  • This method is static so this method is accessible with classname too.
  • The return type of this method is Thread, it returns a reference of currently executing thread object.
  • This method does not raise any exception.

Syntax:

    static Thread currentThread(){
    }

Parameter(s):

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

Return value:

The return type of this method is Thread, it returns reference of current executing thread.

Java program to demonstrate example of currentThread() 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 Thread1 extends Thread {
    // Override run() of Thread class
    public void run() {
        /*  Display a thread name of current executing thread 
            by using Thread.currentThread().getName()
        */
        System.out.println("The name of this thread is " + " " + Thread.currentThread().getName());
    }
}

class Thread2 extends Thread {
    public void run() {
        /*  Display a thread name of current executing thread by 
            using Thread.currentThread().getName()
        */
        System.out.println("The name of this thread is " + " " + Thread.currentThread().getName());
    }
}

public class MainThread {

    public static void main(String[] args) {

        /*  Display a thread name of current executing thread by 
            using Thread.currentThread().getName()
        */
        System.out.println("The name of this thread is " + " " + Thread.currentThread().getName());

        // Creating Thread1 object
        Thread1 t1 = new Thread1();

        // By using start() Thread class start() will call and 
        // it will call run() of Thread1 class
        t1.start();

        Thread2 t2 = new Thread2();

        // By using start() Thread class start() will call 
        // and it will call run() of Thread2 class
        t2.start();
    }
}

Output

E:\Programs>javac MainThread.java

E:\Programs>java MainThread
The name of this thread is  main
The name of this thread is  Thread-0
The name of this thread is  Thread-1






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.