博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring配置线程池-异步执行任务
阅读量:6207 次
发布时间:2019-06-21

本文共 3211 字,大约阅读时间需要 10 分钟。

hot3.png

    系统中有个定时器,针对每个用户定时生成报告。但是每个报告需要消耗3~5秒,所以在定时器里不能去处理,然后就想到线程池,在定时器里只需要启动线程就行了,所有业务全在另起的线程里进行。

    spring的配置如下

    <-- 队列集合 -->    
        
        <-- 线程池 -->    
        
        
        
        
        
        
        
        
        
    

java代码

import java.util.concurrent.BlockingQueue;import java.util.concurrent.RejectedExecutionHandler;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;public class AsyncExecutorService {    private ThreadPoolExecutor executorService;    private RejectedExecutionHandler rejectionHandler;    private BlockingQueue
 blockingQueue;    private int defaultCoreSize;    private int defaultMaxSize;    private long defaultKeepAliveTime;    public RejectedExecutionHandler getRejectionHandler() {        return rejectionHandler;    }    public void setRejectionHandler(RejectedExecutionHandler rejectionHandler) {        this.rejectionHandler = rejectionHandler;        executorService.setRejectedExecutionHandler(rejectionHandler);    }    public BlockingQueue
 getBlockingQueue() {        return blockingQueue;    }    public void setBlockingQueue(BlockingQueue
 blockingQueue) {        this.blockingQueue = blockingQueue;    }    public ThreadPoolExecutor getExecutorService() {        return executorService;    }    public void setExecutorService(ThreadPoolExecutor executorService) {        this.executorService = executorService;        this.defaultCoreSize = this.executorService.getCorePoolSize();        this.defaultMaxSize = this.executorService.getMaximumPoolSize();        this.defaultKeepAliveTime = this.executorService.getKeepAliveTime(TimeUnit.SECONDS);    }            public void execute(Runner runner){        executorService.execute(runner);    }        public void resizePoolMaxSize(int maxSize){        this.executorService.setMaximumPoolSize(maxSize);    }    public void resizePoolCoreSize(int coreSize){        this.executorService.setMaximumPoolSize(coreSize);    }    public void resetPoolAliveTime(long aliveTime){        this.executorService.setKeepAliveTime(aliveTime, TimeUnit.SECONDS);    }        public void resetDefault(){        this.executorService.setCorePoolSize(this.defaultCoreSize);        this.executorService.setMaximumPoolSize(this.defaultMaxSize);        this.executorService.setKeepAliveTime(this.defaultKeepAliveTime, TimeUnit.SECONDS);    }        @PostConstruct    public void init(){        //asyncExecutorService will be init !!!    }    @PreDestroy    public void destroy(){        //asyncExecutorService will be destroyed !!!        executorService.shutdown();    }    public Integer getPoolMaxSize() {        return executorService.getMaximumPoolSize();    }}

测试(调用)代码

public class Test{    @Autowired    private  AsyncExecutorService asyncExecutorService;    public void startTask(){       asyncExecutorService.execute(()->{             syso("run");        });       }    }

 

转载于:https://my.oschina.net/shyloveliyi/blog/470270

你可能感兴趣的文章
MAC下面maven如何设置让其实下载源码
查看>>
查看NVIDIA使用率工具目录
查看>>
PostgreSQL学习手册(二) 模式(Schema)
查看>>
[iOS Animation]-CALayer 性能优化实例
查看>>
Nagios 安装及常见错误
查看>>
我的友情链接
查看>>
python模块学习(1)
查看>>
Groovy在Spring中的简单使用实例
查看>>
Linux 下的 hosts文件
查看>>
升级nginx,查看已经安装的模块,并隐藏或者修改版本号
查看>>
判断 CGRect是否“为空”
查看>>
MySQL日期时间函数大全(转)
查看>>
Android版添加phonegap--websocket客户端插件教程
查看>>
android控件的隐藏与显示
查看>>
VMware下配置固定ip,于本机进行通信。
查看>>
操作系统基础
查看>>
Linux环境下安装部署AWStats日志分析系统实例
查看>>
vim: vimrc
查看>>
防火墙配置十大任务之十,构建虚拟防火墙
查看>>
vim编辑器之按键说明
查看>>