springboot中运用线程池

1.线程池配置类

package com.example.demo.asyn;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class AsyncTask{

    public static ThreadPoolTaskExecutor  getAsyncExecutor() {
        // 自定义线程池
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        // 核心线程数
        taskExecutor.setCorePoolSize(10);
        // 最大线程数
        taskExecutor.setMaxPoolSize(40);
        // 线程队列最大线程数
        taskExecutor.setQueueCapacity(100);
        //设置拒绝策略
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy() );
        // 初始化线程池
        taskExecutor.initialize();
        return taskExecutor;
    }

    public static void asyncTask(Runnable asyncTask) {
        getAsyncExecutor().execute(asyncTask);
    }
}

2.Servcie业务类

package com.example.demo.asyn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {
    @Autowired
    private AsynExcuet asynExcuet;
    @Autowired
    private TestService testService;

    public void text(String s){
        
        System.out.println("kaishi");
        try {
            Thread.sleep(5000);//更好观察是否异步调用
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(s);
    }
}

3.线程调用service

package com.example.demo.asyn;

public class AsynService implements Runnable{
    
    //业务类
    private TestService testService;
    private  String s;

    public AsynService(TestService testService, String s) {
        this.testService = testService;
        this.s = s;
    }

    @Override
    public void run() {
        testService.text(s);
    }
}

4.Controller类

package com.example.demo.contrller;

import com.example.demo.asyn.AsynService;
import com.example.demo.asyn.AsyncTask;
import com.example.demo.asyn.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestContller {
    
    @Autowired
    private TestService testService;
    
    @GetMapping("/test")
    public String test(String s){
        if(s.equals("1")){//同步掉用
            testService.text(s);
        } else {//异步
            AsynService asynService = new AsynService(testService,s);
            AsyncTask.asyncTask(asynService);
        }
        return s;
    }
}

线程的一些基础知识https://blog.csdn.net/ks2356/article/details/114481284?spm=1001.2014.3001.5501
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值