Skip to content

Commit e5ebcfe

Browse files
committed
修复在scheduler的runTask方法内执行scheduler.runTask()方法会导致
在列表循环内添加元素报错的BUG
1 parent 643dc14 commit e5ebcfe

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/main/kotlin/cn/coostack/cooparticlesapi/scheduler/CooScheduler.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package cn.coostack.cooparticlesapi.scheduler
22

3+
import java.util.concurrent.ConcurrentLinkedQueue
34
import kotlin.math.sin
45

56
class CooScheduler {
6-
internal val ticks = HashSet<TickRunnable>()
7+
internal val ticks = ConcurrentLinkedQueue<TickRunnable>()
8+
internal val taskQueue = ConcurrentLinkedQueue<TickRunnable>()
79
internal fun doTick() {
810
val iterator = ticks.iterator()
911
while (iterator.hasNext()) {
@@ -13,6 +15,8 @@ class CooScheduler {
1315
iterator.remove()
1416
}
1517
}
18+
ticks.addAll(taskQueue)
19+
taskQueue.clear()
1620
}
1721

1822
/**
@@ -22,7 +26,7 @@ class CooScheduler {
2226
val tick = TickRunnable(runnable)
2327
tick.singleDelay = delay
2428
tick.loop()
25-
ticks.add(tick)
29+
taskQueue.add(tick)
2630
return tick
2731
}
2832

@@ -32,7 +36,7 @@ class CooScheduler {
3236
fun runTask(delay: Int, runnable: Runnable): TickRunnable {
3337
val tick = TickRunnable(runnable)
3438
tick.singleDelay = delay
35-
ticks.add(tick)
39+
taskQueue.add(tick)
3640
return tick
3741
}
3842

@@ -44,7 +48,7 @@ class CooScheduler {
4448
val tick = TickRunnable(runnable)
4549
tick.maxTick = maxLoopTick
4650
tick.loopTimer()
47-
ticks.add(tick)
51+
taskQueue.add(tick)
4852
return tick
4953
}
5054

@@ -59,7 +63,7 @@ class CooScheduler {
5963
tick.maxTick = maxLoopTick
6064
tick.singleDelay = preDelay
6165
tick.loopTimer()
62-
ticks.add(tick)
66+
taskQueue.add(tick)
6367
return tick
6468
}
6569

0 commit comments

Comments
 (0)