JDK-4850726 : (spec thread) Example of using the class ThreadLocal doesn't work
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-04-18
  • Updated: 2006-03-23
  • Resolved: 2006-03-23
Related Reports
Relates :  
Relates :  
Description
Name: rl43681			Date: 04/18/2003


A DESCRIPTION OF THE PROBLEM :
In documentation, there is written (copy of this text is in EditBox bellow) that class SerialNum should return specific value for each thread. But it's returned only 0 (how expected, if you read the code of this class:-)). Problem is, that this class can't have "static" instance of ThreadLocal, because this class (which is actually called in method SerialNum.get()) remember only 0. If you want to correct this example, the method SerialNum.get() and attribute SerialNum.serialnum can't be static and each thread must create new instance of class SerialNum (There is one more bug in this class, this class has only static methods, so it has probably also private default constructor to nobody can make instance of this class - but it's not so important)



EXPECTED VERSUS ACTUAL BEHAVIOR :
some other example which works:-) but for e.g. this(but it's not good example, because the same functionality can be probably done easier without using class ThreadLocal)
my correction:

For example, in the class below, the private ThreadLocal instance (serialNum) maintains a "serial number" for each thread that creates an instance of class SerialNum and invokes the instance's SerialNum.get() method, which returns the current thread's serial number. (A thread's serial number is assigned the first time it invokes SerialNum.get(), and remains unchanged on subsequent calls.)

public class SerialNum {
     // The next serial number to be assigned
     private static int nextSerialNum = 0;
 
     private ThreadLocal serialNum = new ThreadLocal() {
         protected synchronized Object initialValue() {
             return new Integer(nextSerialNum++);
         }
     };
 
     public int get() {
         return ((Integer) (serialNum.get())).intValue();
     }
 }

For example, in the class below, the private static ThreadLocal instance (serialNum) maintains a "serial number" for each thread that invokes the class's static SerialNum.get() method, which returns the current thread's serial number. (A thread's serial number is assigned the first time it invokes SerialNum.get(), and remains unchanged on subsequent calls.)

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.4.2/docs/api/java/lang/ThreadLocal.html
(Review ID: 184093) 
======================================================================

Comments
EVALUATION The example is correct and does the right thing. Here's an example of usage and what the output looks like: /** * Demonstrate use of SerialNum */ public class TestSerialNum extends Thread { public void run() { System.out.println("I am thread: " + Thread.currentThread().getName() + " and my number is: " + (new SerialNum()).get()); } public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 10; i++ ) { TestSerialNum t = new TestSerialNum(); t.start(); t.join(); } } } ebear >java TestSerialNum I am thread: Thread-0 and my number is: 0 I am thread: Thread-1 and my number is: 1 I am thread: Thread-2 and my number is: 2 I am thread: Thread-3 and my number is: 3 I am thread: Thread-4 and my number is: 4 I am thread: Thread-5 and my number is: 5 I am thread: Thread-6 and my number is: 6 I am thread: Thread-7 and my number is: 7 I am thread: Thread-8 and my number is: 8 I am thread: Thread-9 and my number is: 9 There is thread local memory holding each thread's serial number. This memory is in a hidden data structure that is effectively part of the Thread instance context. The static is only used to dispense values (and notice it's access is within a synchronized method).
23-03-2006

EVALUATION Name: dk30142 Date: 04/18/2003 Accepted ======================================================================
11-06-2004