Java中的 LogRecord getThreadID() 方法及示例
Java.lang.reflect.LogRecord的getThreadID()方法用于获取消息来源线程的标识符。此方法有助于识别生成记录器消息的线程。
句法:
public int getThreadID()
参数:此方法不接受任何内容。
返回:此方法返回线程 ID 。
下面的程序说明了 getThreadID() 方法:
方案一:
// Java program to illustrate
// getThreadID() 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");
// get Thread ID
int id = logRecord.getThreadID();
// print
System.out.println(
"Thread ID = "
+ id);
}
}
输出:
Thread ID = 1
方案二:
// Java program to illustrate
// getThreadID() method
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
// start the Thread
Thread thread1 = new Thread1();
thread1.start();
// Create LogRecord object
LogRecord logRecord
= new LogRecord(Level.SEVERE,
"Hello Logger");
logRecord.setThreadID((int)thread1
.getId());
// get Thread ID
int id = logRecord.getThreadID();
// print
System.out.println(
"Thread ID = "
+ id);
}
}
class Thread1 extends Thread {
public void run()
{
System.out.println("Thread is running...");
}
}
输出:
Thread is running...
Thread ID = 11
参考资料: https: Java/util/logging/LogRecord.html#getThreadID()