Duplicate :
|
FULL PRODUCT VERSION : ADDITIONAL OS VERSION INFORMATION : Windows 7 64 bit A DESCRIPTION OF THE PROBLEM : For example, the class below generates unique identifiers local to each thread. A thread's id is assigned the first time it invokes UniqueThreadIdGenerator.getCurrentThreadId() and remains unchanged on subsequent calls. import java.util.concurrent.atomic.AtomicInteger; public class UniqueThreadIdGenerator { private static final AtomicInteger uniqueId = new AtomicInteger(0); private static final ThreadLocal < Integer > uniqueNum = new ThreadLocal < Integer > () { @Override protected Integer initialValue() { return uniqueId.getAndIncrement(); } }; public static int getCurrentThreadId() { return uniqueId.get(); /*** Should be uniqueNum.get() ***/ } } // UniqueThreadIdGenerator REPRODUCIBILITY : This bug can be reproduced always.