From a9f19cfc8dabe5bd70b208db5462d020339d881c Mon Sep 17 00:00:00 2001 From: ZekerZhayard Date: Mon, 29 Jun 2020 00:57:42 +0800 Subject: [PATCH] Allow users to customize the size of thread pools. (#86) * Increase compatible Java version to 8. * Support Forge & Fabric 1.15.2 * Fix lag spike while loading player skull textures. * Allow users to customize the size of thread pools. * Use the old scheme about loading skulls to avoid blocking the thread pool when "enableUpdateSkull" is true. * Fix merge. --- .../customskinloader/CustomSkinLoader.java | 18 +++++++++--------- .../source/customskinloader/config/Config.java | 2 ++ .../customskinloader/fake/FakeSkinManager.java | 5 +---- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Common/source/customskinloader/CustomSkinLoader.java b/Common/source/customskinloader/CustomSkinLoader.java index 05fbc41..7bd2b34 100644 --- a/Common/source/customskinloader/CustomSkinLoader.java +++ b/Common/source/customskinloader/CustomSkinLoader.java @@ -2,8 +2,7 @@ package customskinloader; import java.io.File; import java.util.Map; -import java.util.concurrent.Executors; -import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -55,7 +54,7 @@ public class CustomSkinLoader { private static final ProfileCache profileCache=new ProfileCache(); private static final DynamicSkullManager dynamicSkullManager=new DynamicSkullManager(); - private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 33, 3, TimeUnit.MINUTES, new LinkedBlockingDeque<>(333), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy()); + private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(config.threadPoolSize, config.threadPoolSize, 1L, TimeUnit.MINUTES, new LinkedBlockingQueue<>()); //For User Skin public static Map loadProfile(GameProfile gameProfile){ @@ -168,12 +167,17 @@ public class CustomSkinLoader { } if (!profileCache.isLoading(credential)) { profileCache.setLoading(credential, true); - threadPool.execute(() -> { + Runnable loadThread = () -> { String tempName = Thread.currentThread().getName(); Thread.currentThread().setName(username + "'s skull"); loadProfile0(gameProfile);//Load in thread Thread.currentThread().setName(tempName); - }); + }; + if (config.enableUpdateSkull) { + new Thread(loadThread).start(); + } else { + threadPool.execute(loadThread); + } } return Maps.newHashMap(); } @@ -185,8 +189,4 @@ public class CustomSkinLoader { logger.info("Minecraft: "+MinecraftUtil.getMinecraftMainVersion()+"("+MinecraftUtil.getMinecraftVersionText()+")"); return logger; } - - public static void initStatic(){ - //Do nothing - } } diff --git a/Common/source/customskinloader/config/Config.java b/Common/source/customskinloader/config/Config.java index 427a1e9..8999f7d 100644 --- a/Common/source/customskinloader/config/Config.java +++ b/Common/source/customskinloader/config/Config.java @@ -27,6 +27,7 @@ public class Config { public boolean ignoreHttpsCertificate=false; public boolean forceLoadAllTextures=false; public boolean enableCape = true; + public int threadPoolSize = 1; //Cache public int cacheExpiry=30; @@ -56,6 +57,7 @@ public class Config { //Init program config.loadExtraList(); config.initLocalFolder(); + config.threadPoolSize = Math.max(config.threadPoolSize, 1); if(config.ignoreHttpsCertificate) HttpUtil0.ignoreHttpsCertificate(); if(config.enableCacheAutoClean && !config.enableLocalProfileCache){ diff --git a/Common/source/customskinloader/fake/FakeSkinManager.java b/Common/source/customskinloader/fake/FakeSkinManager.java index 86b9fe0..97e2db6 100644 --- a/Common/source/customskinloader/fake/FakeSkinManager.java +++ b/Common/source/customskinloader/fake/FakeSkinManager.java @@ -31,7 +31,7 @@ import net.minecraft.client.resources.SkinManager.SkinAvailableCallback; import net.minecraft.util.ResourceLocation; public class FakeSkinManager { - private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(0, 2, 1L, TimeUnit.MINUTES, new LinkedBlockingQueue()); + private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(CustomSkinLoader.config.threadPoolSize, CustomSkinLoader.config.threadPoolSize, 1L, TimeUnit.MINUTES, new LinkedBlockingQueue<>()); private final TextureManager textureManager; private final Map modelCache = new HashMap<>(); @@ -39,9 +39,6 @@ public class FakeSkinManager { public FakeSkinManager(TextureManager textureManagerInstance, File skinCacheDirectory, MinecraftSessionService sessionService) { this.textureManager = textureManagerInstance; HttpTextureUtil.defaultCacheDir = skinCacheDirectory; - - //Init CustomSkinLoader earlier - CustomSkinLoader.initStatic(); } public ResourceLocation loadSkin(MinecraftProfileTexture profileTexture, Type textureType) {