JDK-6434084 : (doc thread) New ThreadLocal SerialNum example still has problems
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6,6u23,6u24
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,linux,windows_7
  • CPU: generic,x86
  • Submitted: 2006-06-05
  • Updated: 2011-03-14
  • Resolved: 2006-07-01
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b91Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description
6202942 rfe 4 18M 1  Fix Delivered pete.soper java/lang (spec thread) ThreadLocal example should use generics and autoboxing

Comment added by    (###@###.###) : 
Wouldn't it be slightly better for the example to use getAndIncrement() rather than getAndAdd(1)? And a few finals wouldn't go amiss. And an @Override.

Oh and the HTML is slightly wrong, The  disappears, so the example makes even less sense than it did beforehand. {@code } would make source and docs readable.

Comments
EVALUATION Doc changes in place, exercise ("unit") test being developed.
24-06-2006

EVALUATION This will be ready for review soon.
06-06-2006

SUGGESTED FIX I have attached and copied below a short patch. I didn't use {@code } because the JavaDoc parser doesn't seem to be able to cope with the { @Override. It uses &lt; and &#64; which is a bit ugly but works. I thought about wrapping the ThreadLocal line, but it looks fine as it is on the HTML page. j2se/src/share/classes/java/lang/ThreadLocal.java 2006-06-04 08:23:52.000000000 +0100 +++ java/lang/ThreadLocal.java 2006-06-06 10:50:33.000000000 +0100 @@ -25,23 +25,23 @@ * unchanged on subsequent calls.) * <pre> * import java.util.concurrent.atomic.AtomicInteger; + * * public class SerialNum { * * // The next serial number to be assigned. Starts at zero. - * private static AtomicInteger nextSerialNum = new AtomicInteger(); + * private static final AtomicInteger nextSerialNum = new AtomicInteger(); * - * private static ThreadLocal<Integer> serialNum = new ThreadLocal<Integer>() { - * + * private static final ThreadLocal&lt;Integer> serialNum = new ThreadLocal&lt;Integer>() { + * &#64;Override * protected Integer initialValue() { - * return nextSerialNum.getAndAdd(1); + * return nextSerialNum.getAndIncrement(); * } * }; * * public static int get() { * return serialNum.get(); * } - * } - * </pre> + * }</pre> * * <p>Each thread holds an implicit reference to its copy of a thread-local * variable as long as the thread is alive and the <tt>ThreadLocal</tt> --- j2se/src/share/classes/java/lang/ThreadLocal.java 2006-06-04 08:23:52.000000000 +0100 +++ java/lang/ThreadLocal.java 2006-06-06 10:50:33.000000000 +0100 @@ -25,23 +25,23 @@ * unchanged on subsequent calls.) * <pre> * import java.util.concurrent.atomic.AtomicInteger; + * * public class SerialNum { * * // The next serial number to be assigned. Starts at zero. - * private static AtomicInteger nextSerialNum = new AtomicInteger(); + * private static final AtomicInteger nextSerialNum = new AtomicInteger(); * - * private static ThreadLocal<Integer> serialNum = new ThreadLocal<Integer>() { - * + * private static final ThreadLocal&lt;Integer> serialNum = new ThreadLocal&lt;Integer>() { + * &#64;Override * protected Integer initialValue() { - * return nextSerialNum.getAndAdd(1); + * return nextSerialNum.getAndIncrement(); * } * }; * * public static int get() { * return serialNum.get(); * } - * } - * </pre> + * }</pre> * * <p>Each thread holds an implicit reference to its copy of a thread-local * variable as long as the thread is alive and the <tt>ThreadLocal</tt>
06-06-2006

EVALUATION These fixes all seem valid.
05-06-2006