60 lines
1.9 KiB
Java
60 lines
1.9 KiB
Java
|
package com.zcloud.util;
|
||
|
|
||
|
import org.apache.xmlbeans.impl.store.QNameFactory;
|
||
|
import org.springframework.context.annotation.Bean;
|
||
|
import org.springframework.stereotype.Component;
|
||
|
|
||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||
|
import java.util.concurrent.ThreadFactory;
|
||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||
|
import java.util.concurrent.TimeUnit;
|
||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||
|
|
||
|
/**
|
||
|
* @ClassName ThreadPoolHelper
|
||
|
* @Description TODO
|
||
|
* @Author Dear lin
|
||
|
* @Date 14:17 2023/2/18
|
||
|
* @Version 1.0
|
||
|
**/
|
||
|
@Component
|
||
|
public class ThreadPoolHelper {
|
||
|
static class MyNameFactory implements ThreadFactory{
|
||
|
private final AtomicInteger poolNumber = new AtomicInteger(1);
|
||
|
|
||
|
private final ThreadGroup threadGroup;
|
||
|
|
||
|
private final AtomicInteger threadNumber = new AtomicInteger(1);
|
||
|
|
||
|
public final String namePrefix;
|
||
|
MyNameFactory(String name){
|
||
|
SecurityManager s = System.getSecurityManager();
|
||
|
threadGroup = (s != null) ? s.getThreadGroup() :
|
||
|
Thread.currentThread().getThreadGroup();
|
||
|
if (null==name || "".equals(name.trim())){
|
||
|
name = "pool";
|
||
|
}
|
||
|
namePrefix = name +"-"+
|
||
|
poolNumber.getAndIncrement() +
|
||
|
"-thread-";
|
||
|
}
|
||
|
@Override
|
||
|
public Thread newThread(Runnable r) {
|
||
|
Thread t = new Thread(threadGroup, r,
|
||
|
namePrefix + threadNumber.getAndIncrement(),
|
||
|
0);
|
||
|
if (t.isDaemon()) {
|
||
|
t.setDaemon(false);
|
||
|
}
|
||
|
if (t.getPriority() != Thread.NORM_PRIORITY) {
|
||
|
t.setPriority(Thread.NORM_PRIORITY);
|
||
|
}
|
||
|
return t;
|
||
|
}
|
||
|
}
|
||
|
@Bean
|
||
|
public ThreadPoolExecutor executionOfTimedTaskThreadPool() {
|
||
|
return new ThreadPoolExecutor(7, 7, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),new MyNameFactory("ExecutionOfTimedTasks"));
|
||
|
}
|
||
|
}
|