threads

Current Thread Information

In this example we shall show you how to get the current Thread‘s information, such as id, name group and priority. To get the current Thread’s information one should perform the following steps:

  • Get a reference to the currently executing thread, using currentThread() API method of Thread.
  • Get the identifier of this Thread, its name, the thread group to which this thread belongs and its priority, using getId(), getName(), getThreadGroup() and getPriority() API methods of Thread,

as described in the code snippet below.  

package com.javacodegeeks.snippets.core;

public class CurrentThreadExample {
	
	public static void main(String[] args) {
		
		Thread thread = Thread.currentThread();
		System.out.println("Thread: " + thread);
		System.out.println("Thread Id: " + thread.getId());
		System.out.println("Thread Name: " + thread.getName());
		System.out.println("Thread Group: " + thread.getThreadGroup());
		System.out.println("Thread Priority: " + thread.getPriority());
		
	}

}

Output:

Thread: Thread[main,5,main]
Thread Id: 1
Thread Name: main
Thread Group: java.lang.ThreadGroup[name=main,maxpri=10]
Thread Priority: 5

  
This was an example of how to get the current Thread’s information in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button