提交 575f19f7 编写于 作者: 如梦技术's avatar 如梦技术 🐛

TimingWheel 代码优化。

上级 85be5259
......@@ -58,6 +58,10 @@ public class SystemTimer implements Timer, Function<TimerTaskEntry, Void> {
private final ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();
public SystemTimer() {
this("SystemTimer");
}
public SystemTimer(String executeName) {
this(1L, 20, executeName);
}
......@@ -72,28 +76,17 @@ public class SystemTimer implements Timer, Function<TimerTaskEntry, Void> {
timingWheel = new TimingWheel(tickMs, wheelSize, startMs, taskCounter, delayQueue);
}
/**
* 添加一个任务,任务被包装为一个TimerTaskEntry
*
* @param timerTask TimerTask
*/
@Override
public void add(TimerTask timerTask) {
readLock.lock();
try {
// 通过任务的延时加上当前时间得到延时的具体时刻,作为定时任务的过期时间
addTimerTaskEntry(new TimerTaskEntry(timerTask, timerTask.getDelayMs() + Timer.getHiresClockMs()));
} finally {
readLock.unlock();
}
}
/**
* Advance the internal clock, executing any tasks whose expiration has been
* reached within the duration of the passed timeout.
*
* @param timeoutMs timeoutMs
* @return whether or not any tasks were executed
*/
@Override
public boolean advanceClock(long timeoutMs) {
TimerTaskList bucket;
......@@ -121,28 +114,20 @@ public class SystemTimer implements Timer, Function<TimerTaskEntry, Void> {
return true;
}
/**
* Get the number of tasks pending execution
*
* @return the number of tasks
*/
@Override
public long size() {
return taskCounter.sum();
}
/**
* Shutdown the timer service, leaving pending tasks unexpected
*/
@Override
public void shutdown() {
taskExecutor.shutdown();
}
private void addTimerTaskEntry(TimerTaskEntry timerTaskEntry) {
// 添加失败任务直接执行
// 尝试将任务加入时间轮
if (!timingWheel.add(timerTaskEntry)) {
// Already expired or cancelled
// 任务过期则执行任务,仅当 任务已经过期 或者 任务主动取消 才会进入此分支
if (!timerTaskEntry.cancelled()) {
taskExecutor.submit(timerTaskEntry.getTimerTask());
}
......
......@@ -6,8 +6,8 @@
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
*
* <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
......@@ -27,31 +27,29 @@ import java.util.concurrent.TimeUnit;
public interface Timer {
/**
* Add a new task to this executor. It will be executed after the task's delay
* (beginning from the time of submission)
* 添加新的任务到当前执行器(线程池),在任务过期后会执行任务。
*
* @param timerTask the task to add
*/
void add(TimerTask timerTask);
/**
* Advance the internal clock, executing any tasks whose expiration has been
* reached within the duration of the passed timeout.
* 推进内部时钟,执行任何在走过的时间间隔内过期的任务
*
* @param timeoutMs timeoutMs
* @return whether or not any tasks were executed
* @return whether any tasks were executed
*/
boolean advanceClock(long timeoutMs);
/**
* Get the number of tasks pending execution
* 取得待执行的任务数量
*
* @return the number of tasks
*/
long size();
/**
* Shutdown the timer service, leaving pending tasks unexecuted
* 关闭定时器服务,待执行的任务将不会被执行
*/
void shutdown();
......
......@@ -19,7 +19,9 @@ package net.dreamlu.iot.mqtt.core.util.timer;
/**
* @author kafka、guest
* TimerTask
*
* @author kafka、guest、L.cm
*/
public abstract class TimerTask implements Runnable {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册