package com.hdl.sdk.sourceos.utils.thread;
|
|
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
/**
|
* Created by Tong on 2023/05/09.
|
*/
|
public abstract class RenameThreadFactory implements ThreadFactory {
|
|
private static final AtomicInteger poolNumber = new AtomicInteger(1);
|
private final ThreadGroup group;
|
private final AtomicInteger threadNumber = new AtomicInteger(1);
|
|
public abstract String getName(int poolNumber, int threadNumber);
|
|
private Thread.UncaughtExceptionHandler exceptionHandler;
|
|
public RenameThreadFactory() {
|
SecurityManager s = System.getSecurityManager();
|
this.group = s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
|
}
|
|
|
public RenameThreadFactory(Thread.UncaughtExceptionHandler exceptionHandler) {
|
this.exceptionHandler = exceptionHandler;
|
SecurityManager s = System.getSecurityManager();
|
this.group = s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
|
}
|
|
public Thread newThread(Runnable r) {
|
Thread t = new Thread(this.group, r, this.getName(poolNumber.getAndIncrement(), this.threadNumber.getAndIncrement()), 0L);
|
if (t.isDaemon()) {
|
t.setDaemon(false);
|
}
|
|
if (t.getPriority() != Thread.NORM_PRIORITY) {
|
t.setPriority(Thread.NORM_PRIORITY);
|
}
|
if (exceptionHandler != null) {
|
t.setUncaughtExceptionHandler(exceptionHandler);
|
}
|
|
|
return t;
|
}
|
}
|