zoukankan      html  css  js  c++  java
  • Spring Boot|Async

    Spring Boot提供了一种通过注解@Async实现异步的方式,可以在启动类上添加注解@EnableAsyn或者在线程池配置类上添加@EnableAsync两种方式实现。

    下面重点说下线程池配置类的方式

    package com.gy.doc.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 jyy
     * @description {异步线程池}
     * @date 2020/4/29 14:35
     */
    @EnableAsync
    @Configuration
    public class AsyncThreadPoolConfig {
    
        @Bean("asyncThreadPool")
        public ThreadPoolTaskExecutor simpleThreadPool() {
            ThreadPoolTaskExecutor asyncThreadPool = new ThreadPoolTaskExecutor();
            //设置线程池最小线程数量
            asyncThreadPool.setCorePoolSize(5);
            //设置线程池最大线程数量
            asyncThreadPool.setMaxPoolSize(15);
            //设置线程池缓冲队列大小
            asyncThreadPool.setQueueCapacity(15);
            asyncThreadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            //初始化线程池
            asyncThreadPool.initialize();
            return asyncThreadPool;
        }
    }
    package com.gy.doc.service;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.scheduling.annotation.AsyncResult;
    import org.springframework.stereotype.Service;
    
    import java.util.concurrent.Future;
    
    /**
     * @author jyy
     * @description {}
     * @date 2020/4/29 14:25
     */
    @Service
    public class TestService {
    
        @Async("asyncThreadPool")
        public Future<String> asyncMethod1() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
            return new AsyncResult("1");
        }
    
        @Async("asyncThreadPool")
        public Future<String> asyncMethod2() {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
            }
            return new AsyncResult("2");
        }
    
        @Async("asyncThreadPool")
        public void asyncMethod3() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
            System.out.println("1");
        }
    
        @Async("asyncThreadPool")
        public void asyncMethod4() {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
            }
            System.out.println("2");
        }
    }

    有返回值的调用,返回类型Future<T>,当调用get()方法后,主线程等待,开始顺序执行。

        public Result test() {
            try {
                System.out.println("start" + new Date());
                Future<String> future1 = testService.asyncMethod1();
                System.out.println("middle" + new Date());
                Future<String> future2 = testService.asyncMethod2();
                System.out.println("end" + new Date());
                System.out.println(future1.get());
                System.out.println(future2.get());
                System.out.println("3");
                System.out.println(new Date());
            } catch (Exception e) {
    
            }
            return null;
        }

     无返回值调用

        public Result test() {
            try {
                System.out.println("start" + new Date());
                testService.asyncMethod3();
                testService.asyncMethod4();
                System.out.println("end" + new Date());
            } catch (Exception e) {
    
            }
            return null;
        }

     注意:切记不要在同一个类里面调用@Async声明的方法,会产生代理绕过问题,异步失效。

  • 相关阅读:
    osgEarth学习
    《C++ Primer》 第12章 类
    C++中 指针与引用的区别
    C++ primer笔记第15章 面向对象编程
    Pandas数据去重和对重复数据分类、求和,得到未重复和重复(求和后)的数据
    Python转页爬取某铝业网站上的数据
    使用Nginx+Tomcat实现分离部署
    使用icomoon字体图标详解
    如何配置PetShop网站
    nopcommerce开源框架技术总结如何实现把controller中的Model的数据传入VIEW中,并生产相应的Html代码
  • 原文地址:https://www.cnblogs.com/maikucha/p/12802149.html
Copyright © 2011-2022 走看看