Skip to content

Commit

Permalink
Some code changes to support #9 in future, but not now
Browse files Browse the repository at this point in the history
Unsafe to do at this time
  • Loading branch information
aikar committed Nov 15, 2016
1 parent 31b1302 commit 9208522
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 20 additions & 7 deletions core/src/main/java/co/aikar/taskchain/TaskChainAsyncQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9208522

Please sign in to comment.