diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b961c1e8..0a10ea6a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ stages: variables: name: "SongodaCore" path: "/builds/$CI_PROJECT_PATH" - version: "2.3.21" + version: "2.3.22" build: stage: build diff --git a/Core/src/main/java/com/songoda/core/SongodaCore.java b/Core/src/main/java/com/songoda/core/SongodaCore.java index a8fbcee2..93df6f44 100644 --- a/Core/src/main/java/com/songoda/core/SongodaCore.java +++ b/Core/src/main/java/com/songoda/core/SongodaCore.java @@ -17,6 +17,8 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.*; import java.util.logging.Level; + +import com.songoda.core.database.DataManagerAbstract; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -402,6 +404,8 @@ public class SongodaCore { if (pi != null) { registeredPlugins.remove(pi); } + // Terminate all active threads + DataManagerAbstract.terminateAllThreads(); if (event.getPlugin() == piggybackedPlugin) { // uh-oh! Abandon ship!! Bukkit.getServicesManager().unregisterAll(piggybackedPlugin); diff --git a/Core/src/main/java/com/songoda/core/database/DataManagerAbstract.java b/Core/src/main/java/com/songoda/core/database/DataManagerAbstract.java index 12a4e799..2cb906c9 100644 --- a/Core/src/main/java/com/songoda/core/database/DataManagerAbstract.java +++ b/Core/src/main/java/com/songoda/core/database/DataManagerAbstract.java @@ -11,6 +11,7 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; public class DataManagerAbstract { @@ -88,4 +89,34 @@ public class DataManagerAbstract { threads.computeIfAbsent(threadKey.toUpperCase(), t -> Executors.newSingleThreadScheduledExecutor()).execute(runnable); } + + /** + * Terminate thread once all tasks have been completed. + * + * @param threadKey the thread key to terminate. + */ + public static void terminateThread(String threadKey) { + ScheduledExecutorService service = threads.get(threadKey); + if (service != null) { + threads.remove(threadKey); + try { + service.awaitTermination(0, TimeUnit.SECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + /** + * Terminate all active threads. + */ + public static void terminateAllThreads() { + for (ScheduledExecutorService service : threads.values()) { + try { + service.awaitTermination(0, TimeUnit.SECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } }