Added Thread Priorities. Hopefully no more stealing TPS.

This commit is contained in:
tom lee
2022-01-23 21:19:08 +08:00
parent e9e2af2807
commit f939839941
6 changed files with 22 additions and 10 deletions
@@ -96,9 +96,11 @@ public class LodBufferBuilderFactory
private static final IMinecraftWrapper MC = SingletonHandler.get(IMinecraftWrapper.class);
/** The thread used to generate new LODs off the main thread. */
public static final ExecutorService mainGenThread = Executors.newSingleThreadExecutor(new LodThreadFactory(LodBufferBuilderFactory.class.getSimpleName() + " - main"));
public static final ExecutorService mainGenThread = Executors.newSingleThreadExecutor(
new LodThreadFactory(LodBufferBuilderFactory.class.getSimpleName() + " - main", Thread.NORM_PRIORITY-2));
/** The threads used to generate buffers. */
public static final ExecutorService bufferBuilderThreads = Executors.newFixedThreadPool(CONFIG.client().advanced().threading().getNumberOfBufferBuilderThreads(), new ThreadFactoryBuilder().setNameFormat("Buffer-Builder-%d").build());
public static final ExecutorService bufferBuilderThreads = Executors.newFixedThreadPool(CONFIG.client().advanced().threading().getNumberOfBufferBuilderThreads(),
new LodThreadFactory("BufferBuilder", Thread.NORM_PRIORITY-2));
public static final long MAX_BUFFER_UPLOAD_TIMEOUT_NANOSECONDS = TimeUnit.NANOSECONDS.convert(1, TimeUnit.SECONDS);
@@ -66,7 +66,8 @@ public class LodBuilder
//public static final ExecutorService lodGenThreadPool = Executors.newFixedThreadPool(8, new ThreadFactoryBuilder().setNameFormat("Lod-Builder-%d").build());
private final ExecutorService lodGenThreadPool = Executors.newSingleThreadExecutor(new LodThreadFactory(this.getClass().getSimpleName()));
private final ExecutorService lodGenThreadPool = Executors.newSingleThreadExecutor(
new LodThreadFactory(this.getClass().getSimpleName(), Thread.NORM_PRIORITY-1));
private final ILodConfigWrapperSingleton config = SingletonHandler.get(ILodConfigWrapperSingleton.class);
@@ -61,9 +61,10 @@ public class LodWorldGenerator
/** This holds the thread used to create LOD generation requests off the main thread. */
private final ExecutorService mainGenThread = Executors.newSingleThreadExecutor(new LodThreadFactory(this.getClass().getSimpleName() + " world generator"));
private final ExecutorService mainGenThread = Executors.newSingleThreadExecutor(
new LodThreadFactory(this.getClass().getSimpleName() + " world generator", 1));
private ExecutorService genSubThreads = Executors.newFixedThreadPool(CONFIG.client().advanced().threading().getNumberOfWorldGenerationThreads(),
new ThreadFactoryBuilder().setNameFormat("Gen-Worker-Thread-%d").build());
new LodThreadFactory("Gen-Worker-Thread", 1));
/** we only want to queue up one generator thread at a time */
@@ -92,7 +92,8 @@ public class LodDimensionFileHandler
* at a time
*/
private final AtomicBoolean isFileWritingThreadRunning = new AtomicBoolean(false);
private ExecutorService fileWritingThreadPool = Executors.newSingleThreadExecutor(new LodThreadFactory(this.getClass().getSimpleName()));
private ExecutorService fileWritingThreadPool = Executors.newSingleThreadExecutor(
new LodThreadFactory(this.getClass().getSimpleName(), Thread.NORM_PRIORITY+1));
private final ConcurrentHashMap<RegionPos, LodRegion> regionToSave = new ConcurrentHashMap<RegionPos, LodRegion>();
@@ -345,7 +346,7 @@ public class LodDimensionFileHandler
ClientApi.LOGGER.error("File writing wait is interrupted! File data may not be saved correctly and may cause corruptions!!!");
e.printStackTrace();
} finally {
fileWritingThreadPool = Executors.newSingleThreadExecutor(new LodThreadFactory(this.getClass().getSimpleName()));
fileWritingThreadPool = Executors.newSingleThreadExecutor(new LodThreadFactory(this.getClass().getSimpleName(), Thread.NORM_PRIORITY+1));
}
}
}
@@ -97,7 +97,8 @@ public class LodDimension
private boolean isCutting = false;
private boolean isExpanding = false;
private final ExecutorService cutAndExpandThread = Executors.newSingleThreadExecutor(new LodThreadFactory(this.getClass().getSimpleName() + " - Cut and Expand"));
private final ExecutorService cutAndExpandThread = Executors.newSingleThreadExecutor(
new LodThreadFactory(this.getClass().getSimpleName() + " - Cut and Expand", Thread.NORM_PRIORITY-1));
/**
@@ -30,17 +30,23 @@ import java.util.concurrent.ThreadFactory;
public class LodThreadFactory implements ThreadFactory
{
public final String threadName;
public final int priority;
private int threadCount = 0;
public LodThreadFactory(String newThreadName)
public LodThreadFactory(String newThreadName, int priority)
{
if (priority < 1 || priority > 10) throw new IllegalArgumentException("Thread priority should be [1-10]!");
threadName = newThreadName + " Thread";
this.priority = priority;
}
@Override
public Thread newThread(Runnable r)
{
return new Thread(r, threadName);
Thread t = new Thread(r, threadName + "[" + (threadCount++) + "]");
t.setPriority(priority);
return t;
}
}