当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LogRecord getThreadID()用法及代码示例


java.lang.reflect.LogRecord的getThreadID()方法用于获取消息起源线程的标识符。此方法有助于识别生成记录器消息的线程。

用法:

public int getThreadID()

参数:此方法不接受任何内容。


返回:此方法返回线程ID。

以下示例程序旨在说明getThreadID()方法:
示例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) 
    { 
  
        // 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

示例2:

// 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://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#getThreadID()



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 LogRecord getThreadID() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。