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

Larry Riedel larryr@saturn.sdsu.edu
27 Jan 2004 20:15:12 -0000

> > ... 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?

Like use weak/soft references and Reference.get() and "=="?
My feeling is that weak references are cumbersome enough to
introduce a higher level of complexity than using a numeric
primitive would in a context where the cost of using ThreadLocal
is considered too high in terms of complexity and performance,
where even the cost of an unsynchronized method call is not
considered negligible, and methods are invoked on the order of
a million of times per second.

Having a Thread.getId() which gives me a primitive value
guaranteed to uniquely identify that thread forever and is
implemented as something extremely close to "return this.id",
seems to me fairly compelling as far as cost/benefit in terms
of performance and complexity for something which comes for
free with the JDK.  Anything to do with weak references or
ThreadLocal values or which does not guarantee a unique value
forever seems like a quantum change of performance/complexity
cost/benefit which may make trying to distinguish one thread
from another not worth it.