This discussion is archived
1 Reply Latest reply: Nov 29, 2011 8:38 PM by EJP RSS

BUG in ThreadLocal Example

902851 Newbie
Currently Being Moderated
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

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points