Wave Fronts

« Previous day (Sep 26, 2006) | Main | Next day (Sep 27, 2006) »

http://blogs.sun.com/microwaves/date/20060927 Wednesday September 27, 2006

java.lang.ThreadLocal UniqueThreadIdGenerator example FIX

A typo crept into the new ThreadLocal example class somehow. Here's the code as it SHOULD be:

 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 uniqueNum.get(); // CORRECT
     }
 } // UniqueThreadIdGenerator

The INCORRECT example code that escaped into Java SE 6 invokes uniqueId.get instead.

This problem was apparently caused by the fact that the unit test I used to test the putback "wasn't good enough" and the fancy replacement's debugging collided with time constraints for the release. I guess I put the typo in at the perfect time to shift mistrust to the new unit test. There's an open bug for the latter and there will soon be an open bug for this last minute botch. There should be no problem getting this example fixed for U1.


Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.