HomeLanguagesJavaLogRecord setThreadID() method in Java with Examples

LogRecord setThreadID() method in Java with Examples

The setThreadID() method of java.util.logging.LogRecord is used to set an identifier for the thread where the message originated. This method is helpful to identify thread which generates the logger message.

Syntax:

public void setThreadID(int threadID)

Parameters: This method accepts threadID which is the thread ID of integer type.

Return: This method returns nothing.

Below programs illustrate setThreadID() method:
Program 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());
    }
}


Output:

Thread ID = 53677

Program 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...");
    }
}


Output:

Thread is running...
Thread ID = 8

References:
https://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#setThreadID(int)

RELATED ARTICLES

7 COMMENTS

Most Popular

Dominic
32548 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6923 POSTS0 COMMENTS
Nicole Veronica
12039 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12150 POSTS0 COMMENTS
Shaida Kate Naidoo
7060 POSTS0 COMMENTS
Ted Musemwa
7302 POSTS0 COMMENTS
Thapelo Manthata
7018 POSTS0 COMMENTS
Umr Jansen
7006 POSTS0 COMMENTS