Get Current Thread Example Java Program


Definition

A thread in computer science is short for a thread of execution. Threads are a way for a program to divide (termed "split") itself into two or more simultaneously (or pseudo-simultaneously) running tasks.

Syntax

Thread currentThread()

Get Current Thread Example Program

import java.lang.*;

public class GetCurrentThread implements Runnable {
	GetCurrentThread() {
	Thread t1 = Thread.currentThread();
	Thread t2 = new Thread(this, "Main Thread");
	System.out.println("current thread = " + t1);
	System.out.println("new thread = " + t2);
	t2.start();
	}
	public void run() {
	System.out.println("I am inside run method");
	}
	public static void main(String args[]) {
	new GetCurrentThread();
	}
} 

Sample Output

Output is:
current thread = Thread[main,5,main]
new thread = Thread[Main Thread,5,main]
I am inside run method