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

Jan Nielsen Jan.Nielsen at sungardhe.com
Fri Jun 9 15:37:58 EDT 2006


For what it's worth, I use a ThreadLocal for this type of problem in 
several areas of my code; I don't see any performance problems with 
ThreadLocal in my use-cases.


Jan Nielsen * System Architect * SunGard Higher Education * Tel +1 801 257 
4155 * Fax +1 801 485 6606 * jan.nielsen at sungardhe.com * www.sungardhe.com 
* 90 South 400 West, Suite 500, Salt Lake City, UT USA

CONFIDENTIALITY: This email (including any attachments) may contain 
confidential, proprietary and privileged information, and unauthorized 
disclosure or use is prohibited.  If you received this email in error, 
please notify the sender and delete this email from your system. Thank 

Kris Schneider <kris at dotech.com> 
Sent by: concurrency-interest-bounces at cs.oswego.edu
06/09/2006 12:46 PM

concurrency-interest at cs.oswego.edu

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

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 
>>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/>
Concurrency-interest mailing list
Concurrency-interest at altair.cs.oswego.edu

More information about the Concurrency-interest mailing list