[concurrency-interest] Ensure Callable.call is invoked fromconstructing thread?

Kris Schneider kris at dotech.com
Fri Jun 9 14:46:22 EDT 2006


Thomas Hawtin wrote:
> Kris Schneider wrote:
> 
>>I need to ensure that a Callable is called from the same thread within 
>>which it was constructed:
> 
> 
>>The two approaches that immediately came to mind were to use a reference 
>>object or System.identityHashCode(Thread.currentThread()). Any thoughts on 
>>which might be better or if there are other approaches that are better 
>>still? Thanks.
> 
> 
> System.identityHashCode does not produce unique values. A very common 
> mistake - the documentation is a bit poor.

Nice. Good to know.

> I guess Thread.currentThread().getId() should work (since 1.5), as 
> should new WeakReference<Thread>(Thread.currentThread()).

This has to work on 1.4.2, so no go with getId. It looks like the ID of a 
terminated thread can be reused, so I'd probably steer clear of that even 
for 1.5. So, that leaves ref object and ThreadLocal (per Tim). Thanks to 
both of you for the feedback so far.

> Tom Hawtin

-- 
Kris Schneider <mailto:kris at dotech.com>
D.O.Tech       <http://www.dotech.com/>


More information about the Concurrency-interest mailing list