1 Reply Latest reply: Nov 29, 2011 10:38 PM by EJP RSS

    BUG in ThreadLocal Example

    902851
      Hi,
      I just spot a bug in ThreadLocal example demonstrated in the JAVA API doc.

      here is the code snippet:

      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();
      }
      }

      where the getCurrentThreadId() method should be change to this for it to work:

      public static int getCurrentThreadId() {
      return uniqueNum.get();
      }

      Edited by: 899848 on Nov 29, 2011 8:10 PM