Java中的 LogRecord setThreadID() 方法及示例
Java.util.logging.LogRecord的setThreadID()方法用于设置消息来源线程的标识符。此方法有助于识别生成记录器消息的线程。
句法:
public void setThreadID(int threadID)
参数:该方法接受threadID ,它是整数类型的线程ID。
Return :此方法不返回任何内容。
下面的程序说明了 setThreadID() 方法:
方案一:
// Java program to illustrate
// setThreadID() method
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
// Create LogRecord object
LogRecord logRecord
= new LogRecord(Level.SEVERE,
"Hello Logger");
// set Thread ID
logRecord.setThreadID(53677);
// print Thread ID
System.out.println(
"Thread ID = "
+ logRecord.getThreadID());
}
}
输出:
Thread ID = 53677
方案二:
// Java program to illustrate
// setThreadID() method
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
// start the Thread
Thread1 thread1 = new Thread1();
thread1.start();
// Create LogRecord object
LogRecord logRecord
= new LogRecord(Level.SEVERE,
"Hello Logger");
// set Thread ID
logRecord.setThreadID((int)thread1
.getId());
// print Thread ID
System.out.println(
"Thread ID = "
+ logRecord.getThreadID());
}
}
class Thread1 extends Thread {
public void run()
{
System.out.println("Thread is running...");
}
}
输出:
Thread is running...
Thread ID = 8
参考:
https://docs.oracle.com/javase/10/docs/api/java Java)