Remove locking in mailbox executor service

This commit is contained in:
DBotThePony 2024-02-04 20:20:03 +07:00
parent a1f7eaeee8
commit abf91f445e
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 52 additions and 53 deletions

View File

@ -4,7 +4,7 @@ kotlin.code.style=official
specifyKotlinAsDependency=false
projectGroup=ru.dbotthepony.kommons
projectVersion=1.7.7
projectVersion=1.7.8
guavaDepVersion=33.0.0
gsonDepVersion=2.8.9

View File

@ -51,7 +51,6 @@ class MailboxExecutorService(thread: Thread = Thread.currentThread()) : Schedule
private val timers = LinkedList<Timer<*>>()
private val repeatableTimers = LinkedList<RepeatableTimer>()
private val executionLock = ReentrantLock()
@Volatile
private var isShutdown = false
@ -138,16 +137,18 @@ class MailboxExecutorService(thread: Thread = Thread.currentThread()) : Schedule
if (!isTerminated) {
isTerminated = true
executionLock.withLock {
futureQueue.forEach {
it.cancel(false)
}
futureQueue.clear()
timers.clear()
repeatableTimers.clear()
}
return
}
}
executionLock.withLock {
var next = futureQueue.poll()
while (next != null) {
@ -193,7 +194,6 @@ class MailboxExecutorService(thread: Thread = Thread.currentThread()) : Schedule
executed.forEach { repeatableTimers.enqueue(it) }
}
}
}
override fun execute(command: Runnable) {
if (isShutdown) throw RejectedExecutionException("This mailbox is shutting down")
@ -210,13 +210,13 @@ class MailboxExecutorService(thread: Thread = Thread.currentThread()) : Schedule
isShutdown = true
}
override fun shutdownNow(): MutableList<Runnable> {
override fun shutdownNow(): List<Runnable> {
if (isTerminated) return listOf()
isShutdown = true
isTerminated = true
val result = ArrayList<Runnable>()
executionLock.withLock {
futureQueue.forEach {
it.cancel(false)
result.add(it)
@ -229,7 +229,6 @@ class MailboxExecutorService(thread: Thread = Thread.currentThread()) : Schedule
timers.clear()
repeatableTimers.clear()
}
return result
}