Java中的定时器

tips:这是一篇系列文章,总目录在这里哟~

一、Timer和TimerTask

Timer是jdk中提供的一个定时器工具,使用的时候会在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次。​
TimerTask是一个实现了Runnable接口的抽象类,代表一个可以被Timer执行的任务。

1. 使用

schedule(TimerTask task, long delay, long period)延迟 delay 执行,并每隔period 执行一次

public static void main(String[] args) {
    for (int i = 0; i < 10; ++i) {
        new Timer("timer - " + i).schedule(new TimerTask() {
            @Override
            public void run() {
                println(Thread.currentThread().getName() + " run ");
            }
        }, 2000, 3000);
    }
}

2. 原理

timer底层是把一个个任务放在一个TaskQueue中,TaskQueue是以平衡二进制堆表示的优先级队列,他是通过nextExecutionTime进行优先级排序的,距离下次执行时间越短优先级越高,通过getMin()获得queue[1]​
。源码如下:

private void sched(TimerTask task, long time, long period) {
    if (time < 0)
        throw new IllegalArgumentException("Illegal execution time.");

    // Constrain value of period sufficiently to prevent numeric
    // overflow while still being effectively infinitely large.
    if (Math.abs(period) > (Long.MAX_VALUE >> 1))
        period >>= 1;

    synchronized(queue) {
        if (!thread.newTasksMayBeScheduled)
            throw new IllegalStateException("Timer already cancelled.");

        synchronized(task.lock) {
            if (task.state != TimerTask.VIRGIN)
                throw new IllegalStateException(
                "Task already scheduled or cancelled");
            task.nextExecutionTime = time;
            task.period = period;
            task.state = TimerTask.SCHEDULED;
        }

        queue.add(task);
        // 如果当前任务处于队列的第一个说明轮到这个任务执行
        if (queue.getMin() == task)
            queue.notify();
    }
}

周期性调度通过什么方式实现的,源码如下:

private void mainLoop() {
  		// 首先一直监听队列中有没有任务
        while (true) {
            try {
                TimerTask task;
                boolean taskFired;
    			// 同步,保证任务执行顺序
                synchronized(queue) {
                    // Wait for queue to become non-empty
                    while (queue.isEmpty() && newTasksMayBeScheduled)
                        queue.wait();
                    if (queue.isEmpty())
                        break; // Queue is empty and will forever remain; die
 
                    // Queue nonempty; look at first evt and do the right thing
                    long currentTime, executionTime;
     				// 获取优先级最高的任务
                    task = queue.getMin();
                    synchronized(task.lock) {
                        if (task.state == TimerTask.CANCELLED) {
                            queue.removeMin();
                            continue; // No action required, poll queue again
                        }
                        currentTime = System.currentTimeMillis();
      					// 获取任务下次执行时间
                        executionTime = task.nextExecutionTime;
                        if (taskFired = (executionTime<=currentTime)) {
       						// 到这里是延迟执行和特定时间点执行已经结束了,状态标记为EXECUTED,周期性执行继续往下走
                            if (task.period == 0) { // Non-repeating, remove
                                queue.removeMin();
                                task.state = TimerTask.EXECUTED;
                            } else { // Repeating task, reschedule
        					// 这里他又重新计算了下下个任务的执行,并且任务还在队列中
                                queue.rescheduleMin(
                                  task.period<0 ? currentTime - task.period
                                                : executionTime + task.period);
                            }
                        }
                    }
     				// 如果任务执行时间大于当前时间说明任务还没点,继续等,否则执行run代码块
                    if (!taskFired) // Task hasn't yet fired; wait
                        queue.wait(executionTime - currentTime);
                }
                if (taskFired) // Task fired; run it, holding no locks
                    task.run();
            } catch(InterruptedException e) {
            }
        }
    }
}

从这段代码可以看出,这里真正驱动Timer运转的是wait()和notify()。一起来看看。

3. 线程协调问题

wait()、notify()是和synchronized配合着用的。synchronized解决了多线程竞争的问题。但是没办法解决多线程协调的问题。所以就有了wait()notify()
以上面的Timer为例,为了确保从queue中添加和读取能够有序进行,sched和mainLoop中其实都有一个针对queue的同步块。这样相当于是获取了queue锁。
在mainLoop中,如果queue为空,则会wait()。这里wait()会先释放queue锁,然后则会让当前线程进入等待状态。如何唤醒呢?答案是在相同的锁对象上调用notify()方法。
注意到在往queue中添加了任务后,线程立刻对queue锁对象调用notify()方法,这个方法会唤醒一个正在queue锁等待的线程(就是在mianLoop中位于queue.wait()的线程),从而使得等待线程从queue.wait()方法返回。
线程返回后,会重新获得queue锁。

4. 小结

我们的目的是实现定时器,通过Timer实现定时器,归根结底是利用了阻塞线程的方案。

二、ScheduledExecutorService

ScheduledExecutorService是juc里面提供的一个类。它的主要作用是定时或者周期性的执行任务。

1. 使用

public interface ScheduledExecutorService extends ExecutorService {
    /**
     * 在指定delay(延时)之后,执行提交Runnable的任务,返回一个ScheduledFuture,
     * 任务执行完成后ScheduledFuture的get()方法返回为null,ScheduledFuture的作用是可以cancel任务
     */
    public ScheduledFuture<?> schedule(Runnable command,
                                       long delay, TimeUnit unit);
    /**
     * 在指定delay(延时)之后,执行提交Callable的任务,返回一个ScheduledFuture
     */
    public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                           long delay, TimeUnit unit);
    /**
     * 提交一个Runnable任务延迟了initialDelay时间后,开始周期性的执行该任务,每period时间执行一次
     * 如果任务异常则退出。如果取消任务或者关闭线程池,任务也会退出。
     * 如果任务执行一次的时间大于周期时间,则任务执行将会延后执行,而不会并发执行
     */
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);
    /**
     * 提交一个Runnable任务延迟了initialDelay时间后,开始周期性的执行该任务,以后
       每两次任务执行的间隔是delay
     * 如果任务异常则退出。如果取消任务或者关闭线程池,任务也会退出。
     */
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);
}

看参数也知道是做什么的了,所以就不写具体的示例了。

2. 原理

ScheduledThreadPoolExecutor实例化的参数:

public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
          new DelayedWorkQueue());
}

其他都很熟悉,只是DelayedWorkQueue有些陌生。看看它在Queue家族里所处的位置吧(没把所有的Queue都列出来)。这里可以看出DelayedWorkQueue也是一种阻塞队列。
在这里插入图片描述

DelayedWorkQueue的不同点在于,任务队列会根据任务延时时间的不同进行排序,延时时间越短地就排在队列的前面,先被获取执行。DelayedWorkQueue使用的是堆排序。排序不是本文研究重点,有兴趣可以看siftUpsiftDown两个方法。

我们回过头来看ScheduledThreadPoolExecutor如何执行周期性任务的。

public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                              long initialDelay,
                                              long period,
                                              TimeUnit unit) {
    if (command == null || unit == null)
        throw new NullPointerException();
    if (period <= 0)
        throw new IllegalArgumentException();
    ScheduledFutureTask<Void> sft =
        new ScheduledFutureTask<Void>(command,
                                      null,
                                      triggerTime(initialDelay, unit),
                                      unit.toNanos(period));
    RunnableScheduledFuture<Void> t = decorateTask(command, sft);
    sft.outerTask = t;
    delayedExecute(t);
    return t;
}

这里又引入了一个新概念ScheduledFutureTask。
在这里插入图片描述

Runnable是用来定义一段执行逻辑的,通常是需要被一个线程执行。Future的概念有点类似一种对异步执行结果的承诺。Delayed是一个混合风格的接口,用来标记实现该接口的对象在延迟一段时间后需要执行某一特定的逻辑。
我们一起看下FutureTask。先看看run方法:

public void run() {
    if (state != NEW ||
        !UNSAFE.compareAndSwapObject(this, runnerOffset,
                                     null, Thread.currentThread()))
        return;
    try {
        Callable<V> c = callable;
        if (c != null && state == NEW) {
            V result;
            boolean ran;
            try {
                result = c.call();
                ran = true;
            } catch (Throwable ex) {
                result = null;
                ran = false;
                setException(ex);
            }
            if (ran)
                set(result);
        }
    } finally {
        // runner must be non-null until state is settled to
        // prevent concurrent calls to run()
        runner = null;
        // state must be re-read after nulling runner to prevent
        // leaked interrupts
        int s = state;
        if (s >= INTERRUPTING)
            handlePossibleCancellationInterrupt(s);
    }
}

上来有个状态检查,尝试进行CAS操作。
重点是执行Callable的call方法,取到结果再调用set方法。

protected void set(V v) {
    if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
        outcome = v;
        UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
        finishCompletion();
    }
}

将结果设置为outcome的值,状态最终转换为NORMAL。
获取结果是通过get获取,如下:

public V get() throws InterruptedException, ExecutionException {
    int s = state;
    if (s <= COMPLETING)
        s = awaitDone(false, 0L);
    return report(s);
}

report里返回的就是上面设置的outcome的值。
重点是状态还没有变成>COMPLETING时,执行一个awaitDone的方法阻塞当前线程。

private int awaitDone(boolean timed, long nanos)
    throws InterruptedException {
    // 没有超时的话,值为0L
    final long deadline = timed ? System.nanoTime() + nanos : 0L;
    WaitNode q = null;
    boolean queued = false;
    for (;;) {
        // 线程被打断,抛异常
        if (Thread.interrupted()) {
            removeWaiter(q);
            throw new InterruptedException();
        }

        int s = state;
        // 状态已完成则直接退出
        if (s > COMPLETING) {
            if (q != null)
                q.thread = null;
            return s;
        }
        // 状态为COMPLETING的,会暗示CPU当前线程愿意让出CPU
        else if (s == COMPLETING) // cannot time out yet
            Thread.yield();
        else if (q == null)
            q = new WaitNode();
        else if (!queued)
            queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
                                                 q.next = waiters, q);
        else if (timed) {
            nanos = deadline - System.nanoTime();
            if (nanos <= 0L) {
                removeWaiter(q);
                return state;
            }
            LockSupport.parkNanos(this, nanos);
        }
        else
            LockSupport.park(this);
    }
}

LockSupport.park()也是一种线程阻塞的方式,如果不设置超时时间,则只能被另一个线程通过unpark唤醒。

在这里插入图片描述

线程相关的内容,我打算专门开个支线聊,这里不展开了。
看回ScheduledThreadPoolExecutor.scheduleAtFixedRate(),delayedExecute里将Task放入了队列里。

private void delayedExecute(RunnableScheduledFuture<?> task) {
    if (isShutdown())
        reject(task);
    else {
        // 添加到队列
        super.getQueue().add(task);
        if (isShutdown() &&
            !canRunInCurrentRunState(task.isPeriodic()) &&
            remove(task))
            task.cancel(false);
        else
            // 创建Worker并开始执行
            ensurePrestart();
    }
}

再看ensurePrestart:

void ensurePrestart() {
    int wc = workerCountOf(ctl.get());
    if (wc < corePoolSize)
        addWorker(null, true);
    else if (wc == 0)
        addWorker(null, false);
}

addWorker的基本流程就是创建新的Worker线程,Worker线程做的事情大致如下:

try {
    Runnable r = timed ?
        workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
    workQueue.take();
    if (r != null)
        return r;
    timedOut = true;
} catch (InterruptedException retry) {
    timedOut = false;
}

3. 总结

整个流程总结如下:

  1. 在ScheduledExecutorService中,维护了一个按执行时间排序的阻塞队列、一个Worker的集合。
  2. ScheduledExecutorService构造时,会初始化队列
  3. ScheduledExecutorService有按固定周期执行任务的方法:scheduleAtFixedRate(Runnable command,long initialDelay, long period, TimeUnit unit),该方法的执行流程如下
    1. 传入的参数会被封装成一个ScheduledFutureTask对象
    2. 将task入队列
    3. 创建Worker线程,并启动
  4. Worker线程会循环从queue中获取任务并执行。因为是阻塞队列,所以如果任务还没到执行时间,Worker会被阻塞。
  5. 任务拿出后会被异步执行,结果通过get方法获取。如果get时还没有执行完成,则会阻塞等待执行。

回到最初的目的上,我们是想要实现定时器的。利用ScheduledExecutorService,Worker只是在执行任务的线程,Task也只是任务本身。他们都没有起到控制定时的作用。真正的定时,是在queue中实现的。

public RunnableScheduledFuture<?> take() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        for (;;) {
            RunnableScheduledFuture<?> first = queue[0];
            if (first == null)
                available.await();
            else {
                long delay = first.getDelay(NANOSECONDS);
                if (delay <= 0)
                    return finishPoll(first);
                first = null; // don't retain ref while waiting
                if (leader != null)
                    available.await();
                else {
                    Thread thisThread = Thread.currentThread();
                    leader = thisThread;
                    try {
                        available.awaitNanos(delay);
                    } finally {
                        if (leader == thisThread)
                            leader = null;
                    }
                }
            }
        }
    } finally {
        if (leader == null && queue[0] != null)
            available.signal();
        lock.unlock();
    }
}

哦吼,原来原理上跟TaskQueue也很像,只不过TaskQueue用的是synchronized机制,而这里采用的是更轻量更灵活的ReentrantLock。available.awaitNanos(delay);直接指定了当前线程需要阻塞delay时长,然后才能被poll出。
继续深入的研究也同样放到下面的深入线程的章节里吧,这里不展开了。同时,java.util.concurrent.DelayQueue也是异曲同工,也就不讲了吧。

参考资料

  1. 使用wait和notify,by 廖雪峰
  2. synchronized 实现原理,by 小米信息部技术团队,张庆波
  3. java定时器之Timer使用与原理分析,by 拉里·佩奇
  4. Java优先级队列DelayedWorkQueue原理分析,by wo883721
  5. 使用Condition,by 廖雪峰
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值