JDK-6202942 : (spec thread) ThreadLocal example should use generics and autoboxing
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0,6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,linux,solaris_9
  • CPU: generic,x86,sparc
  • Submitted: 2004-12-02
  • Updated: 2010-04-02
  • Resolved: 2006-05-30
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 b86Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
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

Comments
EVALUATION These improvements will be combined with the doc fix of 6251838 by adding doc of the need for "synchronized" for the example's initialValue decl. So 6251838 will become a dupe of this older change request. ###@###.### 2005-04-27 15:50:32 GMT
27-04-2005