diff --git a/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/ThreadPool.java b/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/ThreadPool.java index fab1fe7..f732624 100644 --- a/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/ThreadPool.java +++ b/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/ThreadPool.java @@ -42,6 +42,7 @@ public class ThreadPool implements Executor { public static final int DEFAULT_POOL_SIZE = defaultPoolSize(); private static final Object lock = new Object(); + private static final ForkJoinPool defaultPool = ThreadPool.createPool(2, "TFCore"); private static ThreadPool instance = new ThreadPool(defaultPoolSize()); @@ -49,13 +50,18 @@ public class ThreadPool implements Executor { private final ExecutorService service; private ThreadPool() { - this.service = ForkJoinPool.commonPool(); + this.service = defaultPool; this.poolSize = -1; } public ThreadPool(int size) { this.poolSize = size; - this.service = ThreadPool.createService(size, "TerraForged"); + this.service = ThreadPool.createExecutor(size, "TerraForged"); + } + + private ThreadPool(ForkJoinPool pool) { + this.service = pool; + this.poolSize = pool.getPoolSize(); } public void shutdown() { @@ -116,6 +122,10 @@ public class ThreadPool implements Executor { } } + public static ForkJoinPool getDefaultPool() { + return defaultPool; + } + public static void shutdownCurrent() { synchronized (lock) { instance.shutdown(); @@ -129,7 +139,7 @@ public class ThreadPool implements Executor { return Math.max(1, (int) ((threads / 3F) * 2F)); } - public static ExecutorService createService(int size, String name) { + public static ExecutorService createExecutor(int size, String name) { ThreadPoolExecutor service = new ThreadPoolExecutor( size, size, @@ -141,4 +151,8 @@ public class ThreadPool implements Executor { service.allowCoreThreadTimeOut(true); return service; } + + public static ForkJoinPool createPool(int size, String name) { + return new ForkJoinPool(size, new WorkerFactory.ForkJoin(name), null, true); + } } diff --git a/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/WorkerFactory.java b/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/WorkerFactory.java index fe5e925..f04ec6c 100644 --- a/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/WorkerFactory.java +++ b/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/WorkerFactory.java @@ -1,14 +1,16 @@ package com.terraforged.core.util.concurrent; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; // As DefaultThreadPool but with custom thread names public class WorkerFactory implements ThreadFactory { - private final String prefix; - private final ThreadGroup group; - private final AtomicInteger threadNumber = new AtomicInteger(1); + protected final String prefix; + protected final ThreadGroup group; + protected final AtomicInteger threadNumber = new AtomicInteger(1); public WorkerFactory(String name) { group = Thread.currentThread().getThreadGroup(); @@ -22,4 +24,19 @@ public class WorkerFactory implements ThreadFactory { thread.setName(prefix + threadNumber.getAndIncrement()); return thread; } + + public static class ForkJoin extends WorkerFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory { + + public ForkJoin(String name) { + super(name); + } + + @Override + public ForkJoinWorkerThread newThread(ForkJoinPool pool) { + ForkJoinWorkerThread thread = new ForkJoinWorkerThread(pool) {}; + thread.setDaemon(true); + thread.setName(prefix + threadNumber.getAndIncrement()); + return thread; + } + } } diff --git a/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/cache/Cache.java b/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/cache/Cache.java index 466bebf..11dd9e2 100644 --- a/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/cache/Cache.java +++ b/TerraForgedCore/src/main/java/com/terraforged/core/util/concurrent/cache/Cache.java @@ -1,9 +1,9 @@ package com.terraforged.core.util.concurrent.cache; +import com.terraforged.core.util.concurrent.ThreadPool; + import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executor; -import java.util.concurrent.ForkJoinPool; import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.function.Supplier; @@ -12,7 +12,6 @@ public class Cache implements Runnable { private final long expireMS; private final long intervalMS; - private final Executor executor; private final Map map = new ConcurrentHashMap<>(); private volatile long timestamp = 0L; @@ -20,7 +19,6 @@ public class Cache implements Runnable { public Cache(long expireTime, long interval, TimeUnit unit) { this.expireMS = unit.toMillis(expireTime); this.intervalMS = unit.toMillis(interval); - this.executor = ForkJoinPool.commonPool(); } public V computeIfAbsent(K k, Supplier supplier) { @@ -37,7 +35,7 @@ public class Cache implements Runnable { long now = System.currentTimeMillis(); if (now - timestamp > intervalMS) { timestamp = now; - executor.execute(this); + ThreadPool.getDefaultPool().execute(this); } } diff --git a/TerraForgedCore/src/main/java/com/terraforged/core/world/river/RiverManager.java b/TerraForgedCore/src/main/java/com/terraforged/core/world/river/RiverManager.java index c8fbbca..1dc5e1f 100644 --- a/TerraForgedCore/src/main/java/com/terraforged/core/world/river/RiverManager.java +++ b/TerraForgedCore/src/main/java/com/terraforged/core/world/river/RiverManager.java @@ -27,6 +27,7 @@ package com.terraforged.core.world.river; import com.terraforged.core.cell.Cell; import com.terraforged.core.region.Region; +import com.terraforged.core.util.concurrent.ThreadPool; import com.terraforged.core.util.concurrent.cache.Cache; import com.terraforged.core.util.concurrent.cache.CacheEntry; import com.terraforged.core.world.GeneratorContext; @@ -34,7 +35,6 @@ import com.terraforged.core.world.heightmap.Heightmap; import com.terraforged.core.world.terrain.Terrain; import me.dags.noise.util.NoiseUtil; -import java.util.concurrent.ForkJoinPool; import java.util.concurrent.TimeUnit; public class RiverManager { @@ -148,6 +148,6 @@ public class RiverManager { } private CacheEntry generateRegion(int rx, int rz) { - return CacheEntry.supplyAsync(() -> new RiverRegion(rx, rz, heightmap, context, riverContext), ForkJoinPool.commonPool()); + return CacheEntry.supplyAsync(() -> new RiverRegion(rx, rz, heightmap, context, riverContext), ThreadPool.getDefaultPool()); } } diff --git a/gradle.properties b/gradle.properties index 6c3a00c..8ccf744 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -mod_version=0.0.6 +mod_version=0.0.7 mc_version=1.15.2 forge_version=31.1.1 mcp_channel=snapshot