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
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
- duplicates
-
JDK-6251838 (spec thread) Misleading example usage code in java.lang.ThreadLocal
-
- Closed
-
- relates to
-
JDK-6434084 (doc thread) New ThreadLocal SerialNum example still has problems
-
- Resolved
-
-
JDK-4850726 (spec thread) Example of using the class ThreadLocal doesn't work
-
- Closed
-
-
JDK-4078860 API doc comments should describe the concurrency semantics of classes & methods
-
- Open
-