java.util.logging.LogRecord的setThreadID()方法用于为消息所在的线程设置标识符。此方法有助于识别生成记录器消息的线程。
用法:
public void setThreadID(int threadID)
参数:此方法接受threadID,它是整数类型的线程ID。
返回:此方法不返回任何内容。
以下示例程序旨在说明setThreadID()方法:
示例1:
// 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
示例2:
// 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/util/logging/LogRecord.html#setThreadID(int)
相关用法
- Java LogRecord getSourceMethodName()用法及代码示例
- Java LogRecord setSequenceNumber()用法及代码示例
- Java LogRecord getMessage()用法及代码示例
- Java LogRecord getInstant()用法及代码示例
- Java LogRecord getThreadID()用法及代码示例
- Java LogRecord getSourceClassName()用法及代码示例
- Java LogRecord getParameters()用法及代码示例
- Java LogRecord getSequenceNumber()用法及代码示例
- Java LogRecord setMillis()用法及代码示例
- Java LogRecord setParameters()用法及代码示例
- Java LogRecord setResourceBundle()用法及代码示例
- Java LogRecord setResourceBundleName()用法及代码示例
- Java LogRecord getLoggerName()用法及代码示例
- Java LogRecord setLoggerName()用法及代码示例
- Java LogRecord setLevel()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 LogRecord setThreadID() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。