diff --git a/src/share/classes/java/util/logging/LogRecord.java b/src/share/classes/java/util/logging/LogRecord.java index 6eda211f1115f6520594597617f8f514aff56952..0acf9fa78cde225d2b36b3c77c08f7d2b47e215f 100644 --- a/src/share/classes/java/util/logging/LogRecord.java +++ b/src/share/classes/java/util/logging/LogRecord.java @@ -25,6 +25,8 @@ package java.util.logging; import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import java.io.*; /** @@ -64,9 +66,24 @@ import java.io.*; */ public class LogRecord implements java.io.Serializable { - private static long globalSequenceNumber; - private static int nextThreadId=10; - private static ThreadLocal threadIds = new ThreadLocal(); + private static final AtomicLong globalSequenceNumber + = new AtomicLong(0); + + /** + * The default value of threadID will be the current thread's + * thread id, for ease of correlation, unless it is greater than + * MIN_SEQUENTIAL_THREAD_ID, in which case we try harder to keep + * our promise to keep threadIDs unique by avoiding collisions due + * to 32-bit wraparound. Unfortunately, LogRecord.getThreadID() + * returns int, while Thread.getId() returns long. + */ + private static final int MIN_SEQUENTIAL_THREAD_ID = Integer.MAX_VALUE / 2; + + private static final AtomicInteger nextThreadId + = new AtomicInteger(MIN_SEQUENTIAL_THREAD_ID); + + private static final ThreadLocal threadIds + = new ThreadLocal(); /** * @serial Logging message level @@ -122,6 +139,23 @@ public class LogRecord implements java.io.Serializable { private transient Object parameters[]; private transient ResourceBundle resourceBundle; + /** + * Returns the default value for a new LogRecord's threadID. + */ + private int defaultThreadID() { + long tid = Thread.currentThread().getId(); + if (tid < MIN_SEQUENTIAL_THREAD_ID) { + return (int) tid; + } else { + Integer id = threadIds.get(); + if (id == null) { + id = nextThreadId.getAndIncrement(); + threadIds.set(id); + } + return id; + } + } + /** * Construct a LogRecord with the given level and message values. *

@@ -144,15 +178,8 @@ public class LogRecord implements java.io.Serializable { this.level = level; message = msg; // Assign a thread ID and a unique sequence number. - synchronized (LogRecord.class) { - sequenceNumber = globalSequenceNumber++; - Integer id = threadIds.get(); - if (id == null) { - id = new Integer(nextThreadId++); - threadIds.set(id); - } - threadID = id.intValue(); - } + sequenceNumber = globalSequenceNumber.getAndIncrement(); + threadID = defaultThreadID(); millis = System.currentTimeMillis(); needToInferCaller = true; } diff --git a/test/java/util/logging/LoggerSubclass.java b/test/java/util/logging/LoggerSubclass.java index bd71f11a56669a31326a507d1006c870f260a8c5..ba18fa36b2e4dd46a272a4890f8c4214808f807f 100644 --- a/test/java/util/logging/LoggerSubclass.java +++ b/test/java/util/logging/LoggerSubclass.java @@ -23,7 +23,7 @@ /* * @test - * @bug 6830220 + * @bug 6830220 6278014 * @summary Test Logger subclasses */ @@ -68,6 +68,8 @@ public class LoggerSubclass { l.getSequenceNumber()); equal(lastThreadID.get(), l.getThreadID()); + equal((int) Thread.currentThread().getId(), + l.getThreadID()); } lastSequenceNumber.set(l.getSequenceNumber()); lastThreadID.set(l.getThreadID());