panlili2024
2025-03-05 134209ad70f82051da3ce63471df0cc8f778e57d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
    }
}