From a55823ed83b59ad962356c0281109c07a61e59e1 Mon Sep 17 00:00:00 2001 From: Patrik Ivarsson Date: Mon, 5 Sep 2022 16:54:38 +0200 Subject: [PATCH] TP-1222: Make getInstance() thread-safe with double-checked locking --- .../com/avanza/gs/test/JVMGlobalGigaSpacesManager.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/gs-test-core/src/main/java/com/avanza/gs/test/JVMGlobalGigaSpacesManager.java b/gs-test-core/src/main/java/com/avanza/gs/test/JVMGlobalGigaSpacesManager.java index c5da1d8..a8984b2 100644 --- a/gs-test-core/src/main/java/com/avanza/gs/test/JVMGlobalGigaSpacesManager.java +++ b/gs-test-core/src/main/java/com/avanza/gs/test/JVMGlobalGigaSpacesManager.java @@ -22,12 +22,16 @@ */ public final class JVMGlobalGigaSpacesManager { - private static InMemoryGigaSpacesManager instance; + private static volatile InMemoryGigaSpacesManager instance; private static InMemoryGigaSpacesManager getInstance() { if (instance == null) { - instance = new InMemoryGigaSpacesManager(); - Runtime.getRuntime().addShutdownHook(new Thread(() -> instance.close())); + synchronized (JVMGlobalGigaSpacesManager.class) { + if (instance == null) { + instance = new InMemoryGigaSpacesManager(); + Runtime.getRuntime().addShutdownHook(new Thread(() -> instance.close())); + } + } } return instance; }