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;
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<Behavior> 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<Behavior> getBehaviors() {
public List<Behavior> 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<Baritone> runnable) {
public void registerInitListener(Consumer<Baritone> runnable) {
this.onInitConsumers.add(runnable);
}
}

View File

@ -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();
});
}
/**

View File

@ -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<Chunk> queue = toPack;

View File

@ -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() {
Baritone.INSTANCE.getExecutor().execute(() -> {
System.out.println("Started saving the world in a new thread");
cache.save();
}
}.start();
});
}
}