[concurrency-interest] Need one more reviewer, please Re: new SerialNum example for ThreadLocal class doc

Pete Soper Pete.Soper at Sun.COM
Fri Jun 9 16:46:25 EDT 2006


Thanks for all the high quality input. I will send a 'final' version for 
review but I have to finish the unit test and I picked a bad time to 
come up to speed on NetBeans (but it is *absolutely fabulous* and I 
haven't had such a cool tool as this in a long time).

But here's the "current" version sans "*" and HTML escapes.
Josh: I'm using latest jstyle and will get that sorted if this version 
isn't right yet. Thanks for the gentle feedback.
Brian: I will get back to that other example with you soonest.

(again, comments directly to me, please)

I should also note that because the example starting this review was a 
"suggested fix" for a bug I'm not constrained by Sun licensing 
restrictions. It would not be wise to cut and paste source out of the 
JDk and do this kind of thing (at least not yet!).

-Pete
--------------
import java.util.concurrent.atomic.AtomicInteger;

public class UniqueThreadIdGenerator {

     private static final AtomicInteger i = new AtomicInteger(0);

     private static final ThreadLocal<Integer> uniqueNum =
         new ThreadLocal<Integer>() {
             @Override protected Integer initialValue() {
                 return i.getAndIncrement();
             }
     };

     public static int getCurrentThreadId() {
         return i.get();
     }
} // UniqueThreadIdGenerator


More information about the Concurrency-interest mailing list