栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot使用自定义线程池

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot使用自定义线程池

使用场景:

方法处理到某一步,需要将信息交给另一个线程去处理!!

第一种:最简单的Runnable

//创建一个Runnable,重写run方法
public Runnable dealMsg(String msg){
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("新开线程中处理:"+msg);
            }
        };

        return runnable;
    }
 public void test(String msg){
        System.out.println(Thread.currentThread().getName()+":"+msg);
        Runnable runnable = dealMsg(msg);
    //将返回的runnable对象传入,并start()启动线程
     new Thread(runnable).start();
    }

第二种:自己创建JDK线程池,交给spring管理,然后将任务交给线程池即可

1.创建线程池,交给spring管理

import com.example.recordlog.threadFactory.CustomThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;



@Configuration
public class ThreadPoolConfig {

    @Bean(name = "customThreadExecutor")
    public ExecutorService nlpFeignExecutor() throws Exception {
        ExecutorService executor = new ThreadPoolExecutor(4, 8,
                0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(5), new CustomThreadFactory("custom-Thread-pool"), new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                try {
                    e.getQueue().put(r);
                } catch (InterruptedException e1) {
                    Thread.currentThread().interrupt();
                }
            }
        });
        return executor;
    }

}

自定义实现ThreadFactory

import java.util.concurrent.atomic.AtomicInteger;



public class CustomThreadFactory implements java.util.concurrent.ThreadFactory {


    private AtomicInteger threadNo = new AtomicInteger(1);

    private final String nameStart;

    private final String nameEnd = "]";

    public CustomThreadFactory(String poolName) {
        this.nameStart = "[" + poolName + "-";
    }

    @Override
    public Thread newThread(Runnable r) {
        String threadName = this.nameStart + this.threadNo.getAndIncrement() + "]";
        Thread newThread = new Thread(r, threadName);
        newThread.setDaemon(true);
        if (newThread.getPriority() != 5) {
            newThread.setPriority(5);
        }
        return newThread;
    }
}

使用自定义线程池

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.concurrent.ExecutorService;

@Controller
@RequestMapping("/custom")
@Slf4j
public class ThreadPoolTest {


    @Autowired
    @Qualifier(value = "customThreadExecutor")
    ExecutorService executorService;


    @RequestMapping(value = "/thread", method = RequestMethod.GET)
    public String task() {

        for (int i = 0; i < 5; i++) {
            executorService.execute(() -> {
                //实现逻辑
                System.out.println(Thread.currentThread() + "正在执行");
            });
        }
        return "OK";
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/631485.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号