|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.atlassian.jira.util.concurrent.ThreadsafeLazyLoadedReference
public abstract class ThreadsafeLazyLoadedReference
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).
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 |
---|
public ThreadsafeLazyLoadedReference()
Method Detail |
---|
public final Object get()
create()
method throws an
Exception calls to get()
will throw an InitializationException which wraps the previously thrown
exception.
create()
created.
ThreadsafeLazyLoadedReference.InitializationException
- if the create()
method throws an exception. The Throwable.getCause()
will contain the exception thrown by the create()
methodpublic final Object getInterruptibly() throws InterruptedException
create()
method throws an
Exception, calls to get()
will throw a RuntimeException which wraps the previously thrown
exception.
create()
created.
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).protected abstract Object create() throws Exception
get()
and getInterruptibly()
will return.
ThreadsafeLazyLoadedReference.InitializationException
- if anything goes wrong, rethrown as an InitializationException from get()
and getInterruptibly()
Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |