关于异步:Spring-AOP-异步操作

9次阅读

共计 2202 个字符,预计需要花费 6 分钟才能阅读完成。

Spring 业务的异步实现

在基于注解形式的配置中,借助 @EnableAsync 注解进行异步启动申明
(1)在 Spring Boot 我的项目中,在启动类 Application 上利用 @EnableAsync 注解
(2)在须要异步执行的业务办法上,应用 @Async 办法进行异步申明
如果须要获取业务层异步办法的执行后果,AsyncResult 对象能够对异步办法的执行后果进行封装,如果外界须要异步办法后果时,能够通过 Future 对象的 get 办法获取后果

Spring 自定义异步池

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
public class TestThreadPool {public static void main(String[] args) {BlockingQueue<Runnable> workQueue=new ArrayBlockingQueue<Runnable>(1);// 线程容量:阻塞队列数 + 最大线程数
        // 如果默认线程名不满足你业务须要, 能够本人创立线程工厂, 而后定义线程名
        ThreadFactory threadFactory=new ThreadFactory() {
            //AtomicLong 提供了一种线程平安的自增或自减算法对一个整数进行计算
            private AtomicLong al=new AtomicLong(1);
            @Override
            public Thread newThread(Runnable r) {return new Thread(r, "db-thread-"+al.getAndIncrement());
            }
        };
        //1. 构建一个线程池
        ThreadPoolExecutor tp=new ThreadPoolExecutor(2,//corePoolSize 外围线程数(当应用池对象执行工作时, 池中线程没有达到 corePoolSize 设置定值时, 每来一个新的工作都会创立一个新的线程)
                3,//maximumPoolSize 最大线程(当外围线程都在忙, 队列也满, 再来新的工作则创立新线程)
                60,//keepAliveTime 最大闲暇工夫
                TimeUnit.SECONDS, //unit 工夫单位
                workQueue,//workQueue 阻塞式队列
                threadFactory,// 创立线程的工厂
                new ThreadPoolExecutor.CallerRunsPolicy());// 工作拒绝执行策略, 这里抉择了 CallerRunsPolicy 对象(示意最初由调用者线程执行)
        //2. 启动池中线程执行工作
        
        tp.execute(new Runnable() {
            @Override
            public void run() {String tName=Thread.currentThread().getName();
                System.out.println(tName+"execute task 01");
                try{Thread.sleep(5000);}catch (Exception e) {}}
        });
        tp.execute(new Runnable() {
            @Override
            public void run() {String tName=Thread.currentThread().getName();
                System.out.println(tName+"execute task 02");
                try{Thread.sleep(5000);}catch (Exception e) {}}
        });
        tp.execute(new Runnable() {
            @Override
            public void run() {String tName=Thread.currentThread().getName();
                System.out.println(tName+"execute task 03");
                try{Thread.sleep(5000);}catch (Exception e) {}}
        });
        tp.execute(new Runnable() {
            @Override
            public void run() {String tName=Thread.currentThread().getName();
                System.out.println(tName+"execute task 04");
                try{Thread.sleep(5000);}catch (Exception e) {}}
        });
        tp.execute(new Runnable() {
            @Override
            public void run() {String tName=Thread.currentThread().getName();
                System.out.println(tName+"execute task 05");
                try{Thread.sleep(5000);}catch (Exception e) {}}
        });    
    }
}

执行后果

正文完
 0