springboot异步多线程返回结果

一 自定义线程池

package com.example.demo.config;

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

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author cc
 * @Date 2020/7/28 10:45
 * @Version 1.0
 * 线程池配置
 */
@Configuration
@EnableAsync
public class ThreadPoolTaskConfig {
    /**
     *   默认情况下,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务,
     *	当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中;
     *  当队列满了,就继续创建线程,当线程数量大于等于maxPoolSize后,开始使用拒绝策略拒绝
     */

    /** 核心线程数(默认线程数) */
    private static final int corePoolSize = 15;
    /** 最大线程数 */
    private static final int maxPoolSize = 50;
    /** 允许线程空闲时间(单位:默认为秒) */
    private static final int keepAliveTime = 60;
    /** 缓冲队列大小 */
    private static final int queueCapacity = 100;
    /** 线程池名前缀 */
    private static final String threadNamePrefix = "Async-Service-";

    @Bean("taskExecutor") // bean的名称,默认为首字母小写的方法名
    public ThreadPoolTaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(keepAliveTime);
        executor.setThreadNamePrefix(threadNamePrefix);

        // 线程池对拒绝任务的处理策略
        // CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

}

二 异步执行方法

启动类添加@EnableAsync注解

@SpringBootApplication
@EnableAsync
@EnableScheduling
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

service层方法

@Service
public class AsyncService {
    Logger log = LoggerFactory.getLogger(AsyncService.class);

    // 发送提醒短信 1
    @Async("taskExecutor")// 指定线程池,也可以直接写@Async
    public void sendMessage1() throws InterruptedException {
        log.info("发送短信方法---- 1   执行开始");
        Thread.sleep(5000); // 模拟耗时
        log.info("发送短信方法---- 1   执行结束" + new Date());
    }

    // 发送提醒短信 2
    @Async("taskExecutor") // 指定线程池,也可以直接写@Async
    public void sendMessage2() throws InterruptedException {

        log.info("发送短信方法---- 2   执行开始");
        Thread.sleep(2000); // 模拟耗时
        log.info("发送短信方法---- 2   执行结束" + new Date());
    }

    // 发送提醒短信 3
    @Async("taskExecutor") // 指定线程池,也可以直接写@Async
    public Future<String> sendMessage3() throws InterruptedException {

        log.info("发送短信方法---- 3   执行开始"+ new Date());
        Thread.sleep(2000); // 模拟耗时
        log.info("发送短信方法---- 3   执行结束" + new Date());
        return new AsyncResult<String>("发送短信3");
    }

    // 发送提醒短信 3
    @Async("taskExecutor") // 指定线程池,也可以直接写@Async
    public Future<String> sendMessage4() throws InterruptedException {

        log.info("发送短信方法---- 4   执行开始"+ new Date());
        Thread.sleep(2000); // 模拟耗时
        log.info("发送短信方法---- 4   执行结束" + new Date());
        return new AsyncResult<String>("发送短信4");
    }
}

三 多线程返回结果

	@GetMapping("/b")
    public String b() throws InterruptedException, ExecutionException {
        Future<String> sendMessage3 = asyncService.sendMessage3();
        Future<String> sendMessage4 = asyncService.sendMessage4();
        String result="hello world";
        System.out.println(new Date());
        while (!sendMessage3.isDone() || !sendMessage4.isDone()){
            Thread.sleep(50);
            result += sendMessage3.get();
            result += sendMessage4.get();
        }
        System.out.println(new Date());
        return result;
    }

日志

Thu Aug 06 10:46:33 CST 2020
2020-08-06 10:46:33.703  INFO 9688 --- [Async-Service-3] com.example.demo.service.AsyncService    : 发送短信方法---- 3   执行开始Thu Aug 06 10:46:33 CST 2020
2020-08-06 10:46:33.703  INFO 9688 --- [Async-Service-2] com.example.demo.service.AsyncService    : 发送短信方法---- 4   执行开始Thu Aug 06 10:46:33 CST 2020
2020-08-06 10:46:35.704  INFO 9688 --- [Async-Service-2] com.example.demo.service.AsyncService    : 发送短信方法---- 4   执行结束Thu Aug 06 10:46:35 CST 2020
2020-08-06 10:46:35.704  INFO 9688 --- [Async-Service-3] com.example.demo.service.AsyncService    : 发送短信方法---- 3   执行结束Thu Aug 06 10:46:35 CST 2020
Thu Aug 06 10:46:35 CST 2020

接口返回
在这里插入图片描述

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以使用多种方式来实现多线程返回结果。以下是其中一种常见的方法: 1. 使用Callable和Future接口:可以创建一个实现了Callable接口的任务,并将其提交给线程池进行执行。该任务可以返回一个Future对象,通过该对象可以获取任务的执行结果。 示例代码如下: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.async.DeferredResult; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @RestController public class MyController { private ExecutorService executorService = Executors.newFixedThreadPool(10); @GetMapping("/async") public DeferredResult<String> asyncRequest() { DeferredResult<String> deferredResult = new DeferredResult<>(); Callable<String> callable = () -> { // 执行耗时操作 Thread.sleep(1000); return "Result"; }; Future<String> future = executorService.submit(callable); future.addListener(() -> { try { String result = future.get(); deferredResult.setResult(result); // 设置结果到DeferredResult } catch (Exception e) { deferredResult.setErrorResult(e.getMessage()); } }, executorService); return deferredResult; } } ``` 在上述示例中,通过`@GetMapping("/async")`标记一个异步请求的接口。在接口方法中,创建了一个实现了Callable接口的任务,并将其提交给线程池进行执行。使用`addListener`方法对Future对象进行监听,在任务执行完成后,将结果设置到DeferredResult对象中,从而实现异步返回结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值