From 33ebf0302bf7d80aa06096899248ecda80ab7e8f Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 15 Oct 2021 08:25:38 +0200 Subject: [PATCH] Initialize the default event bus statically The previous method that used double checked synchronization offers little performance benefits over a plain static initialization. Reported-by @harkle-the-cake --- .../java/dev/kske/eventbus/core/EventBus.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/event-bus-core/src/main/java/dev/kske/eventbus/core/EventBus.java b/event-bus-core/src/main/java/dev/kske/eventbus/core/EventBus.java index 27bb4fb..3eef15c 100644 --- a/event-bus-core/src/main/java/dev/kske/eventbus/core/EventBus.java +++ b/event-bus-core/src/main/java/dev/kske/eventbus/core/EventBus.java @@ -52,26 +52,18 @@ public final class EventBus { */ public static final int DEFAULT_PRIORITY = 100; - private static volatile EventBus singletonInstance; + private static final EventBus singletonInstance = new EventBus(); private static final Logger logger = System.getLogger(EventBus.class.getName()); /** - * Produces a singleton instance of the event bus. It is lazily initialized on the first call. + * Returns the default event bus, which is a statically initialized singleton instance. * - * @return a singleton instance of the event bus. + * @return the default event bus * @since 0.0.2 */ public static EventBus getInstance() { - EventBus instance = singletonInstance; - if (instance == null) - synchronized (EventBus.class) { - if ((instance = singletonInstance) == null) { - logger.log(Level.DEBUG, "Initializing singleton event bus instance"); - instance = singletonInstance = new EventBus(); - } - } - return instance; + return singletonInstance; } private final Map, TreeSet> bindings =