Added executor to utilities.

This commit is contained in:
Harrison Deng 2020-04-29 14:00:30 -05:00
parent 14d0375c7e
commit c2fb0ffea8
2 changed files with 22 additions and 8 deletions

View File

@ -3,11 +3,15 @@ package ca.recrown.islandsurvivalcraft;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
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 static <K, V> HashMap<V, ArrayList<K>> invertHashMap(HashMap<K, V> hashMap) {
HashMap<V, ArrayList<K>> res = new HashMap<>();
@ -71,4 +75,14 @@ public class Utilities {
public static Point2 worldToLocalChunkCoordinates(Point2 worldCoordinates) {
return worldToLocalChunkCoordinates(worldCoordinates.x, worldCoordinates.y);
}
public static ThreadFactory createThreadFactory() {
return new ThreadFactory(){
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "ISC-worker");
return thread;
}
};
}
}

View File

@ -28,7 +28,7 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
private final Cache<Point2, Double> blockValueCache = new Cache<>(102400);
private final Cache<Point2, Biome[]> biomeCache = new Cache<>(102400);
private final Cache<Point2, Boolean> chunkExistenceCache = new Cache<>(16384);
private final ExecutorService executor = Executors.newFixedThreadPool(2);
private final ExecutorService executor = Utilities.ISC_EXECUTOR;
private volatile World currentWorld;
@Override
@ -43,7 +43,7 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
BiomeGenerator biomeGenerator = new UniBiomeIslandGenerator();
int maxHeight = world.getMaxHeight();
LinkedList<Future<Boolean>> biomeColumns = new LinkedList<>();
LinkedList<Future<Boolean>> tasks = new LinkedList<>();
ChunkData chunkData = createChunkData(world);
Biome[][] biomes = new Biome[Utilities.CHUNK_SIZE][Utilities.CHUNK_SIZE];
@ -57,23 +57,23 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
final int biomeX = localX;
final int biomeZ = localZ;
biomeColumns.add(executor.submit(() -> {
tasks.add(executor.submit(() -> {
for (int y = 0; y < maxHeight; y++) {
biomeGrid.setBiome(biomeX, y, biomeZ, biomes[biomeX][biomeZ]);
}
return true;
}));
int worldX = 16 * chunkX + localX;
int worldZ = 16 * chunkZ + localZ;
final int worldX = 16 * chunkX + localX;
final int worldZ = 16 * chunkZ + localZ;
int height = heightShader.getAltitude(worldX, worldZ, biomes[localX][localZ]);
chunkData.setRegion(localX, 1, localZ, localX + 1, height, localZ + 1, Material.DIAMOND_BLOCK);
}
}
chunkData.setRegion(0, 0, 0, 16, 1, 16, Material.BEDROCK);
while (!biomeColumns.isEmpty()) {
while (!tasks.isEmpty()) {
try {
biomeColumns.poll().get();
tasks.poll().get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
throw new IllegalStateException(e.getCause().getMessage());
@ -81,7 +81,7 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
}
return chunkData;
}
@EventHandler(priority = EventPriority.MONITOR)
public void onChunkLoaded(ChunkLoadEvent event) {
if (event.isNewChunk()) {