package com.taoban.util;
/**
* 执行单次任务或定时任务工具类(用于减少new Thread()和new Timer()的使用)
*/
public class TaskUtil {
private static Log log = LogFactory.getLog(TaskUtil.class);
private static ExecutorService cachedExecutor = null;
private static ScheduledExecutorService scheduledExecutor = null;
private static Map<Runnable, Future<?>> keepRunningTasks = null;
private static Map<Future<?>, Callback> callbackdTasks = null;
static {
cachedExecutor = Executors.newCachedThreadPool(new TaskUtilThreadFactory("cached"));
scheduledExecutor = Executors.newScheduledThreadPool(5, new TaskUtilThreadFactory("scheduled"));
Runtime.getRuntime().addShutdownHook(new Thread() {//线程池自动退出
@Override
public void run() {
cachedExecutor.shutdown();
scheduledExecutor.shutdown();
log.info("TaskUtil executors shutdown.");
}
});
}
/**
* 立即执行任务
*/
public static Future<?> submit(Runnable task) {
return cachedExecutor.submit(task);
}
/**
* 自动保持任务持续运行,每分钟监视一次
*/
public static Future<?> submitKeepRunning(Runnable task){
Future<?> future = submit(task);
checkInitCachedTasks();
synchronized (keepRunningTasks) {
keepRunningTasks.put(task, future);
}
return future;
}
/**
* 延迟执行任务,例如延迟5秒:schedule(task,5,TimeUnit.SECONDS)
*/
public static void schedule(Runnable task, long delay, TimeUnit unit) {
scheduledExecutor.schedule(task, delay, unit);
}
/**
* 定时执行任务一次,比如下午两点:scheduleAt(task, DateUtils.setHours(new Date(), 13))
*/
public static void scheduleAt(Runnable task, Date time) {
long mills = time.getTime() - System.currentTimeMillis();
scheduledExecutor.schedule(task, mills>0 ? mills : 3, TimeUnit.MILLISECONDS);
}
/**
* 定时重复执行任务,比如延迟5秒,每10分钟执行一次:scheduleAtFixRate(task, 5, TimeUnit.MINUTES.toSeconds(10), TimeUnit.SECONDS)
*/
public static void scheduleAtFixtRate(Runnable task, long initialDelay, long delay, TimeUnit unit) {
scheduledExecutor.scheduleWithFixedDelay(task, initialDelay, delay, unit);
}
/**
* 定时重复执行任务,比如下午两点开始,每小时执行一次:scheduleAtFixRate(task, DateUtils.setHours(new Date(), 13), 1, TimeUnit.HOURS)
*/
public static void scheduleAtFixtRate(Runnable task, Date time, long delay, TimeUnit unit) {
long mills = time.getTime() - System.currentTimeMillis();
scheduledExecutor.scheduleWithFixedDelay(task, mills>0 ? mills : 3, unit.toMillis(delay), TimeUnit.MILLISECONDS);
}
/**
* 提交带返回值的任务,支持后续处理(调用者手动处理)
*/
public static <T> Future<T> submit(Callable<T> task) {
return cachedExecutor.submit(task);
}
/**
* 提交带返回值的任务,支持后续处理(自动调用Callback接口)
*/
public static <T> Future<T> submit(Callable<T> task, Callback callback) {
Future<T> future = submit(task);
checkInitCachedTasks();
if(callback != null) {
synchronized (callbackdTasks) {
callbackdTasks.put(future, callback);
}
}
return future;
}
/**
* 提交任务,等待返回值(阻塞调用者)
*/
public static <T> T wait(Callable<T> task) {
Future<T> future = cachedExecutor.submit(task);
try {
return future.get();
} catch (Exception e) {
log.warn(e);
return null;
}
}
private static void checkInitCachedTasks() {
if(keepRunningTasks != null) return;
keepRunningTasks = new HashMap<Runnable, Future<?>>();
callbackdTasks = new HashMap<Future<?>, Callback>();
scheduleAtFixtRate(new CachedTasksMonitor(), 1, 1, TimeUnit.MINUTES);
}
/**
* 监视需要保持运行的任务
*/
static class CachedTasksMonitor implements Runnable {
@Override
public void run() {
if(keepRunningTasks.size() > 0) {
synchronized (keepRunningTasks) {
Map<Runnable, Future<?>> tempTasks = null;
for(Runnable task : keepRunningTasks.keySet()) {
Future<?> future = keepRunningTasks.get(task);
if(future.isDone()) {
future = submit(task);//恢复运行异常结束任务
if(tempTasks == null) tempTasks = new HashMap<Runnable, Future<?>>();
tempTasks.put(task, future);
}
}
if(tempTasks != null && tempTasks.size() > 0) keepRunningTasks.putAll(tempTasks);
}
}
if(callbackdTasks.size() > 0) {
synchronized (callbackdTasks) {
List<Future<?>> callbackedFutures = null;
for(Future<?> future : callbackdTasks.keySet()) {
final Callback callback = callbackdTasks.get(future);
if(future.isDone()) {
try{
final Object result = future.get(5, TimeUnit.SECONDS);
submit(new Runnable() {
@Override
public void run() {//callback可能耗时所以作为独立运行任务,而本监视器需尽快完成工作
callback.handle(result);
}
});
if(callbackedFutures == null) callbackedFutures = new LinkedList<Future<?>>();
callbackedFutures.add(future);
}catch (Exception e) {
log.warn("TaskUtil callbackedTasks warn: ", e);
}
}
}
if(callbackedFutures != null && callbackedFutures.size() > 0) {
for(Future<?> future : callbackedFutures) {
callbackdTasks.remove(future);
}
}
}
}
}
}
/**
* 自定义线程名称Task-idx-name-idx2
*/
static class TaskUtilThreadFactory implements ThreadFactory {
private final static AtomicInteger taskutilThreadNumber = new AtomicInteger(1);
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String threadNamePrefix;
TaskUtilThreadFactory(String threadNamePrefix){
this.threadNamePrefix = threadNamePrefix;
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, String.format("TaskUtil-%d-%s-%d", taskutilThreadNumber.getAndIncrement(), this.threadNamePrefix, threadNumber.getAndIncrement()));
t.setDaemon(false);
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
/**
* 等待结果回调接口
*/
public static interface Callback {
void handle(Object result);
}
}
- spring多线程与定时任务
本篇主要描述一下spring的多线程的使用与定时任务的使用. 1.spring多线程任务的使用 spring通过任务执行器TaskExecutor来实现多线程与并发编程.通常使用ThreadPoolT ...
- Java多线程之定时任务(Timer)
package org.study2.javabase.ThreadsDemo.schedule; import java.util.Date; import java.util.Timer; imp ...
- spring的@Scheduled定时任务,同一时间段的定时任务只会执行一个,其余的会被阻塞,@Scheduled注解定时任务并发执行解决办法,即多线程运行定时任务
原文:https://blog.csdn.net/qq_35937303/article/details/88851064 现有两个定时任务 @Component("aa") pu ...
- Spring Boot 定时任务单线程和多线程
Spring Boot 的定时任务: 第一种:把参数配置到.properties文件中: 代码: package com.accord.task; import java.text.SimpleDat ...
- Spring4.0编程式定时任务配置
看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...
- JAVA基础知识之多线程——线程组和未处理异常
线程组 Java中的ThreadGroup类表示线程组,在创建新线程时,可以通过构造函数Thread(group...)来指定线程组. 线程组具有以下特征 如果没有显式指定线程组,则新线程属于默认线程 ...
- java多线程编程核心技术——第五章总结
定时器Timer的使用 1.1方法schedule(TimerTask task, Date time)的测试 1.2方法schedule(TimerTask task, Date firstTime ...
- springBoot中使用定时任务
简单示例 导入依赖 springBoot已经默认集成了定时任务的依赖,只需要引入基本的依赖就可以使用定时任务. <parent> <groupId>org.springfram ...
- Java编程的逻辑 (80) - 定时任务的那些坑
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
随机推荐
- MAT(1) 小样
一.内存溢出时生成hprof文件 运行参数: -Xms40m -Xmx40m -Xmn20m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=E:\J ...
- Ehcache详细解读
[http://raychase.iteye.com/blog/1545906] Ehcache 是现在最流行的纯Java开源缓存框架. [通过编程方式使用EhCache ] //从class ...
- 【转】Android图片加载神器之Fresco-加载图片基础[详细图解Fresco的使用]
Fresco简单的使用—SimpleDraweeView 百学须先立志—学前须知: 在我们平时加载图片(不管是下载还是加载本地图片…..)的时候,我们经常会遇到这样一个需求,那就是当图片正在加载时应该 ...
- ThreadPool for Delphi
http://sourceforge.net/projects/threadpoolpas/ http://hivelocity.dl.sourceforge.net/project/threadpo ...
- PostgreSQL下,对汉字按拼音排序
参考学习此文: http://blog.163.com/digoal@126/blog/static/163877040201173003547236/ 建库 postgres=# \l List o ...
- ISA中的WEB链
在ISA Server 2004中提供了Web链功能,它就相当于将ISA Server配置为二级代理,可以将你的请求转发到上游的代理服务器或其他站点.使用Web链,你就可以实现条件路由,对不同的目的地 ...
- Codeforces Gym 100803F There is No Alternative 暴力Kruskal
There is No Alternative 题目连接: http://codeforces.com/gym/100803/attachments Description ICPC (Isles o ...
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog dp
B. Longtail Hedgehog 题目连接: http://www.codeforces.com/contest/615/problem/B Description This Christma ...
- ASP.NET Jquery+ajax上传文件(带进度条)
效果图 支持ie6+,chrome,ie6中文文件名会显示乱码. 上传时候会显示进度条. 需要jquery.uploadify.js插件,稍后会给出下载 前台代码 <%@ Page Langua ...
- C#窗体钉在桌面、置底、嵌入桌面的办法
想做一个桌面时钟,钉在桌面上不影响正常使用,只在看桌面的时候显示. 从网上多方寻找找到这么个代码,但是还是有不方便的地方,大家探讨一下. 这个程序在使用“显示桌面”的时候还可以显示,将程序的Form1 ...