0

Java Doc

 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() {
         // uniqueId is right? or is uniqueNum ?
         return uniqueId.get();
     }
 } // UniqueThreadIdGenerator

getCurrentThreadId() uniqueId is right?I think uniqueNum is right

learn from each other about Threadlocal~~

StephenQu
  • 9
  • 1
  • You want `uniqueNum.get()`. `uniqueId.get()` will just give you the next ID. – shmosel Nov 04 '22 at 05:42
  • Oh, I see, it's a mistake in the JavaDoc. Looks like it was corrected in [Java 7](https://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html). – shmosel Nov 04 '22 at 05:44
  • @shmosel thank you, If you'd like to add it as an answer I'll be happy to accept it. – StephenQu Nov 04 '22 at 07:26

0 Answers0