Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6202942

(spec thread) ThreadLocal example should use generics and autoboxing

XMLWordPrintable

    • b86
    • generic, x86, sparc
    • generic, linux, solaris_9

      A DESCRIPTION OF THE PROBLEM :
      ThreadLocal is a generic class but the example doesn't use the generics feature or autoboxing. In the existing documentation no type is specified for the ThreadLocal variable and the get method must use a cast to get the Integer and intValue to convert to int.

       

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      public class SerialNum {
           // The next serial number to be assigned
           private static int nextSerialNum = 0;

           private static ThreadLocal<Integer> serialNum = new ThreadLocal<Integer>() {
               protected synchronized Integer initialValue() {
                   return new Integer(nextSerialNum++);
               }
           };

           public static int get() {
               return serialNum.get();
           }
       }
      ACTUAL -
      public class SerialNum {
           // The next serial number to be assigned
           private static int nextSerialNum = 0;

           private static ThreadLocal serialNum = new ThreadLocal() {
               protected synchronized Object initialValue() {
                   return new Integer(nextSerialNum++);
               }
           };

           public static int get() {
               return ((Integer) (serialNum.get())).intValue();
           }
       }

      URL OF FAULTY DOCUMENTATION :
      http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ThreadLocal.html

            psoper Pete Soper (Inactive)
            jssunw Jitender S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: