2015-07-24 11:32:22 +03:00
|
|
|
package com.iluwatar.lazy.loading;
|
2015-04-10 20:24:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2015-11-01 21:29:13 -05:00
|
|
|
* Same as HolderNaive but with added synchronization. This implementation is thread safe, but each
|
|
|
|
* {@link #getHeavy()} call costs additional synchronization overhead.
|
2015-04-10 20:24:16 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class HolderThreadSafe {
|
|
|
|
|
2015-11-01 21:29:13 -05:00
|
|
|
private Heavy heavy;
|
|
|
|
|
|
|
|
public HolderThreadSafe() {
|
|
|
|
System.out.println("HolderThreadSafe created");
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized Heavy getHeavy() {
|
|
|
|
if (heavy == null) {
|
|
|
|
heavy = new Heavy();
|
|
|
|
}
|
|
|
|
return heavy;
|
|
|
|
}
|
2015-04-10 20:24:16 +03:00
|
|
|
}
|