博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
定时任务调度
阅读量:5117 次
发布时间:2019-06-13

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

 

import java.util.Iterator;import java.util.Map;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ScheduledFuture;import java.util.concurrent.TimeUnit;/** * 

* Description: 任务调度支持类

* 当调度执行完或者中断都需要把该任务从任务调度池中删除掉, 这样是为了避免池中垃圾对象积累。
*
* * public void run() {
*

// do something...
*
ScheduleProcessSupport.remove(key);
* } *
*

* */public class ScheduledSupport { private static Map
> scheduleFutureMap = ScheduledProcessPool.INSTANCE.getPool(); private static ScheduledExecutorService scheduledService = ScheduledThreadPool.INSTANCE.getPool(); /** *

* Description: 获取任务调度 *

* * @param key * 主键 * @return */ private static ScheduledFuture
getScheduledFutrue(String key) { ScheduledFuture
future = scheduleFutureMap.get(key); removeScheduled(key); return future; } /** *

* Description: 取消任务调度 *

* * @param key */ private static void cancelScheduled(String key) { ScheduledFuture
future = getScheduledFutrue(key); if (future != null && !future.isDone()) { future.cancel(true); } } /** *

* Description: 创建延迟任务 *

* * @param key * 主键 * @param thread * 线程 * @param delay * 延迟执行时间 * @param timeUnit * 延迟参数的时间单位 */ public static void schedule(String key, Thread thread, long delay, TimeUnit timeUnit) { cancelScheduled(key); scheduleFutureMap.put(key, scheduledService.schedule(thread, delay, timeUnit)); } /** *

* Description: 创建延迟任务 *

* * @param key * 主键 * @param thread * 线程 * @param delay * 延迟执行时间(单位:秒) */ public static void scheduleDelaySeconds(String key, Thread thread, long delay) { schedule(key, thread, delay, TimeUnit.SECONDS); } /** *

* Description: 移除任务调度 *

* * @param key * 主键 * @return */ public static void removeScheduled(String key) { if (scheduleFutureMap.containsKey(key)) { Iterator
it = scheduleFutureMap.keySet().iterator(); while (it.hasNext()) { if (it.next().equalsIgnoreCase(key)) { it.remove(); break; } } } } /** *

* Description: 获取调度池容量 *

* * @return */ public static int getScheduledPoolSize() { return scheduleFutureMap.size(); }}

 

import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ScheduledFuture;/** * 

* Description: 任务调度池 *

* */public enum ScheduledProcessPool { INSTANCE; private Map
> scheduledPool; /** 调度池初始容量 */ private int initialCapacity = 50; private ScheduledProcessPool() { scheduledPool = new ConcurrentHashMap
>(initialCapacity); } /** *

* Description: 获取任务调度池 *

* * @return */ public Map
> getPool() { return scheduledPool; }}

 

import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;/** * 

* Description: 任务线程池 *

* */public enum ScheduledThreadPool { INSTANCE; /** 线程池初始容量 */ private int corePoolSize = 50; private ScheduledExecutorService exec; private ScheduledThreadPool() { exec = Executors.newScheduledThreadPool(corePoolSize, new ThreadFactoryBuilder().setNamePrefix("VIVO-H5-Thread") .setDaemon(false).setPriority(Thread.MAX_PRIORITY).build()); } /** *

* Description: 获取任务线程池 *

* * @return */ public ScheduledExecutorService getPool() { return exec; }}

 

import java.util.concurrent.ThreadFactory;import java.util.concurrent.atomic.AtomicLong;/** * 

* Description: 自定义线程工厂 *

* */public class ThreadFactoryBuilder { private String namePrefix = null; private boolean daemon = false; private int priority = Thread.NORM_PRIORITY; public ThreadFactoryBuilder setNamePrefix(String namePrefix) { if (namePrefix == null) { throw new NullPointerException(); } this.namePrefix = namePrefix; return this; } public ThreadFactoryBuilder setDaemon(boolean daemon) { this.daemon = daemon; return this; } public ThreadFactoryBuilder setPriority(int priority) { if (priority < Thread.MIN_PRIORITY) { throw new IllegalArgumentException( String.format("Thread priority (%s) must be >= %s", priority, Thread.MIN_PRIORITY)); } if (priority > Thread.MAX_PRIORITY) { throw new IllegalArgumentException( String.format("Thread priority (%s) must be <= %s", priority, Thread.MAX_PRIORITY)); } this.priority = priority; return this; } public ThreadFactory build() { return build(this); } private static ThreadFactory build(ThreadFactoryBuilder builder) { final String namePrefix = builder.namePrefix; final Boolean daemon = builder.daemon; final Integer priority = builder.priority; final AtomicLong count = new AtomicLong(0); return new ThreadFactory() { @Override public Thread newThread(Runnable runnable) { Thread thread = new Thread(runnable); if (namePrefix != null) { thread.setName(namePrefix + "-" + count.getAndIncrement()); } if (daemon != null) { thread.setDaemon(daemon); } if (priority != null) { thread.setPriority(priority); } return thread; } }; }}

 

转载于:https://www.cnblogs.com/hellocyc/p/5819449.html

你可能感兴趣的文章
django+uwsgi+nginx+sqlite3部署+screen
查看>>
Andriod小型管理系统(Activity,SQLite库操作,ListView操作)(源代码下载)
查看>>
在Server上得到数据组装成HTML后导出到Excel。两种方法。
查看>>
浅谈项目需求变更管理
查看>>
经典算法系列一-快速排序
查看>>
设置java web工程中默认访问首页的几种方式
查看>>
ASP.NET MVC 拓展ViewResult实现word文档下载
查看>>
jQuery Mobile笔记
查看>>
8、RDD持久化
查看>>
第二次团队冲刺--2
查看>>
VMware Tools安装
查看>>
Linux上架设boost的安装及配置过程
查看>>
[转载]加密算法库Crypto——nodejs中间件系列
查看>>
zoj 2286 Sum of Divisors
查看>>
OO5~7次作业总结
查看>>
如何判断主机是大端还是小端(字节序)
查看>>
Centos7 日志查看工具
查看>>
使用Xshell密钥认证机制远程登录Linux
查看>>
OpenCV之响应鼠标(三):响应鼠标信息
查看>>
python7 数据类型的相互转化 字符编码
查看>>