diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/BlockableEventLoop.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/util/BlockableEventLoop.kt index 171c816b..f6182bf4 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/util/BlockableEventLoop.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/util/BlockableEventLoop.kt @@ -170,12 +170,12 @@ open class BlockableEventLoop(name: String) : Thread(name), ScheduledExecutorSer } final override fun execute(command: Runnable) { - if (!isRunning) - throw RejectedExecutionException("EventLoop is shutting down") - if (currentThread() === this) { command.run() } else { + if (!isRunning) + throw RejectedExecutionException("EventLoop is shutting down") + val future = CompletableFuture() val pair = TaskPair(future) { command.run() } @@ -189,9 +189,6 @@ open class BlockableEventLoop(name: String) : Thread(name), ScheduledExecutorSer } final override fun submit(task: Callable): CompletableFuture { - if (!isRunning) - throw RejectedExecutionException("EventLoop is shutting down") - if (currentThread() === this) { try { return CompletableFuture.completedFuture(task.call()) @@ -199,6 +196,9 @@ open class BlockableEventLoop(name: String) : Thread(name), ScheduledExecutorSer return CompletableFuture.failedFuture(err) } } else { + if (!isRunning) + throw RejectedExecutionException("EventLoop is shutting down") + val future = CompletableFuture() val pair = TaskPair(future, task) @@ -228,9 +228,6 @@ open class BlockableEventLoop(name: String) : Thread(name), ScheduledExecutorSer fun isSameThread() = this === currentThread() final override fun submit(task: Runnable): CompletableFuture<*> { - if (!isRunning) - throw RejectedExecutionException("EventLoop is shutting down") - if (currentThread() === this) { try { return CompletableFuture.completedFuture(task.run()) @@ -238,6 +235,9 @@ open class BlockableEventLoop(name: String) : Thread(name), ScheduledExecutorSer return CompletableFuture.failedFuture(err) } } else { + if (!isRunning) + throw RejectedExecutionException("EventLoop is shutting down") + val future = CompletableFuture() val pair = TaskPair(future) { task.run() } @@ -252,9 +252,6 @@ open class BlockableEventLoop(name: String) : Thread(name), ScheduledExecutorSer } final override fun submit(task: Runnable, result: T): CompletableFuture { - if (!isRunning) - throw RejectedExecutionException("EventLoop is shutting down") - if (currentThread() === this) { try { task.run() @@ -263,6 +260,9 @@ open class BlockableEventLoop(name: String) : Thread(name), ScheduledExecutorSer return CompletableFuture.failedFuture(err) } } else { + if (!isRunning) + throw RejectedExecutionException("EventLoop is shutting down") + val future = CompletableFuture() val pair = TaskPair(future) { task.run(); result } @@ -278,32 +278,20 @@ open class BlockableEventLoop(name: String) : Thread(name), ScheduledExecutorSer } final override fun invokeAll(tasks: Collection>): List> { - if (!isRunning) - throw RejectedExecutionException("EventLoop is shutting down") - return tasks.map { submit(it) } } final override fun invokeAll(tasks: Collection>, timeout: Long, unit: TimeUnit): List> { - if (!isRunning) - throw RejectedExecutionException("EventLoop is shutting down") - val futures = tasks.map { submit(it) } CompletableFuture.allOf(*futures.toTypedArray()).get(timeout, unit) return futures } final override fun invokeAny(tasks: Collection>): T { - if (!isRunning) - throw RejectedExecutionException("EventLoop shut down") - return submit(tasks.first()).get() } final override fun invokeAny(tasks: Collection>, timeout: Long, unit: TimeUnit): T { - if (!isRunning) - throw RejectedExecutionException("EventLoop is shutting down") - return submit(tasks.first()).get(timeout, unit) }