LogRecord getThreadID() method in Java with Examples
Last Updated :
23 Oct, 2019
The getThreadID() method of java.lang.reflect.LogRecord is used to get an identifier for the thread where the message originated. This method is helpful to identify thread which generates the logger message.
Syntax:
public int getThreadID()
Parameters: This method accepts nothing.
Return: This method returns thread ID.
Below programs illustrate getThreadID() method:
Program 1:
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
LogRecord logRecord
= new LogRecord(Level.SEVERE,
"Hello Logger" );
int id = logRecord.getThreadID();
System.out.println(
"Thread ID = "
+ id);
}
}
|
Program 2:
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
Thread thread1 = new Thread1();
thread1.start();
LogRecord logRecord
= new LogRecord(Level.SEVERE,
"Hello Logger" );
logRecord.setThreadID(( int )thread1
.getId());
int id = logRecord.getThreadID();
System.out.println(
"Thread ID = "
+ id);
}
}
class Thread1 extends Thread {
public void run()
{
System.out.println( "Thread is running..." );
}
}
|
Output:
Thread is running...
Thread ID = 11
References: https://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#getThreadID()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...