fix weird ModLauncher class loading issue when using forkjoin.commonpool()

This commit is contained in:
dags- 2020-03-19 00:14:54 +00:00
parent 359dddf5a2
commit 9583c1a69f
5 changed files with 43 additions and 14 deletions

View File

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

View File

@ -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;
}
}
}

View File

@ -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<K, V extends ExpiringEntry> implements Runnable {
private final long expireMS;
private final long intervalMS;
private final Executor executor;
private final Map<K, V> map = new ConcurrentHashMap<>();
private volatile long timestamp = 0L;
@ -20,7 +19,6 @@ public class Cache<K, V extends ExpiringEntry> 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<V> supplier) {
@ -37,7 +35,7 @@ public class Cache<K, V extends ExpiringEntry> implements Runnable {
long now = System.currentTimeMillis();
if (now - timestamp > intervalMS) {
timestamp = now;
executor.execute(this);
ThreadPool.getDefaultPool().execute(this);
}
}

View File

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

View File

@ -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