jdk/src/share/classes/java/util/logging/LogRecord.java
changeset 2632 9779b9cbae42
parent 2 90ce3da70b43
child 2947 b0135c99348e
equal deleted inserted replaced
2631:29398881792c 2632:9779b9cbae42
    23  * have any questions.
    23  * have any questions.
    24  */
    24  */
    25 
    25 
    26 package java.util.logging;
    26 package java.util.logging;
    27 import java.util.*;
    27 import java.util.*;
       
    28 import java.util.concurrent.atomic.AtomicInteger;
       
    29 import java.util.concurrent.atomic.AtomicLong;
    28 import java.io.*;
    30 import java.io.*;
    29 
    31 
    30 /**
    32 /**
    31  * LogRecord objects are used to pass logging requests between
    33  * LogRecord objects are used to pass logging requests between
    32  * the logging framework and individual log Handlers.
    34  * the logging framework and individual log Handlers.
    62  *
    64  *
    63  * @since 1.4
    65  * @since 1.4
    64  */
    66  */
    65 
    67 
    66 public class LogRecord implements java.io.Serializable {
    68 public class LogRecord implements java.io.Serializable {
    67     private static long globalSequenceNumber;
    69     private static final AtomicLong globalSequenceNumber
    68     private static int nextThreadId=10;
    70         = new AtomicLong(0);
    69     private static ThreadLocal<Integer> threadIds = new ThreadLocal<Integer>();
    71 
       
    72     /**
       
    73      * The default value of threadID will be the current thread's
       
    74      * thread id, for ease of correlation, unless it is greater than
       
    75      * MIN_SEQUENTIAL_THREAD_ID, in which case we try harder to keep
       
    76      * our promise to keep threadIDs unique by avoiding collisions due
       
    77      * to 32-bit wraparound.  Unfortunately, LogRecord.getThreadID()
       
    78      * returns int, while Thread.getId() returns long.
       
    79      */
       
    80     private static final int MIN_SEQUENTIAL_THREAD_ID = Integer.MAX_VALUE / 2;
       
    81 
       
    82     private static final AtomicInteger nextThreadId
       
    83         = new AtomicInteger(MIN_SEQUENTIAL_THREAD_ID);
       
    84 
       
    85     private static final ThreadLocal<Integer> threadIds
       
    86         = new ThreadLocal<Integer>();
    70 
    87 
    71     /**
    88     /**
    72      * @serial Logging message level
    89      * @serial Logging message level
    73      */
    90      */
    74     private Level level;
    91     private Level level;
   119     private String resourceBundleName;
   136     private String resourceBundleName;
   120 
   137 
   121     private transient boolean needToInferCaller;
   138     private transient boolean needToInferCaller;
   122     private transient Object parameters[];
   139     private transient Object parameters[];
   123     private transient ResourceBundle resourceBundle;
   140     private transient ResourceBundle resourceBundle;
       
   141 
       
   142     /**
       
   143      * Returns the default value for a new LogRecord's threadID.
       
   144      */
       
   145     private int defaultThreadID() {
       
   146         long tid = Thread.currentThread().getId();
       
   147         if (tid < MIN_SEQUENTIAL_THREAD_ID) {
       
   148             return (int) tid;
       
   149         } else {
       
   150             Integer id = threadIds.get();
       
   151             if (id == null) {
       
   152                 id = nextThreadId.getAndIncrement();
       
   153                 threadIds.set(id);
       
   154             }
       
   155             return id;
       
   156         }
       
   157     }
   124 
   158 
   125     /**
   159     /**
   126      * Construct a LogRecord with the given level and message values.
   160      * Construct a LogRecord with the given level and message values.
   127      * <p>
   161      * <p>
   128      * The sequence property will be initialized with a new unique value.
   162      * The sequence property will be initialized with a new unique value.
   142         // Make sure level isn't null, by calling random method.
   176         // Make sure level isn't null, by calling random method.
   143         level.getClass();
   177         level.getClass();
   144         this.level = level;
   178         this.level = level;
   145         message = msg;
   179         message = msg;
   146         // Assign a thread ID and a unique sequence number.
   180         // Assign a thread ID and a unique sequence number.
   147         synchronized (LogRecord.class) {
   181         sequenceNumber = globalSequenceNumber.getAndIncrement();
   148             sequenceNumber = globalSequenceNumber++;
   182         threadID = defaultThreadID();
   149             Integer id = threadIds.get();
       
   150             if (id == null) {
       
   151                 id = new Integer(nextThreadId++);
       
   152                 threadIds.set(id);
       
   153             }
       
   154             threadID = id.intValue();
       
   155         }
       
   156         millis = System.currentTimeMillis();
   183         millis = System.currentTimeMillis();
   157         needToInferCaller = true;
   184         needToInferCaller = true;
   158    }
   185    }
   159 
   186 
   160     /**
   187     /**