Scheduler.java 文件源码

java
阅读 25 收藏 0 点赞 0 评论 0

项目:org.openntf.domino 作者:
/**
 * Schedules a cancelable action to be executed periodically. This default implementation schedules
 * recursively and waits for actions to complete (instead of potentially executing long-running actions
 * concurrently). Each scheduler that can do periodic scheduling in a better way should override this.
 * <p>
 * Note to implementors: non-positive {@code initialTime} and {@code period} should be regarded as
 * undelayed scheduling of the first and any subsequent executions.
 * 
 * @param action
 *            the Action to execute periodically
 * @param initialDelay
 *            time to wait before executing the action for the first time; non-positive values indicate
 *            an undelayed schedule
 * @param period
 *            the time interval to wait each time in between executing the action; non-positive values
 *            indicate no delay between repeated schedules
 * @param unit
 *            the time unit of {@code period}
 * @return a subscription to be able to unsubscribe the action (unschedule it if not executed)
 */
public Subscription schedulePeriodically(final Action0 action, long initialDelay, long period, TimeUnit unit) {
    final long periodInNanos = unit.toNanos(period);
    final long startInNanos = TimeUnit.MILLISECONDS.toNanos(now()) + unit.toNanos(initialDelay);

    final MultipleAssignmentSubscription mas = new MultipleAssignmentSubscription();
    final Action0 recursiveAction = new Action0() {
        long count = 0;
        @Override
        public void call() {
            if (!mas.isUnsubscribed()) {
                action.call();
                long nextTick = startInNanos + (++count * periodInNanos);
                mas.set(schedule(this, nextTick - TimeUnit.MILLISECONDS.toNanos(now()), TimeUnit.NANOSECONDS));
            }
        }
    };
    mas.set(schedule(recursiveAction, initialDelay, unit));
    return mas;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号