Thread Local Variables

public Object calculate Object param HashMap hm results.get Object o hm.get param if o null return o o doLocalCalculate param hm.put param, o return o protected abstract Object doLocalCalculate Object param Thread local objects are declared static so that the object itself that is, the results variable in this example is shared among all threads. When the get method of the thread local variable is called, the internal mechanism of the thread local class returns the specific object assigned to...

Callable Tasks and Future Results

Executors in general operate on tasks, which are objects that implement theRunnable interface. In order to provide more control overtasks, Java also defines a special runnable object known as a callable task package java.util.concurrent public interface Callable lt V gt public lt V gt call throws Execption Unlike a runnable object, a callable object can return a result or throw a checked exception. Callable objects are used only by executor services not simple executors the services operate on...

Inheritable Thread Local Variables

Values stored by threads in thread local variables are unrelated. When a new thread is created, it gets a new copy of the thread local variable, and the value of that variable is what's returned by the initialValue method of the thread local subclass. An alternative to this idea is the InheritableThreadLocal class This document was created by an unregistered ChmMagic, please go to http www.bisenter.com to register it. Thanks, public class InheritableThreadLocal extends ThreadLocal protected...

Interrupted IO

protected void processData byte b, int n class ReaderClass extends Thread public void run byte b new byte buflen while done try int n is.read b, 0, buflen processData b, n catch lOException ioe done true public lnterruptibleReader lnputStream is this is, 512 public lnterruptibleReader lnputStream is, int len this.is is buflen len ReaderClass rc new ReaderClass synchronized lock rc.start This document was created by an unregistered ChmMagic, please go to http www.bisenter.com to register it....

Optimistic Synchronization

What's happening in our examples with atomic variables is that there is no free lunch the code avoids synchronization but it pays a potential penalty in the amount of work it performs. You can think of this as optimistic synchronization to modify a term from database management the code grabs the value of the protected variable assuming that no one else is modifying it at the moment. The code then calculates a new value for the variable and attempts to update the variable. If another thread...