當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java LogRecord setThreadID()用法及代碼示例


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)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LogRecord setThreadID() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。