[concurrency-interest] reusing threads and thread local state

Jeremy Manson jmanson at cs.purdue.edu
Sat Apr 30 18:19:39 EDT 2005


Joshua Bloch wrote:
> Jeremy,
> 
> Honestly, my complexity meter is buried deep in the red.  Anything
> that complex can't be good.  Regardless of whether it's sound, the
> average bear coudln't make use of such a facility.
> 
>          Sorry to be so blunt,
> 
>          Josh
>

Honestly, I think it is more my inability to explain it articulately in 
that last message.  Basically, you could imagine a class ExecutionUnit 
with a method start() that executes in the same thread, but pretends to 
be in a different thread for the purposes of final fields and 
ThreadLocals.  Use would look something like:

public class Foo implements Runnable {

   static ThreadLocal t = new ThreadLocal();
   static Object o = new Object();
   static Object p = new Object();

   public static void main(String [] args) {
     t.put(o);
     Object r1 = t.get(); // sees o
     ExecutionUnit eu = new ExecutionUnit(new Foo());
     eu.start();
     Object r2 = t.get(); // sees o
   }

   public void run() {
     Object r3 = t.get(); // sees null
     t.put(p);
     Object r4 = t.get(); // sees p
   }

}

Basically, all of the ThreadLocals are pushed on a stack for the 
duration of the execution of the ExecutionUnit.  Implementation would be 
fairly straightforward - ThreadLocals would be defined in terms of 
ExecutionUnits instead of Threads.  The other benefits would be what I 
said about final fields (the complex bit) and anything else people 
wanted to think of.  Maybe you could call join on ExecutionUnits, for 
example.

At a high level, this is an attempt to separate Threads into two 
orthogonal concepts - the programmer-level view of a thread as some 
activities performed by the program versus the vm-level view of a thread 
as some low-level constructs.

On the other hand, perhaps this is hitting a small nail with a big 
hammer.  Between other projects, I'm trying to get up the courage to 
think about a standard way to deserialize final fields, and this is the 
kind of related thing that pops into my head from time to time.

					Jeremy


More information about the Concurrency-interest mailing list