Skip to content

Commit e55a45b

Browse files
zhangtaozhangtao
zhangtao
authored and
zhangtao
committed
去除掉重复任务检查
1 parent 34e98c6 commit e55a45b

File tree

1 file changed

+9
-29
lines changed

1 file changed

+9
-29
lines changed

router/src/main/java/com/therouter/TheRouterThreadPool.kt

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ private val MAXIMUM_CORE_POOL_SIZE = CPU_COUNT * 8
2222
private const val MAXIMUM_POOL_SIZE = Int.MAX_VALUE
2323
var KEEP_ALIVE_SECONDS = 30L
2424
var MAX_QUEUE_SIZE = 10
25-
var REPEAT_TASK_COUNT = 10
26-
var REPEAT_TASK_INTERVAL_SECONDS = 2L
2725

2826
private const val THREAD_NAME = "TheRouterLibThread"
2927

@@ -112,18 +110,16 @@ private class BufferExecutor : ExecutorService, Executor {
112110
// 加入一级队列时被记录,任务执行完成时被移除
113111
val flightTaskMap = SparseArray<FlightTaskInfo>()
114112

115-
// 加入二级级队列时被记录,不会被移除
116-
val taskTraceCountMap = HashMap<String, FlightTaskInfo>()
117-
118113
@Synchronized
119114
override fun execute(r: Runnable) {
120-
val trace = if (TheRouter.isDebug) {
121-
getTrace(Thread.currentThread().stackTrace)
122-
} else {
123-
""
124-
}
125-
checkTask(trace)
126-
taskQueue.offer(Task(r, trace) {
115+
taskQueue.offer(Task(
116+
r, if (TheRouter.isDebug) {
117+
checkTask()
118+
getTrace(Thread.currentThread().stackTrace)
119+
} else {
120+
""
121+
}
122+
) {
127123
flightTaskMap.remove(r.hashCode())
128124
scheduleNext()
129125
})
@@ -137,7 +133,7 @@ private class BufferExecutor : ExecutorService, Executor {
137133
* 检查是否有频繁添加任务,或有轮询任务的情况
138134
*/
139135
@Synchronized
140-
private fun checkTask(trace: String) {
136+
private fun checkTask() {
141137
if (TheRouter.isDebug) {
142138
flightTaskMap.forEach { _, v ->
143139
require(
@@ -148,24 +144,8 @@ private class BufferExecutor : ExecutorService, Executor {
148144
"当前任务被创建时间为${v?.createTime}此时时间为${System.currentTimeMillis()}\n${v?.trace}"
149145
)
150146
}
151-
152-
val info = taskTraceCountMap[trace] ?: FlightTaskInfo(trace).also {
153-
it.createTime = Long.MAX_VALUE
154-
it.count = 0
155-
}
156-
info.count++
157-
if (info.createTime - System.currentTimeMillis() < REPEAT_TASK_INTERVAL_SECONDS * 1000L) {
158-
require(
159-
info.count <= REPEAT_TASK_COUNT,
160-
"ThreadPool",
161-
"该任务连续${info.count}次在${REPEAT_TASK_INTERVAL_SECONDS}秒内被添加,请优化逻辑\n${trace}"
162-
)
163-
}
164-
info.createTime = System.currentTimeMillis()
165-
taskTraceCountMap[trace] = info
166147
} else {
167148
flightTaskMap.clear()
168-
taskTraceCountMap.clear()
169149
}
170150
}
171151

0 commit comments

Comments
 (0)