Add double checked synchronization to EventBus instance initializer
This commit is contained in:
parent
603a838640
commit
dbb816c6cb
@ -19,7 +19,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
*/
|
*/
|
||||||
public final class EventBus {
|
public final class EventBus {
|
||||||
|
|
||||||
private static EventBus singletonInstance;
|
private static volatile EventBus singletonInstance;
|
||||||
|
|
||||||
private static final Logger logger = System.getLogger(EventBus.class.getName());
|
private static final Logger logger = System.getLogger(EventBus.class.getName());
|
||||||
|
|
||||||
@ -30,11 +30,15 @@ public final class EventBus {
|
|||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public static EventBus getInstance() {
|
public static EventBus getInstance() {
|
||||||
if (singletonInstance == null) {
|
EventBus instance = singletonInstance;
|
||||||
logger.log(Level.DEBUG, "Initializing singleton event bus instance");
|
if (instance == null)
|
||||||
singletonInstance = new EventBus();
|
synchronized (EventBus.class) {
|
||||||
}
|
if ((instance = singletonInstance) == null) {
|
||||||
return singletonInstance;
|
logger.log(Level.DEBUG, "Initializing singleton event bus instance");
|
||||||
|
instance = singletonInstance = new EventBus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Map<Class<? extends IEvent>, TreeSet<EventHandler>> bindings
|
private final Map<Class<? extends IEvent>, TreeSet<EventHandler>> bindings
|
||||||
|
Loading…
Reference in New Issue
Block a user