Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica-mqtt
提交
575f19f7
mica-mqtt
项目概览
mica
/
mica-mqtt
通知
71
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica-mqtt
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
575f19f7
编写于
5月 14, 2022
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
TimingWheel 代码优化。
上级
85be5259
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
17 addition
and
32 deletion
+17
-32
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/util/timer/SystemTimer.java
...ava/net/dreamlu/iot/mqtt/core/util/timer/SystemTimer.java
+7
-22
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/util/timer/Timer.java
...main/java/net/dreamlu/iot/mqtt/core/util/timer/Timer.java
+7
-9
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/util/timer/TimerTask.java
.../java/net/dreamlu/iot/mqtt/core/util/timer/TimerTask.java
+3
-1
未找到文件。
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/util/timer/SystemTimer.java
浏览文件 @
575f19f7
...
...
@@ -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
());
}
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/util/timer/Timer.java
浏览文件 @
575f19f7
...
...
@@ -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
();
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/util/timer/TimerTask.java
浏览文件 @
575f19f7
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录