JAVA-线程池创建

线程池在项目启动前通过配置初始化

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


@Configuration
@EnableAsync
public class ExecutorConfig {

    @Bean
    public ThreadPoolExecutor asyncExecutor() {
        ThreadPoolExecutor executor =
            new ThreadPoolExecutor(4, 20, 60000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(200),
                new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable r) {
                        LogUtil.info(LogEnum.CHAT_GPT,"线程" + r.hashCode() + "创建");
                        //线程命名
                        Thread th = new Thread(r, "threadPool-gpt-api-" + r.hashCode());
                        return th;
                    }
                });
        return executor;
    }

}

猜你喜欢

转载自blog.csdn.net/caryeko/article/details/141031661