[concurrency-interest] new SerialNum example for ThreadLocal class doc

Pete Soper Pete.Soper at Sun.COM
Wed Jun 7 17:13:38 EDT 2006

Recently the "SerialNum" example class in the java.lang.ThreadLocal doc 
was upgraded to use autoboxing and generics and to avoid unclear use of 
"synchronized." We (some JSR166 expert group members and I) thought this 
new version looked pretty good, but Tom Hawtin thought otherwise, 
pointing out the value of using @Overload, "final" and 
getAndIncrement(). That he found three ways to improve this was a bit of 
a surprise and suggests the more eyes the better, so I'm asking for any 
additional comments for the new version listed below. This is the raw 
text and the escape sequences of course display as "<" and "@" as HTML. 
Note also the two long lines format fine despite the autowrap your mail 
client might show below.


  * <pre>
  * import java.util.concurrent.atomic.AtomicInteger;
  * public class SerialNum {
  *     // The next serial number to be assigned. Starts at zero.
  *     private static final AtomicInteger nextSerialNum = new 
  *     private static final ThreadLocal&lt;Integer> serialNum = new 
ThreadLocal&lt;Integer>() {
  *         &#64;Override
  *         protected Integer initialValue() {
  *             return nextSerialNum.getAndIncrement();
  *         }
  *     };
  *     public static int get() {
  *         return serialNum.get();
  *     }
  * }</pre>

More information about the Concurrency-interest mailing list