Skip to content

Commit

Permalink
Merge pull request #693 from qinerg/master
Browse files Browse the repository at this point in the history
fix issue #638
  • Loading branch information
wendal committed Jul 23, 2014
2 parents eb4bf3e + 7a24b32 commit 2d293e4
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/org/nutz/lang/Tasks.java
@@ -1,6 +1,7 @@
package org.nutz.lang;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
Expand All @@ -14,14 +15,14 @@

/**
* 定时任务服务的友好封装
*
* @author QinerG(qinerg@gmail.com)
*/
public abstract class Tasks {

private static Log logger = Logs.get();

private static ScheduledThreadPoolExecutor taskScheduler = new ScheduledThreadPoolExecutor(getBestPoolSize());
private static List<Timer> timerList = new ArrayList<Timer>();

/**
* 立即启动,并以固定的频率来运行任务。后续任务的启动时间不受前次任务延时影响。
Expand Down Expand Up @@ -69,8 +70,10 @@ public static void scheduleAtFixedRate(final Runnable task, Date startTime, fina
public void run() {
taskScheduler.scheduleAtFixedRate(task, 0, period, unit);
timer.cancel();
timerList.remove(timer);
}
}, startTime);
timerList.add(timer);
}

/**
Expand Down Expand Up @@ -119,8 +122,10 @@ public static void scheduleWithFixedDelay(final Runnable task, Date startTime, f
public void run() {
taskScheduler.scheduleWithFixedDelay(task, 0, period, unit);
timer.cancel();
timerList.remove(timer);
}
}, startTime);
timerList.add(timer);
}

/**
Expand All @@ -144,8 +149,16 @@ public static ScheduledThreadPoolExecutor getTaskScheduler() {
* <p>系统关闭时可调用此方法终止正在执行的定时任务,一旦关闭后不允许再向线程池中添加任务,否则会报RejectedExecutionException异常</p>
*/
public static void depose() {
int timerNum = timerList.size();
//清除Timer
synchronized (timerList) {
for (Timer t: timerList)
t.cancel();
timerList.clear();
}

List<Runnable> awaitingExecution = taskScheduler.shutdownNow();
logger.infof("Tasks stopping. Tasks awaiting execution: %d", awaitingExecution.size());
logger.infof("Tasks stopping. Tasks awaiting execution: %d", timerNum + awaitingExecution.size());
}

/**
Expand Down

0 comments on commit 2d293e4

Please sign in to comment.