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