View Javadoc

1   package cz.cuni.amis.utils;
2   
3   /**
4    * {@link Lazy} that has synchronized {@link SyncLazy#create()} method (you do not need to synchronize it for yourself).
5    * <p><p>
6    * THREAD-SAFE!
7    * 
8    * @author Jimmy
9    *
10   * @param <T>
11   */
12  public abstract class SyncLazy<T> extends Lazy<T> {
13  
14  	private Object mutex = new Object();
15  	
16  	/**
17       * @deprecated
18       * @return value created by {@link Lazy#create()} (SYNCHRONIZED CREATION!)
19       */
20  	@Override
21  	public T getVal() {
22  		return get();
23  	}
24  	
25  	/**
26       * @return value created by {@link Lazy#create()} (SYNCHRONIZED CREATION!)
27       */
28  	@Override
29  	public T get() {
30          if (obj != null) {
31          	return obj;
32          }
33          synchronized(mutex) {
34          	if (obj != null) return obj;
35              obj = create();
36              return obj;
37          }
38      }
39  	
40  	@Override
41  	public void set(T val) {
42  		synchronized(mutex) {
43  			this.obj = val;
44  		}
45  	}
46  	
47  }