thread pool executor

This commit is contained in:
Leijurv 2018-09-16 14:14:50 -07:00
parent 23779329a9
commit 8dac838fe0
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
4 changed files with 33 additions and 22 deletions

View File

@ -17,8 +17,8 @@
package baritone; package baritone;
import baritone.api.event.listener.IGameEventListener;
import baritone.api.behavior.Behavior; import baritone.api.behavior.Behavior;
import baritone.api.event.listener.IGameEventListener;
import baritone.behavior.*; import baritone.behavior.*;
import baritone.event.GameEventHandler; import baritone.event.GameEventHandler;
import baritone.utils.InputOverrideHandler; import baritone.utils.InputOverrideHandler;
@ -29,6 +29,10 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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; import java.util.function.Consumer;
/** /**
@ -52,6 +56,8 @@ public enum Baritone {
private Settings settings; private Settings settings;
private List<Behavior> behaviors; private List<Behavior> behaviors;
private File dir; private File dir;
private ThreadPoolExecutor threadPool;
/** /**
* List of consumers to be called after Baritone has initialized * List of consumers to be called after Baritone has initialized
@ -71,6 +77,7 @@ public enum Baritone {
if (initialized) { if (initialized) {
return; return;
} }
this.threadPool = new ThreadPoolExecutor(4, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>());
this.gameEventHandler = new GameEventHandler(); this.gameEventHandler = new GameEventHandler();
this.inputOverrideHandler = new InputOverrideHandler(); this.inputOverrideHandler = new InputOverrideHandler();
this.settings = new Settings(); this.settings = new Settings();
@ -96,22 +103,26 @@ public enum Baritone {
this.onInitConsumers.forEach(consumer -> consumer.accept(this)); this.onInitConsumers.forEach(consumer -> consumer.accept(this));
} }
public final boolean isInitialized() { public boolean isInitialized() {
return this.initialized; return this.initialized;
} }
public final IGameEventListener getGameEventHandler() { public IGameEventListener getGameEventHandler() {
return this.gameEventHandler; return this.gameEventHandler;
} }
public final InputOverrideHandler getInputOverrideHandler() { public InputOverrideHandler getInputOverrideHandler() {
return this.inputOverrideHandler; return this.inputOverrideHandler;
} }
public final List<Behavior> getBehaviors() { public List<Behavior> getBehaviors() {
return this.behaviors; return this.behaviors;
} }
public Executor getExecutor() {
return threadPool;
}
public void registerBehavior(Behavior behavior) { public void registerBehavior(Behavior behavior) {
this.behaviors.add(behavior); this.behaviors.add(behavior);
this.registerEventListener(behavior); this.registerEventListener(behavior);
@ -121,11 +132,11 @@ public enum Baritone {
this.gameEventHandler.registerEventListener(listener); this.gameEventHandler.registerEventListener(listener);
} }
public final boolean isActive() { public boolean isActive() {
return this.active; return this.active;
} }
public final Settings getSettings() { public Settings getSettings() {
return this.settings; return this.settings;
} }
@ -133,11 +144,11 @@ public enum Baritone {
return Baritone.INSTANCE.settings; // yolo return Baritone.INSTANCE.settings; // yolo
} }
public final File getDir() { public File getDir() {
return this.dir; return this.dir;
} }
public final void registerInitListener(Consumer<Baritone> runnable) { public void registerInitListener(Consumer<Baritone> runnable) {
this.onInitConsumers.add(runnable); this.onInitConsumers.add(runnable);
} }
} }

View File

@ -61,7 +61,7 @@ public final class PathingBehavior extends Behavior implements Helper {
private boolean lastAutoJump; private boolean lastAutoJump;
private void dispatchPathEvent(PathEvent event) { private void dispatchPathEvent(PathEvent event) {
new Thread(() -> Baritone.INSTANCE.getGameEventHandler().onPathEvent(event)).start(); Baritone.INSTANCE.getExecutor().execute(() -> Baritone.INSTANCE.getGameEventHandler().onPathEvent(event));
} }
@Override @Override
@ -249,7 +249,7 @@ public final class PathingBehavior extends Behavior implements Helper {
} }
isPathCalcInProgress = true; isPathCalcInProgress = true;
} }
new Thread(() -> { Baritone.INSTANCE.getExecutor().execute(() -> {
if (talkAboutIt) { if (talkAboutIt) {
logDebug("Starting to search for path from " + start + " to " + goal); logDebug("Starting to search for path from " + start + " to " + goal);
} }
@ -292,7 +292,7 @@ public final class PathingBehavior extends Behavior implements Helper {
synchronized (pathCalcLock) { synchronized (pathCalcLock) {
isPathCalcInProgress = false; isPathCalcInProgress = false;
} }
}).start(); });
} }
/** /**

View File

@ -66,8 +66,8 @@ public final class CachedWorld implements Helper {
System.out.println("Cached world directory: " + directory); System.out.println("Cached world directory: " + directory);
// Insert an invalid region element // Insert an invalid region element
cachedRegions.put(0, null); cachedRegions.put(0, null);
new PackerThread().start(); Baritone.INSTANCE.getExecutor().execute(new PackerThread());
new Thread(() -> { Baritone.INSTANCE.getExecutor().execute(() -> {
try { try {
Thread.sleep(30000); Thread.sleep(30000);
while (true) { while (true) {
@ -80,7 +80,7 @@ public final class CachedWorld implements Helper {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
}).start(); });
} }
public final void queueForPacking(Chunk chunk) { 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; 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() { public void run() {
while (true) { while (true) {
LinkedBlockingQueue<Chunk> queue = toPack; LinkedBlockingQueue<Chunk> queue = toPack;

View File

@ -17,6 +17,8 @@
package baritone.cache; package baritone.cache;
import baritone.Baritone;
import java.nio.file.Path; import java.nio.file.Path;
/** /**
@ -37,11 +39,9 @@ public class WorldData {
} }
void onClose() { void onClose() {
new Thread() { Baritone.INSTANCE.getExecutor().execute(() -> {
public void run() { System.out.println("Started saving the world in a new thread");
System.out.println("Started saving the world in a new thread"); cache.save();
cache.save(); });
}
}.start();
} }
} }