mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 16:49:11 +00:00
Improve ThreadUtil.
This commit is contained in:
@@ -51,7 +51,8 @@ public final class GameService extends Service {
|
||||
/**
|
||||
* The scheduled executor service.
|
||||
*/
|
||||
private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(ThreadUtil.build("GameService"));
|
||||
private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(ThreadUtil
|
||||
.create("GameService"));
|
||||
|
||||
/**
|
||||
* The {@link ClientSynchronizer}.
|
||||
@@ -142,7 +143,8 @@ public final class GameService extends Service {
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
scheduledExecutor.scheduleAtFixedRate(new GamePulseHandler(this), GameConstants.PULSE_DELAY, GameConstants.PULSE_DELAY, TimeUnit.MILLISECONDS);
|
||||
scheduledExecutor.scheduleAtFixedRate(new GamePulseHandler(this), GameConstants.PULSE_DELAY, GameConstants.PULSE_DELAY,
|
||||
TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -50,7 +50,7 @@ public final class ParallelClientSynchronizer extends ClientSynchronizer {
|
||||
* processing cores available (this is found by the {@link ThreadUtil#AVAILABLE_PROCESSORS} method.
|
||||
*/
|
||||
public ParallelClientSynchronizer() {
|
||||
executor = Executors.newFixedThreadPool(ThreadUtil.AVAILABLE_PROCESSORS, ThreadUtil.build("ClientSynchronizer"));
|
||||
executor = Executors.newFixedThreadPool(ThreadUtil.AVAILABLE_PROCESSORS, ThreadUtil.create("ClientSynchronizer"));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -33,7 +33,7 @@ public final class LoginService extends Service {
|
||||
/**
|
||||
* The {@link ExecutorService} to which workers are submitted.
|
||||
*/
|
||||
private final ExecutorService executor = Executors.newCachedThreadPool(ThreadUtil.build("LoginService"));
|
||||
private final ExecutorService executor = Executors.newCachedThreadPool(ThreadUtil.create("LoginService"));
|
||||
|
||||
/**
|
||||
* The current {@link PlayerSerializer}.
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.apollo.util;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import java.lang.Thread.UncaughtExceptionHandler;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
@@ -13,7 +11,8 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
/**
|
||||
* A static utility class which provides ease of use functionality for {@link Thread}s
|
||||
*
|
||||
* @author Ryley Kimmel <ryley.kimmel@live.com>
|
||||
* @author Ryley
|
||||
* @author Major
|
||||
*/
|
||||
public final class ThreadUtil {
|
||||
|
||||
@@ -31,106 +30,58 @@ public final class ThreadUtil {
|
||||
* The default {@link UncaughtExceptionHandler} which raises an error from the logger with the exception and name of
|
||||
* the specified thread the exception occurred in.
|
||||
*/
|
||||
private static final UncaughtExceptionHandler DEFAULT_EXCEPTION_HANDLER = (thread, exception) -> LOGGER.log(Level.SEVERE, "Exception occured in thread " + thread.getName(), exception);
|
||||
|
||||
/**
|
||||
* Builds a {@link ThreadFactory} using the specified {@code String} name-format, {@link ThreadPriority} and
|
||||
* {@link UncaughtExceptionHandler}.
|
||||
*
|
||||
* @param name The name-format used when creating threads, may not be {@code null}.
|
||||
* @param priority The priority used when creating threads, may not be {@code null}.
|
||||
* @param handler The {@link UncaughtExceptionHandler} used when creating threads, may not be {@code null}.
|
||||
* @return A new {@link ThreadFactory} from the specified parameters, never {@code null}.
|
||||
*/
|
||||
public static ThreadFactory build(String name, ThreadPriority priority, UncaughtExceptionHandler handler) {
|
||||
Objects.requireNonNull(priority);
|
||||
|
||||
ThreadFactoryBuilder bldr = new ThreadFactoryBuilder();
|
||||
bldr.setNameFormat(name);
|
||||
bldr.setPriority(priority.getValue());
|
||||
bldr.setUncaughtExceptionHandler(handler);
|
||||
return bldr.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a {@link ThreadFactory} using the specified {@code String} name-format, {@link ThreadPriority} and the
|
||||
* default {@link UncaughtExceptionHandler}.
|
||||
*
|
||||
* @param name The name-format used when creating threads, may not be {@code null}.
|
||||
* @param priority The priority used when creating threads, may not be {@code null}.
|
||||
* @return A new {@link ThreadFactory} from the specified parameters, never {@code null}.
|
||||
* @see {@link #DEFAULT_EXCEPTION_HANDLER}
|
||||
*/
|
||||
public static ThreadFactory build(String name, ThreadPriority priority) {
|
||||
return build(name, priority, DEFAULT_EXCEPTION_HANDLER);
|
||||
}
|
||||
private static final UncaughtExceptionHandler DEFAULT_EXCEPTION_HANDLER = (thread, exception) -> LOGGER.log(Level.SEVERE,
|
||||
"Exception occured in thread " + thread.getName(), exception);
|
||||
|
||||
/**
|
||||
* Builds a {@link ThreadFactory} using the specified {@code String} name-format, normal thread priority and the
|
||||
* default {@link UncaughtExceptionHandler}.
|
||||
*
|
||||
* @see #DEFAULT_EXCEPTION_HANDLER
|
||||
*
|
||||
* @param name The name-format used when creating threads, may not be {@code null}.
|
||||
* @return A new {@link ThreadFactory} from the specified parameters, never {@code null}.
|
||||
* @see {@link #DEFAULT_EXCEPTION_HANDLER}
|
||||
* @see {@link ThreadPriority#NORMAL_PRIORITY}
|
||||
*/
|
||||
public static ThreadFactory build(String name) {
|
||||
return build(name, ThreadPriority.NORMAL_PRIORITY, DEFAULT_EXCEPTION_HANDLER);
|
||||
public static ThreadFactory create(String name) {
|
||||
return create(name, Thread.NORM_PRIORITY, DEFAULT_EXCEPTION_HANDLER);
|
||||
}
|
||||
|
||||
/**
|
||||
* An enumeration representing the priority of a {@link Thread}.
|
||||
* Builds a {@link ThreadFactory} using the specified {@code String} name-format, priority and the
|
||||
* {@link #DEFAULT_EXCEPTION_HANDLER}.
|
||||
*
|
||||
* @author Ryley Kimmel <ryley.kimmel@live.com>
|
||||
* @param name The name-format used when creating threads, may not be {@code null}.
|
||||
* @param priority The priority used when creating threads.
|
||||
* @return A new {@link ThreadFactory} from the specified parameters, never {@code null}.
|
||||
*/
|
||||
public enum ThreadPriority {
|
||||
|
||||
/**
|
||||
* Represents the minimum priority of a thread.
|
||||
*/
|
||||
MINIMUM_PRIORITY(1),
|
||||
|
||||
/**
|
||||
* Represents the normal priority of a thread.
|
||||
*/
|
||||
NORMAL_PRIORITY(5),
|
||||
|
||||
/**
|
||||
* Represents the maximum priority of a thread.
|
||||
*/
|
||||
MAXIMUM_PRIORITY(10);
|
||||
|
||||
/**
|
||||
* The value of this thread priority.
|
||||
*/
|
||||
private final int value;
|
||||
|
||||
/**
|
||||
* Constructs a new {@link ThreadPriority} with the specified value.
|
||||
*
|
||||
* @param value The value of this thread priority, must be within the bounds of {@link Thread#MIN_PRIORITY} and
|
||||
* {@link Thread#MAX_PRIORITY}.
|
||||
*/
|
||||
private ThreadPriority(int value) {
|
||||
// fail-fast for invalid priority values
|
||||
checkArgument(value >= Thread.MIN_PRIORITY, "Thread priority (%s) must be >= %s", value, Thread.MIN_PRIORITY);
|
||||
checkArgument(value <= Thread.MAX_PRIORITY, "Thread priority (%s) must be <= %s", value, Thread.MAX_PRIORITY);
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of this thread priority.
|
||||
*/
|
||||
public final int getValue() {
|
||||
return value;
|
||||
}
|
||||
public static ThreadFactory create(String name, int priority) {
|
||||
return create(name, priority, DEFAULT_EXCEPTION_HANDLER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevents the default-public constructor to discourage instantiation of this class.
|
||||
* Builds a {@link ThreadFactory} using the specified {@code String} name-format, priority and
|
||||
* {@link UncaughtExceptionHandler}.
|
||||
*
|
||||
* @param name The name-format used when creating threads. Must not be {@code null}.
|
||||
* @param priority The priority used when creating threads.
|
||||
* @param handler The {@link UncaughtExceptionHandler} used when creating threads. Must not be {@code null}.
|
||||
* @return A new {@link ThreadFactory} using the specified parameters.
|
||||
*/
|
||||
public static ThreadFactory create(String name, int priority, UncaughtExceptionHandler handler) {
|
||||
Objects.requireNonNull(priority);
|
||||
|
||||
ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
|
||||
builder.setNameFormat(name);
|
||||
builder.setPriority(priority);
|
||||
builder.setUncaughtExceptionHandler(handler);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sole private constructor to prevent instantiation.
|
||||
*/
|
||||
private ThreadUtil() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user