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

SpringBoot学习笔记45——通过注解开启线程异步执行方法

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

SpringBoot学习笔记45——通过注解开启线程异步执行方法

今天来记录一下通过注解来实现异步操作的功能。

在这里我们需要用到两个注解:

@EnableAsync        //用来开启异步执行的监听
@Async        //用来标注异步方法

通过这两个注解可以很简单的实现异步执行方法 ,也就是开启多线程。

1.在启动类处开启添加@EnableAsync注解

package com.youyou.sso;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@ServletComponentScan
@SpringBootApplication
@EnableAsync
public class ApiServerApplication {
    
    public static void main(String[] args) {

        SpringApplication.run(ApiServerApplication.class, args);
    }
    
}

2.在service处添加@Async注解

package com.youyou.sso.service.impl;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


@Service
public class AsyncTestService {

//    @Async("asyncServiceExecutor")
    @Async()
    public  void test1() throws InterruptedException {
        System.out.println(1);
        Thread.sleep(10000);
        System.out.println(2);
    }
//    @Async("asyncServiceExecutor")
    @Async()
    public  void test2() throws InterruptedException {
        System.out.println(3);
        Thread.sleep(10000);
        System.out.println(4);
    }
}

3.测试

package com.youyou.sso.controller;

import com.youyou.sso.bean.UserInfo;
import com.youyou.sso.service.ISsoService;
import com.youyou.sso.service.impl.AsyncTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Api(value = "异步测试", tags = "testAsync")
@RestController("")
@RequestMapping("/testAsync")
public class AsyncTestController {

    @Autowired
    private AsyncTestService asyncTestService;


    @ApiOperation(value = "|test|")
    @GetMapping("/test")
    public void login() throws InterruptedException {
        asyncTestService.test1();
        asyncTestService.test2();
    }


}

调用此接口时控制台会输出如下:

 搞定!

进阶内容

我们可以自己进行线程池的配置

package com.youyou.sso.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.Executor;
import java.util.concurrent.ThreadPoolExecutor;

//@Configuration
//@EnableAsync
public class ExecutorConfig {


    //    @Bean(name = "asyncServiceExecutor")
//    @Bean
    public Executor executor() {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //配置核心线程数
        executor.setCorePoolSize(5);
        //配置最大线程数
        executor.setMaxPoolSize(5);
        //配置队列大小
        executor.setQueueCapacity(99999);
        //配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix("async-service-");

        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }

//    @Bean(name = "asyncServiceExecutor1")
    public Executor asyncServiceExecutor() {
        return executor();
    }
}

 在bean注解的name处指定线程池的名字,并且在使用@Async注解时添加此线程池的名字就可以了


 

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

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

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