com.atlassian.jira.util.concurrent
Class ThreadsafeLazyLoadedReference
java.lang.Object
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.
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. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ThreadsafeLazyLoadedReference
public ThreadsafeLazyLoadedReference()
get
public final Object get()
- Get the lazily loaded reference. If your
create()
method
throws an Exception, calls to get()
will throw a RuntimeException
which wraps the previously thrown exception.
create
protected abstract Object create()
throws Exception
- The object factory method, guaranteed to be called once and only once.
protected abstract V create() throws Exception;
- Throws:
Exception
Copyright © 2002-2007 Atlassian. All Rights Reserved.