mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-11-05 01:26:20 +00:00
Clean up our thread creation code a little
- Provide a helper method for creating threads with a lower priority. - Use that in our network code (which already used this priority) and for the computer worker threads (which used the default priority before). I genuinely thought I did this years ago.
This commit is contained in:
parent
f0abb83f6e
commit
b691430889
@ -39,17 +39,8 @@ import java.util.concurrent.TimeUnit;
|
|||||||
public final class NetworkUtils {
|
public final class NetworkUtils {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(NetworkUtils.class);
|
private static final Logger LOG = LoggerFactory.getLogger(NetworkUtils.class);
|
||||||
|
|
||||||
public static final ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(
|
public static final ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(4, ThreadUtils.lowPriorityFactory("Network"));
|
||||||
4,
|
public static final EventLoopGroup LOOP_GROUP = new NioEventLoopGroup(4, ThreadUtils.lowPriorityFactory("Netty"));
|
||||||
ThreadUtils.builder("Network")
|
|
||||||
.setPriority(Thread.MIN_PRIORITY + (Thread.NORM_PRIORITY - Thread.MIN_PRIORITY) / 2)
|
|
||||||
.build()
|
|
||||||
);
|
|
||||||
|
|
||||||
public static final EventLoopGroup LOOP_GROUP = new NioEventLoopGroup(4, ThreadUtils.builder("Netty")
|
|
||||||
.setPriority(Thread.MIN_PRIORITY + (Thread.NORM_PRIORITY - Thread.MIN_PRIORITY) / 2)
|
|
||||||
.build()
|
|
||||||
);
|
|
||||||
|
|
||||||
private static final AbstractTrafficShapingHandler SHAPING_HANDLER = new GlobalTrafficShapingHandler(
|
private static final AbstractTrafficShapingHandler SHAPING_HANDLER = new GlobalTrafficShapingHandler(
|
||||||
EXECUTOR, CoreConfig.httpUploadBandwidth, CoreConfig.httpDownloadBandwidth
|
EXECUTOR, CoreConfig.httpUploadBandwidth, CoreConfig.httpDownloadBandwidth
|
||||||
|
@ -55,7 +55,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||||||
public final class ComputerThread {
|
public final class ComputerThread {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(ComputerThread.class);
|
private static final Logger LOG = LoggerFactory.getLogger(ComputerThread.class);
|
||||||
private static final ThreadFactory monitorFactory = ThreadUtils.factory("Computer-Monitor");
|
private static final ThreadFactory monitorFactory = ThreadUtils.factory("Computer-Monitor");
|
||||||
private static final ThreadFactory workerFactory = ThreadUtils.factory("Computer-Worker");
|
private static final ThreadFactory workerFactory = ThreadUtils.lowPriorityFactory("Computer-Worker");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How often the computer thread monitor should run.
|
* How often the computer thread monitor should run.
|
||||||
|
@ -17,6 +17,16 @@ public final class ThreadUtils {
|
|||||||
private static final Logger LOG = LoggerFactory.getLogger(ThreadUtils.class);
|
private static final Logger LOG = LoggerFactory.getLogger(ThreadUtils.class);
|
||||||
private static final ThreadGroup baseGroup = new ThreadGroup("ComputerCraft");
|
private static final ThreadGroup baseGroup = new ThreadGroup("ComputerCraft");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A lower thread priority (though not the minimum), used for most of ComputerCraft's threads.
|
||||||
|
* <p>
|
||||||
|
* The Minecraft thread typically runs on a higher priority thread anyway, but this ensures we don't dominate other,
|
||||||
|
* more critical work.
|
||||||
|
*
|
||||||
|
* @see Thread#setPriority(int)
|
||||||
|
*/
|
||||||
|
public static final int LOWER_PRIORITY = (Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2;
|
||||||
|
|
||||||
private ThreadUtils() {
|
private ThreadUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,16 +39,6 @@ public final class ThreadUtils {
|
|||||||
return baseGroup;
|
return baseGroup;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a group under ComputerCraft's shared group.
|
|
||||||
*
|
|
||||||
* @param name The group's name. This will be prefixed with "ComputerCraft-".
|
|
||||||
* @return The constructed thread group.
|
|
||||||
*/
|
|
||||||
public static ThreadGroup group(String name) {
|
|
||||||
return new ThreadGroup(baseGroup, baseGroup.getName() + "-" + name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@link ThreadFactoryBuilder}, which constructs threads under a group of the given {@code name}.
|
* Create a new {@link ThreadFactoryBuilder}, which constructs threads under a group of the given {@code name}.
|
||||||
* <p>
|
* <p>
|
||||||
@ -50,7 +50,7 @@ public final class ThreadUtils {
|
|||||||
* @see #factory(String)
|
* @see #factory(String)
|
||||||
*/
|
*/
|
||||||
public static ThreadFactoryBuilder builder(String name) {
|
public static ThreadFactoryBuilder builder(String name) {
|
||||||
var group = group(name);
|
var group = new ThreadGroup(baseGroup, baseGroup.getName() + "-" + name);
|
||||||
return new ThreadFactoryBuilder()
|
return new ThreadFactoryBuilder()
|
||||||
.setDaemon(true)
|
.setDaemon(true)
|
||||||
.setNameFormat(group.getName().replace("%", "%%") + "-%d")
|
.setNameFormat(group.getName().replace("%", "%%") + "-%d")
|
||||||
@ -71,4 +71,16 @@ public final class ThreadUtils {
|
|||||||
public static ThreadFactory factory(String name) {
|
public static ThreadFactory factory(String name) {
|
||||||
return builder(name).build();
|
return builder(name).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link ThreadFactory}, which constructs threads under a group of the given {@code name}. This is the
|
||||||
|
* same as {@link #factory(String)}, but threads will be created with a {@linkplain #LOWER_PRIORITY lower priority}.
|
||||||
|
*
|
||||||
|
* @param name The name for the thread group and child threads.
|
||||||
|
* @return The constructed thread factory.
|
||||||
|
* @see #builder(String)
|
||||||
|
*/
|
||||||
|
public static ThreadFactory lowPriorityFactory(String name) {
|
||||||
|
return builder(name).setPriority(LOWER_PRIORITY).build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user