赞
踩
ScheduledExecutorService是基于线程池的定时任务类,每个调度的任务都会分配到线程池中到一个线程去执行,即任务是并发执行的,互不影响,只有当调度任务来的时候,ScheduledExecutorService才会真正启动一个线程,其余时间ScheduledExecutorService都是处于轮询任务的状态。
public interface ScheduledExecutorService extends ExecutorService { /** * 创建一个到达指定延迟时间执行的任务 */ public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); /** * 创建一个到达指定延迟时间执行的ScheduledFuture */ public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit); /** * 每次执行时间为上一次任务开始起向后推一个时间间隔 */ public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); /** * 每次执行时间为上一次任务结束起向后推一个时间间隔 */ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); }
public class ExecutorServiceDemo { private static ScheduledExecutorService scheduledExecutorServicePool = new ScheduledThreadPoolExecutor(10 , r -> new Thread(r, "executor-service-pool-" + r.hashCode())); /** * 创建一个到达指定延迟时间执行的任务 */ public static void scheduleWithRunnable() { scheduledExecutorServicePool.schedule(() -> System.out.println("scheduleWithRunnable Task") , 5, TimeUnit.SECONDS); scheduledExecutorServicePool.shutdown(); } /** * 创建一个到达指定延迟时间执行的ScheduledFuture */ public static void scheduleWithCallable() { scheduledExecutorServicePool.schedule((Callable) () -> doSomeThing() , 5, TimeUnit.SECONDS); scheduledExecutorServicePool.shutdown(); } private static String doSomeThing() { System.out.println("scheduleWithCallable Task"); return "scheduleWithCallable Task"; } /** * 每次执行时间为上一次任务开始起向后推一个时间间隔 */ public static void scheduleAtFixedRate() { scheduledExecutorServicePool.scheduleAtFixedRate(() -> { System.out.println("scheduleAtFixedRate Task" + System.currentTimeMillis()); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } }, 1, 5, TimeUnit.SECONDS); } /** * 每次执行时间为上一次任务结束起向后推一个时间间隔 */ public static void scheduleWithFixedDelay() { scheduledExecutorServicePool.scheduleWithFixedDelay(() -> { System.out.println("scheduleWithFixedDelay Task" + System.currentTimeMillis()); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } }, 1, 5, TimeUnit.SECONDS); } public static void main(String[] args) { //scheduleWithRunnable(); //scheduleWithCallable(); scheduleAtFixedRate(); //scheduleWithFixedDelay(); } }
@Configuration
public class ScheduleConfig {
@Bean
public ScheduledExecutorService taskScheduler() {
return new ScheduledThreadPoolExecutor(1);
}
}
@Slf4j public class JobUtils { @Resource private static ScheduledExecutorService scheduler; private static final ConcurrentMap<String, ScheduledFuture> timingTaskMap = new ConcurrentHashMap<>(); private static final Map<Long, String> taskHistorical = new HashMap<>(); // private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); /** * 创建一个定时任务 * @param taskName 任务名称 * @param initialDelay 延时开始 * @param period 执行周期 * @param command 任务线程 * @param mandatory (存在)强制创建 */ public static boolean CreateTimingTask(String taskName, long initialDelay, long period, Runnable command, boolean mandatory) { if (FindTimingTask(taskName)) { log.info("定时任务存在"); if (mandatory) { log.info("强制创建"); CancelTimingTask(taskName); ScheduledFuture future = scheduler.scheduleAtFixedRate(command, initialDelay, period, TimeUnit.MINUTES); timingTaskMap.put(taskName, future); return true; } else { return false; } } ScheduledFuture future = scheduler.scheduleAtFixedRate(command, initialDelay, period, TimeUnit.MINUTES); timingTaskMap.put(taskName, future); return true; } /** * 创建一个一次性的延迟任务 * @param initialDelay 延时开始 * @param command 任务线程 */ public static void CreateTask(String name, long initialDelay, Runnable command) { try { scheduler.schedule(command, initialDelay, TimeUnit.MINUTES); taskHistorical.put(System.currentTimeMillis(), name); } catch (Exception e) { log.info("任务执行失败"); } } /** * 根据名称查询定时任务 * @param taskName 任务名称 * @return true任务存在 false任务取消 */ public static boolean FindTimingTask(String taskName) { try { ScheduledFuture scheduledFuture = timingTaskMap.get(taskName); if (scheduledFuture != null) { return true; } } catch (Exception e) { return false; } return false; } /** * 查询定时任务列表 * @return 任务名称集合 */ public static Set<String> FindListTimingTask() { return timingTaskMap.keySet(); } /** * 根据名称取消定时任务 * @param taskName 任务名称 */ public static boolean CancelTimingTask(String taskName) { try { if (FindTimingTask(taskName)) { boolean b = timingTaskMap.get(taskName).cancel(true); timingTaskMap.remove(taskName); return b; } } catch (Exception e) { return false; } return false; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。