Skip to content

Commit

Permalink
add executor
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Sep 20, 2023
1 parent 345f44c commit 8d1c5b9
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main/java/de/cubeside/globalserver/GlobalServer.java
Expand Up @@ -44,6 +44,9 @@
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
Expand Down Expand Up @@ -107,11 +110,14 @@ public class GlobalServer {

private File pluginFolder;

private final ExecutorService executor;

public GlobalServer() throws PluginLoadException {
console = new JLineConsole(this);

LOGGER.info("Starting GlobalServer...");

executor = new ThreadPoolExecutor(1, Integer.MAX_VALUE, 300L, TimeUnit.SECONDS, new SynchronousQueue<>());
Constructor constructor = new Constructor(ServerConfig.class);
TypeDescription serverConfigDescription = new TypeDescription(ServerConfig.class);
serverConfigDescription.addPropertyParameters("clientConfigs", ClientConfig.class);
Expand Down Expand Up @@ -296,6 +302,20 @@ void run() {
LOGGER.info("Unloading plugin " + plugin.getDescription().getName() + " " + plugin.getDescription().getVersion());
plugin.onUnload();
}
executor.shutdown();
try {
// wait 1 second without info, only print a message if this was not enough
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
LOGGER.info("Waiting 60 seconds for all async tasks to finish...");
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
LOGGER.warn("Not all tasks were completed before unloading!");
} else {
LOGGER.info("All tasks have finished executing!");
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
pluginManagerWrapper.shutdown();

console.stop();
Expand Down Expand Up @@ -525,4 +545,8 @@ public EventBus getEventBus() {
public PluginManager getPluginManager() {
return pluginManager;
}

public ExecutorService getExecutor() {
return executor;
}
}

0 comments on commit 8d1c5b9

Please sign in to comment.