Ensure Callable.call is invoked from constructing thread?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Ensure Callable.call is invoked from constructing thread?

kschneid
I need to ensure that a Callable is called from the same thread within
which it was constructed:

public class Processor implements Callable {
   private Object ctx;

   public Processor(Object threadSensitiveContext) {
     if (threadSensitiveContext == null) {
       throw new NullPointerException("threadSensitiveContext cannot be null");
     }
     this.ctx = threadSensitiveContext;
     // Store id/ref of current thread
   }

   public Object call() throws Exception {
     if (this.ctx == null) {
       throw new IllegalStateException("ctx has already been processed -
this Callable can only be called once");
     }

     // Ensure id/ref of current thread is the same as stored id/ref

     Object result = null;
     try {
       // Use this.ctx to produce result
     } finally {
       this.ctx = null;
     }
     return result;
   }
}

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.

--
Kris Schneider <mailto:[hidden email]>
D.O.Tech       <http://www.dotech.com/>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Ensure Callable.call is invoked from constructing thread?

tpeierls
You could store the thread-sensitive context in a ThreadLocal, set by the constructor, checked and used in call().

For different approaches to dealing with execution policies like this, see chapters 6 and 8 of Java Concurrency in Practice (http://jcip.net).

--tim

On 6/9/06, Kris Schneider <[hidden email]> wrote:
I need to ensure that a Callable is called from the same thread within
which it was constructed:

public class Processor implements Callable {
   private Object ctx;

   public Processor(Object threadSensitiveContext) {
     if (threadSensitiveContext == null) {
       throw new NullPointerException("threadSensitiveContext cannot be null");
     }
     this.ctx = threadSensitiveContext;
     // Store id/ref of current thread
   }

   public Object call() throws Exception {
     if (this.ctx == null) {
       throw new IllegalStateException("ctx has already been processed -
this Callable can only be called once");
     }

     // Ensure id/ref of current thread is the same as stored id/ref

     Object result = null;
     try {
       // Use this.ctx to produce result
     } finally {
       this.ctx = null;
     }
     return result;
   }
}

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.

--
Kris Schneider <mailto:[hidden email]>
D.O.Tech       < http://www.dotech.com/>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Ensure Callable.call is invoked fromconstructing thread?

Thomas Hawtin
In reply to this post by kschneid
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.

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

Tom Hawtin

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Ensure Callable.call is invoked fromconstructing thread?

kschneid
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:[hidden email]>
D.O.Tech       <http://www.dotech.com/>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Ensure Callable.call is invoked fromconstructing thread?

Jan Nielsen-4
Kris,

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


Jan Nielsen * System Architect * SunGard Higher Education * Tel +1 801 257
4155 * Fax +1 801 485 6606 * [hidden email] * 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
you.



Kris Schneider <[hidden email]>
Sent by: [hidden email]
06/09/2006 12:46 PM

To
[hidden email]
cc

Subject
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
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:[hidden email]>
D.O.Tech       <http://www.dotech.com/>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Ensure Callable.call is invoked fromconstructing thread?

Dhanji R. Prasanna
In reply to this post by Thomas Hawtin
On 6/10/06, Thomas Hawtin <[hidden email]> wrote:
>
> System.identityHashCode does not produce unique values. A very common
> mistake - the documentation is a bit poor.
>
This is very interesting, identifying transient instances uniquely is
quite a common design problem for me. One good solution I find is
using IoC to instantiate/register your objects and store a unique id
for each, then query the container for the id. I'd venture that using
a thread pool similarly (with a unique checkout id) would be a good
solution if your architecture allows for it. just a thought.

> I guess Thread.currentThread().getId() should work (since 1.5), as
> should new WeakReference<Thread>(Thread.currentThread()).
>
> Tom Hawtin
>
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest