From 8dac838fe001d4033ea341e9387c47e896ff591d Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 16 Sep 2018 14:14:50 -0700 Subject: [PATCH] thread pool executor --- src/main/java/baritone/Baritone.java | 29 +++++++++++++------ .../baritone/behavior/PathingBehavior.java | 6 ++-- src/main/java/baritone/cache/CachedWorld.java | 8 ++--- src/main/java/baritone/cache/WorldData.java | 12 ++++---- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/main/java/baritone/Baritone.java b/src/main/java/baritone/Baritone.java index ed69acda..0d89b730 100755 --- a/src/main/java/baritone/Baritone.java +++ b/src/main/java/baritone/Baritone.java @@ -17,8 +17,8 @@ package baritone; -import baritone.api.event.listener.IGameEventListener; import baritone.api.behavior.Behavior; +import baritone.api.event.listener.IGameEventListener; import baritone.behavior.*; import baritone.event.GameEventHandler; import baritone.utils.InputOverrideHandler; @@ -29,6 +29,10 @@ import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import java.util.function.Consumer; /** @@ -52,6 +56,8 @@ public enum Baritone { private Settings settings; private List behaviors; private File dir; + private ThreadPoolExecutor threadPool; + /** * List of consumers to be called after Baritone has initialized @@ -71,6 +77,7 @@ public enum Baritone { if (initialized) { return; } + this.threadPool = new ThreadPoolExecutor(4, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>()); this.gameEventHandler = new GameEventHandler(); this.inputOverrideHandler = new InputOverrideHandler(); this.settings = new Settings(); @@ -96,22 +103,26 @@ public enum Baritone { this.onInitConsumers.forEach(consumer -> consumer.accept(this)); } - public final boolean isInitialized() { + public boolean isInitialized() { return this.initialized; } - public final IGameEventListener getGameEventHandler() { + public IGameEventListener getGameEventHandler() { return this.gameEventHandler; } - public final InputOverrideHandler getInputOverrideHandler() { + public InputOverrideHandler getInputOverrideHandler() { return this.inputOverrideHandler; } - public final List getBehaviors() { + public List getBehaviors() { return this.behaviors; } + public Executor getExecutor() { + return threadPool; + } + public void registerBehavior(Behavior behavior) { this.behaviors.add(behavior); this.registerEventListener(behavior); @@ -121,11 +132,11 @@ public enum Baritone { this.gameEventHandler.registerEventListener(listener); } - public final boolean isActive() { + public boolean isActive() { return this.active; } - public final Settings getSettings() { + public Settings getSettings() { return this.settings; } @@ -133,11 +144,11 @@ public enum Baritone { return Baritone.INSTANCE.settings; // yolo } - public final File getDir() { + public File getDir() { return this.dir; } - public final void registerInitListener(Consumer runnable) { + public void registerInitListener(Consumer runnable) { this.onInitConsumers.add(runnable); } } diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 205e5001..99d7f5e2 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -61,7 +61,7 @@ public final class PathingBehavior extends Behavior implements Helper { private boolean lastAutoJump; private void dispatchPathEvent(PathEvent event) { - new Thread(() -> Baritone.INSTANCE.getGameEventHandler().onPathEvent(event)).start(); + Baritone.INSTANCE.getExecutor().execute(() -> Baritone.INSTANCE.getGameEventHandler().onPathEvent(event)); } @Override @@ -249,7 +249,7 @@ public final class PathingBehavior extends Behavior implements Helper { } isPathCalcInProgress = true; } - new Thread(() -> { + Baritone.INSTANCE.getExecutor().execute(() -> { if (talkAboutIt) { logDebug("Starting to search for path from " + start + " to " + goal); } @@ -292,7 +292,7 @@ public final class PathingBehavior extends Behavior implements Helper { synchronized (pathCalcLock) { isPathCalcInProgress = false; } - }).start(); + }); } /** diff --git a/src/main/java/baritone/cache/CachedWorld.java b/src/main/java/baritone/cache/CachedWorld.java index d903c2cd..f866989a 100644 --- a/src/main/java/baritone/cache/CachedWorld.java +++ b/src/main/java/baritone/cache/CachedWorld.java @@ -66,8 +66,8 @@ public final class CachedWorld implements Helper { System.out.println("Cached world directory: " + directory); // Insert an invalid region element cachedRegions.put(0, null); - new PackerThread().start(); - new Thread(() -> { + Baritone.INSTANCE.getExecutor().execute(new PackerThread()); + Baritone.INSTANCE.getExecutor().execute(() -> { try { Thread.sleep(30000); while (true) { @@ -80,7 +80,7 @@ public final class CachedWorld implements Helper { } catch (InterruptedException e) { e.printStackTrace(); } - }).start(); + }); } public final void queueForPacking(Chunk chunk) { @@ -221,7 +221,7 @@ public final class CachedWorld implements Helper { return regionX <= REGION_MAX && regionX >= -REGION_MAX && regionZ <= REGION_MAX && regionZ >= -REGION_MAX; } - private class PackerThread extends Thread { + private class PackerThread implements Runnable { public void run() { while (true) { LinkedBlockingQueue queue = toPack; diff --git a/src/main/java/baritone/cache/WorldData.java b/src/main/java/baritone/cache/WorldData.java index a7d0b72a..6637d7b1 100644 --- a/src/main/java/baritone/cache/WorldData.java +++ b/src/main/java/baritone/cache/WorldData.java @@ -17,6 +17,8 @@ package baritone.cache; +import baritone.Baritone; + import java.nio.file.Path; /** @@ -37,11 +39,9 @@ public class WorldData { } void onClose() { - new Thread() { - public void run() { - System.out.println("Started saving the world in a new thread"); - cache.save(); - } - }.start(); + Baritone.INSTANCE.getExecutor().execute(() -> { + System.out.println("Started saving the world in a new thread"); + cache.save(); + }); } }