Duplicate :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
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
|