com.atlassian.jira.util.concurrent
Class ThreadsafeLazyLoadedReference

java.lang.Object
  extended by com.atlassian.jira.util.concurrent.ThreadsafeLazyLoadedReference

public abstract class ThreadsafeLazyLoadedReference
extends Object

Thread-safe lock-less (see note) reference that is not constructed until required. This class is used to maintain a reference to an object that is expensive to create and must be constructed once and once only. Therefore this reference behaves as though the final keyword has been used (you cannot reset it once it has been constructed).

When using this class you need to implement the create() method to return the object this reference will hold.

For instance:

 final ThreadsafeLazyLoadedReference ref = new ThreadsafeLazyLoadedReference()
 {
     protected Object create() throws Exception
     {
         // Do some useful object construction here
         return new MyObject();
     }
 };
 

Then call to get a reference to the object:

   MyObject myLazyLoadedObject = (MyObject) ref.get()
 

Note: uncomment generics declarations for Java 5 and change imports to java.util.concurrent.*

Note: The jdk1.4 backport of the util.concurrent libraries is actually not lockless as it uses synchronized to simulate the compareAndSet primitives in jdk1.5.

Interruption policy is that if you want to be cancellable while waiting for another thread to create the value, instead of calling get() call getInterruptibly(). If your create() method throws an InterruptedException however, it will be the causal exception inside the runtime ThreadsafeLazyLoadedReference.InitializationException that get() or getInterruptibly() throws and your create() will not be called again.


Nested Class Summary
static class ThreadsafeLazyLoadedReference.InitializationException
          If the factory create() method threw an exception, this wraps it.
 
Constructor Summary
ThreadsafeLazyLoadedReference()
           
 
Method Summary
protected abstract  Object create()
          The object factory method, guaranteed to be called once and only once.
 Object get()
          Get the lazily loaded reference in a non-cancelable manner.
 Object getInterruptibly()
          Get the lazily loaded reference in a cancelable manner.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadsafeLazyLoadedReference

public ThreadsafeLazyLoadedReference()
Method Detail

get

public final Object get()
Get the lazily loaded reference in a non-cancelable manner. If your create() method throws an Exception calls to get() will throw an InitializationException which wraps the previously thrown exception.

Returns:
the object that create() created.
Throws:
ThreadsafeLazyLoadedReference.InitializationException - if the create() method throws an exception. The Throwable.getCause() will contain the exception thrown by the create() method

getInterruptibly

public final Object getInterruptibly()
                              throws InterruptedException
Get the lazily loaded reference in a cancelable manner. If your create() method throws an Exception, calls to get() will throw a RuntimeException which wraps the previously thrown exception.

Returns:
the object that create() created.
Throws:
ThreadsafeLazyLoadedReference.InitializationException - if the create() method throws an exception. The Throwable.getCause() will contain the exception thrown by the create() method
InterruptedException - If the calling thread is Interrupted while waiting for another thread to create the value (if the creating thread is interrupted while blocking on something, the InterruptedException will be thrown as the causal exception of the ThreadsafeLazyLoadedReference.InitializationException to everybody calling this method).

create

protected abstract Object create()
                          throws Exception
The object factory method, guaranteed to be called once and only once.

Returns:
the object that get() and getInterruptibly() will return.
Throws:
ThreadsafeLazyLoadedReference.InitializationException - if anything goes wrong, rethrown as an InitializationException from get() and getInterruptibly()
Exception


Copyright © 2002-2009 Atlassian. All Rights Reserved.