WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
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
package tech.powerjob.server.common.thread;
 
import lombok.extern.slf4j.Slf4j;
 
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicLong;
 
/**
 * @author Echo009
 * @since 2022/10/12
 */
@Slf4j
public class NewThreadRunRejectedExecutionHandler implements RejectedExecutionHandler {
 
    private static final AtomicLong COUNTER = new AtomicLong();
 
    private final String source;
 
    public NewThreadRunRejectedExecutionHandler(String source) {
        this.source = source;
    }
 
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor p) {
        log.error("[{}] ThreadPool[{}] overload, the task[{}] will run by a new thread!, Maybe you need to adjust the ThreadPool config!", source, p, r);
        if (!p.isShutdown()) {
            String threadName = source + "-T-" + COUNTER.getAndIncrement();
            log.info("[{}] create new thread[{}] to run job", source, threadName);
            new Thread(r, threadName).start();
        }
    }
}