[concurrency-interest] Re: Thread.getId()

Bill Pugh pugh@cs.umd.edu
Tue, 27 Jan 2004 14:24:46 -0500


At 1:59 PM -0500 1/27/04, Luke Blanshard wrote:
>Larry Riedel <larryr@saturn.sdsu.edu> wrote:
>
>>... Is there a
>>different way to (nearly) as trivially and efficiently retrieve
>>from a thread a value I can uniquely and efficiently associate
>>in the same way with that thread for the lifespan of the [JVM],
>>regardless of the lifespan of the thread?
>
>You mean, aside from the Thread object itself?

Consider the following problem. You want to generate a log showing 
each write to a variable and the thread that performed the write. For 
logging purposes, you would need to have a unique, non-reused id for 
each thread.

Since it sounds like making Thread.getId() return unique values is 
impossible, it seems that the best solution is to have a WeakHashMap 
from threads to threadIds (perhaps Integer or Long objects). Not as 
simple as using Thread.getId(), but not too bad.


Bill