From 92085222c1ccb6886e46e6d2fa1efee592565b9e Mon Sep 17 00:00:00 2001 From: Aikar Date: Mon, 14 Nov 2016 21:17:55 -0500 Subject: [PATCH] Some code changes to support #9 in future, but not now Unsafe to do at this time --- .../taskchain/BukkitTaskChainFactory.java | 19 +++++++++---- .../aikar/taskchain/TaskChainAsyncQueue.java | 27 ++++++++++++++----- .../taskchain/SpongeTaskChainFactory.java | 22 ++++++++++----- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/bukkit/src/main/java/co/aikar/taskchain/BukkitTaskChainFactory.java b/bukkit/src/main/java/co/aikar/taskchain/BukkitTaskChainFactory.java index 4687f1d..348724f 100644 --- a/bukkit/src/main/java/co/aikar/taskchain/BukkitTaskChainFactory.java +++ b/bukkit/src/main/java/co/aikar/taskchain/BukkitTaskChainFactory.java @@ -31,26 +31,35 @@ import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.plugin.Plugin; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @SuppressWarnings({"WeakerAccess", "unused"}) public class BukkitTaskChainFactory extends TaskChainFactory { - private BukkitTaskChainFactory(Plugin plugin) { - super(new BukkitGameInterface(plugin)); + private BukkitTaskChainFactory(Plugin plugin, AsyncQueue asyncQueue) { + super(new BukkitGameInterface(plugin, asyncQueue)); } public static TaskChainFactory create(Plugin plugin) { - return new BukkitTaskChainFactory(plugin); + return new BukkitTaskChainFactory(plugin, new TaskChainAsyncQueue()); } +/* @TODO: #9 - Not Safe to do this + public static TaskChainFactory create(Plugin plugin, ThreadPoolExecutor executor) { + return new BukkitTaskChainFactory(plugin, new TaskChainAsyncQueue(executor)); + } + + public static TaskChainFactory create(Plugin plugin, AsyncQueue asyncQueue) { + return new BukkitTaskChainFactory(plugin, asyncQueue); + }*/ @SuppressWarnings("PublicInnerClass") private static class BukkitGameInterface implements GameInterface { private final Plugin plugin; private final AsyncQueue asyncQueue; - BukkitGameInterface(Plugin plugin) { + BukkitGameInterface(Plugin plugin, AsyncQueue asyncQueue) { this.plugin = plugin; - this.asyncQueue = new TaskChainAsyncQueue(); + this.asyncQueue = asyncQueue; } @Override diff --git a/core/src/main/java/co/aikar/taskchain/TaskChainAsyncQueue.java b/core/src/main/java/co/aikar/taskchain/TaskChainAsyncQueue.java index f60e78b..ab1d466 100644 --- a/core/src/main/java/co/aikar/taskchain/TaskChainAsyncQueue.java +++ b/core/src/main/java/co/aikar/taskchain/TaskChainAsyncQueue.java @@ -28,13 +28,26 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -class TaskChainAsyncQueue implements AsyncQueue { - private final AtomicInteger threadId = new AtomicInteger(); - private final ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(r -> { - final Thread thread = new Thread(r); - thread.setName("TaskChainAsyncQueue Thread " + threadId.getAndIncrement()); - return thread; - }); +@SuppressWarnings("WeakerAccess") +public class TaskChainAsyncQueue implements AsyncQueue { + private static final AtomicInteger threadId = new AtomicInteger(); + private final ThreadPoolExecutor executor; + + public TaskChainAsyncQueue() { + this.executor = createCachedThreadPool(); + } + + public TaskChainAsyncQueue(ThreadPoolExecutor executor) { + this.executor = executor; + } + + public static ThreadPoolExecutor createCachedThreadPool() { + return (ThreadPoolExecutor) Executors.newCachedThreadPool(r -> { + final Thread thread = new Thread(r); + thread.setName("TaskChainAsyncQueue Thread " + threadId.getAndIncrement()); + return thread; + }); + } public void postAsync(Runnable runnable) { executor.submit(runnable); diff --git a/sponge/src/main/java/co/aikar/taskchain/SpongeTaskChainFactory.java b/sponge/src/main/java/co/aikar/taskchain/SpongeTaskChainFactory.java index 2be6db1..e7667af 100644 --- a/sponge/src/main/java/co/aikar/taskchain/SpongeTaskChainFactory.java +++ b/sponge/src/main/java/co/aikar/taskchain/SpongeTaskChainFactory.java @@ -28,31 +28,41 @@ import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.scheduler.Task; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @SuppressWarnings("WeakerAccess") public class SpongeTaskChainFactory extends TaskChainFactory { - private SpongeTaskChainFactory(GameInterface impl) { - super(impl); + private SpongeTaskChainFactory(Object plugin, AsyncQueue asyncQueue) { + super(new SpongeGameInterface(plugin, asyncQueue)); } public static TaskChainFactory create(PluginContainer pluginContainer) { return create(pluginContainer.getInstance().orElse(null)); } public static TaskChainFactory create(Object plugin) { - return new SpongeTaskChainFactory(new SpongeGameInterface(plugin)); + return new SpongeTaskChainFactory(plugin, new TaskChainAsyncQueue()); } +/* @TODO: #9 - Not Safe to do this + public static TaskChainFactory create(Object plugin, ThreadPoolExecutor executor) { + return new SpongeTaskChainFactory(new SpongeGameInterface(plugin, new TaskChainAsyncQueue(executor))); + } + + public static TaskChainFactory create(Object plugin, AsyncQueue asyncQueue) { + return new SpongeTaskChainFactory(new SpongeGameInterface(plugin, asyncQueue)); + }*/ + private static class SpongeGameInterface implements GameInterface { - private final AsyncQueue asyncQueue; private final Object plugin; + private final AsyncQueue asyncQueue; - private SpongeGameInterface(Object plugin) { + private SpongeGameInterface(Object plugin, AsyncQueue asyncQueue) { + this.asyncQueue = asyncQueue; if (plugin == null || !Sponge.getPluginManager().fromInstance(plugin).isPresent()) { throw new IllegalArgumentException("Not a valid Sponge Plugin"); } this.plugin = plugin; - this.asyncQueue = new TaskChainAsyncQueue(); } @Override