Changed async execution setup.

Two beta threads with lower thread priority and one alpha with normal thread priority.

Implemented use of both in chunk loading.
This commit is contained in:
Harrison Deng 2020-04-30 16:26:02 -05:00
parent 9b111cc977
commit d0701ce63d
2 changed files with 13 additions and 11 deletions

View File

@ -11,7 +11,8 @@ import ca.recrown.islandsurvivalcraft.datatypes.Point2;
public class Utilities {
public final static int CHUNK_SIZE = 16;
public final static ExecutorService ISC_EXECUTOR = Executors.newFixedThreadPool(2, createThreadFactory());
public final static ExecutorService ISC_EXECUTOR_ALPHA = Executors.newFixedThreadPool(1, createThreadFactory("ALPHA", Thread.NORM_PRIORITY));
public final static ExecutorService ISC_EXECUTOR_BETA = Executors.newFixedThreadPool(2, createThreadFactory("BETA", Thread.MIN_PRIORITY));
public static <K, V> HashMap<V, ArrayList<K>> invertHashMap(HashMap<K, V> hashMap) {
HashMap<V, ArrayList<K>> res = new HashMap<>();
@ -76,11 +77,12 @@ public class Utilities {
return worldToLocalChunkCoordinates(worldCoordinates.x, worldCoordinates.y);
}
public static ThreadFactory createThreadFactory() {
return new ThreadFactory(){
public static ThreadFactory createThreadFactory(final String identifier, final int priority) {
return new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "ISC-worker");
Thread thread = new Thread(r, String.format("ISC-worker-%s", identifier));
thread.setPriority(priority);
return thread;
}
};

View File

@ -28,7 +28,8 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
private final Cache<Point2, Biome[]> biomeCache = new Cache<>(262144);
private final Cache<Point2, Boolean> chunkExistenceCache = new Cache<>(32768);
private final BiomeSelector biomeSelector = new BiomeSelector();
private final ExecutorService executor = Utilities.ISC_EXECUTOR;
private final ExecutorService exAlpha = Utilities.ISC_EXECUTOR_ALPHA;
private final ExecutorService exBeta = Utilities.ISC_EXECUTOR_BETA;
private volatile World currentWorld;
public IslandWorldChunkGenerator() {
@ -46,9 +47,9 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
int maxHeight = world.getMaxHeight();
LinkedList<Future<Boolean>> tasks = new LinkedList<>();
ChunkData chunkData = createChunkData(world);
Future<Boolean> preLoader = executor.submit(() -> {
for (int x = 0; x < Utilities.CHUNK_SIZE; x++) {
for (int z = 0; z < Utilities.CHUNK_SIZE; z++) {
Future<Boolean> preLoader = exAlpha.submit(() -> {
for (int x = Utilities.CHUNK_SIZE - 1; x >= 0; x--) {
for (int z = Utilities.CHUNK_SIZE - 1; z >= 0; z--) {
if (Thread.currentThread().isInterrupted()) return false;
mapper.getWorldBlockValue(Utilities.CHUNK_SIZE * chunkX + x, Utilities.CHUNK_SIZE * chunkZ + z);
}
@ -65,9 +66,8 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
biomeGenerator.generateBiomeColumn(biomes, world, chunkX, chunkZ, localX, localZ, mapper, biomeSelector,
temperatureMapGenerator, biomeCache, chunkExistenceCache);
}
if (biomes[localX][localZ] == null) throw new IllegalStateException("Biome was null.");
tasks.add(executor.submit(() -> {
tasks.add(exBeta.submit(() -> {
for (int y = 0; y < maxHeight; y++) {
biomeGrid.setBiome(localX, y, localZ, biomes[localX][localZ]);
}
@ -82,7 +82,7 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
}
chunkData.setRegion(0, 0, 0, 16, 1, 16, Material.BEDROCK);
preLoader.cancel(true);
preLoader.cancel(false);
try {
while (!tasks.isEmpty()) {
tasks.poll().get();