DiscoveryPool 共享线程池

基本信息

属性 值
类 com.mitchej123.jarjar.util.DiscoveryPool
类型 ForkJoinPool 单例
并行度 Math.max(1, Config.maxThreads)
关闭时机 LoaderV2.identifyMods 末尾

整个 JarJar 共享一个 ForkJoinPool 用于 mod 发现 / 嵌套 jar 扫描 / 各种 ASM 解析子任务。线程数由 config/jarjar.properties 的 maxThreads 控制,缺省 -1 → CPU 核数。

实现

public final class DiscoveryPool {
    private static volatile ForkJoinPool pool;
    private DiscoveryPool() {}

    public static ForkJoinPool get() {
        ForkJoinPool p = pool;
        if (p == null) {
            synchronized (DiscoveryPool.class) {
                p = pool;
                if (p == null) {
                    p = new ForkJoinPool(Math.max(1, Config.maxThreads));
                    pool = p;
                }
            }
        }
        return p;
    }

    public static void shutdown() {
        synchronized (DiscoveryPool.class) {
            final ForkJoinPool p = pool;
            if (p != null) {
                p.shutdown();
                pool = null;
            }
        }
    }
}

双重检查 + 同步块构造单例,shutdown() 后下一次 get() 会重新创建。

使用点

调用者 用途
ParallellModDiscoverer.discoverMods 主 fork/join pool
CoreModManagerV2.discoverCoreMods coremod 解析并发

CoreModManagerV2.discoverCoreMods 显式使用同一 pool:

final ForkJoinPool pool = DiscoveryPool.get();
final List<Future<ModCandidateV2>> futures = new ArrayList<>(modFilesToExample.size());
for (File modFile : modFilesToExample) {
    futures.add(pool.submit(() -> JarUtil.examineModCandidate(modFile, null, true)));
}

LoaderV2.identifyMods 末尾:

DiscoveryPool.shutdown();
return discoverer;

意味着发现完成后,pool 不再接受新任务,等待现有任务自然终结。

与 maxThreads 关系

Config.maxThreads:

final int threads = Integer.parseInt(config.getProperty("maxThreads", "-1"));
maxThreads = threads > 0 ? threads : Runtime.getRuntime().availableProcessors();

默认等于 CPU 核数。可显式覆盖为较低值(在小型 VPS 上避免抢占)或较高值(大型 modpack 加速)。

相关条目