Use custom thread factory by default to better recognize the threads
... from their names
This commit is contained in:
parent
61e169e53a
commit
04cf1c8199
@ -5,7 +5,6 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
@ -15,8 +14,8 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
|
|
||||||
public abstract class MultithreadEventExecutor implements EventExecutor {
|
public abstract class MultithreadEventExecutor implements EventExecutor {
|
||||||
|
|
||||||
protected static final int DEFAULT_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
|
private static final int DEFAULT_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
|
||||||
protected static final ThreadFactory DEFAULT_THREAD_FACTORY = Executors.defaultThreadFactory();
|
private static final AtomicInteger poolId = new AtomicInteger();
|
||||||
|
|
||||||
private final EventExecutor[] children;
|
private final EventExecutor[] children;
|
||||||
private final AtomicInteger childIndex = new AtomicInteger();
|
private final AtomicInteger childIndex = new AtomicInteger();
|
||||||
@ -32,7 +31,7 @@ public abstract class MultithreadEventExecutor implements EventExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected MultithreadEventExecutor(int nThreads, Object... args) {
|
protected MultithreadEventExecutor(int nThreads, Object... args) {
|
||||||
this(nThreads, DEFAULT_THREAD_FACTORY, args);
|
this(nThreads, null, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected MultithreadEventExecutor(int nThreads, ThreadFactory threadFactory, Object... args) {
|
protected MultithreadEventExecutor(int nThreads, ThreadFactory threadFactory, Object... args) {
|
||||||
@ -40,8 +39,9 @@ public abstract class MultithreadEventExecutor implements EventExecutor {
|
|||||||
throw new IllegalArgumentException(String.format(
|
throw new IllegalArgumentException(String.format(
|
||||||
"nThreads: %d (expected: > 0)", nThreads));
|
"nThreads: %d (expected: > 0)", nThreads));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (threadFactory == null) {
|
if (threadFactory == null) {
|
||||||
throw new NullPointerException("threadFactory");
|
threadFactory = new DefaultThreadFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
children = new SingleThreadEventExecutor[nThreads];
|
children = new SingleThreadEventExecutor[nThreads];
|
||||||
@ -201,4 +201,31 @@ public abstract class MultithreadEventExecutor implements EventExecutor {
|
|||||||
}
|
}
|
||||||
return loop;
|
return loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final class DefaultThreadFactory implements ThreadFactory {
|
||||||
|
private final AtomicInteger nextId = new AtomicInteger();
|
||||||
|
private final String prefix;
|
||||||
|
|
||||||
|
DefaultThreadFactory() {
|
||||||
|
String typeName = MultithreadEventExecutor.this.getClass().getSimpleName();
|
||||||
|
typeName = "" + Character.toLowerCase(typeName.charAt(0)) + typeName.substring(1);
|
||||||
|
prefix = typeName + '-' + poolId.incrementAndGet() + '-';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Thread newThread(Runnable r) {
|
||||||
|
Thread t = new Thread(r, prefix + nextId.incrementAndGet());
|
||||||
|
try {
|
||||||
|
if (t.isDaemon()) {
|
||||||
|
t.setDaemon(false);
|
||||||
|
}
|
||||||
|
if (t.getPriority() != Thread.MAX_PRIORITY) {
|
||||||
|
t.setPriority(Thread.MAX_PRIORITY);
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
// Doesn't matter even if failed to set.
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ import java.util.concurrent.BlockingQueue;
|
|||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.DelayQueue;
|
import java.util.concurrent.DelayQueue;
|
||||||
import java.util.concurrent.Delayed;
|
import java.util.concurrent.Delayed;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.FutureTask;
|
import java.util.concurrent.FutureTask;
|
||||||
import java.util.concurrent.RejectedExecutionException;
|
import java.util.concurrent.RejectedExecutionException;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
@ -68,10 +67,6 @@ public abstract class SingleThreadEventExecutor extends AbstractExecutorService
|
|||||||
private long lastCheckTimeNanos;
|
private long lastCheckTimeNanos;
|
||||||
private long lastPurgeTimeNanos;
|
private long lastPurgeTimeNanos;
|
||||||
|
|
||||||
protected SingleThreadEventExecutor() {
|
|
||||||
this(Executors.defaultThreadFactory());
|
|
||||||
}
|
|
||||||
|
|
||||||
protected SingleThreadEventExecutor(ThreadFactory threadFactory) {
|
protected SingleThreadEventExecutor(ThreadFactory threadFactory) {
|
||||||
thread = threadFactory.newThread(new Runnable() {
|
thread = threadFactory.newThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -4,8 +4,6 @@ import java.util.concurrent.ThreadFactory;
|
|||||||
|
|
||||||
public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop {
|
public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop {
|
||||||
|
|
||||||
protected SingleThreadEventLoop() {}
|
|
||||||
|
|
||||||
protected SingleThreadEventLoop(ThreadFactory threadFactory) {
|
protected SingleThreadEventLoop(ThreadFactory threadFactory) {
|
||||||
super(threadFactory);
|
super(threadFactory);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user