|
1 | 1 | package org.javacs.kt.util
|
2 | 2 |
|
3 |
| -import org.javacs.kt.LOG |
4 |
| -import java.time.Duration |
5 |
| -import java.util.function.Supplier |
6 | 3 | import java.util.concurrent.CompletableFuture
|
7 | 4 | import java.util.concurrent.Executors
|
8 | 5 | import java.util.concurrent.TimeUnit
|
| 6 | +import java.util.function.Supplier |
| 7 | +import org.javacs.kt.LOG |
9 | 8 |
|
10 | 9 | private var threadCount = 0
|
11 | 10 |
|
12 | 11 | class AsyncExecutor {
|
13 |
| - private val workerThread = Executors.newSingleThreadExecutor { Thread(it, "async${threadCount++}") } |
| 12 | + private val workerThread = |
| 13 | + Executors.newSingleThreadExecutor { Thread(it, "async${threadCount++}") } |
14 | 14 |
|
15 | 15 | fun execute(task: () -> Unit): CompletableFuture<Void> =
|
16 |
| - CompletableFuture.runAsync(Runnable(task), workerThread) |
| 16 | + CompletableFuture.runAsync(Runnable(task), workerThread) |
17 | 17 |
|
18 |
| - fun <R> compute(task: () -> R): CompletableFuture<R> = |
19 |
| - CompletableFuture.supplyAsync(Supplier(task), workerThread) |
| 18 | + fun <R> compute(task: () -> R): CompletableFuture<R> = |
| 19 | + CompletableFuture.supplyAsync(Supplier(task), workerThread) |
20 | 20 |
|
21 |
| - fun <R> computeOr(defaultValue: R, task: () -> R?) = |
22 |
| - CompletableFuture.supplyAsync(Supplier { |
23 |
| - try { |
24 |
| - task() ?: defaultValue |
25 |
| - } catch (e: Exception) { |
26 |
| - defaultValue |
27 |
| - } |
28 |
| - }, workerThread) |
| 21 | + fun <R> computeOr(defaultValue: R, task: () -> R?) = |
| 22 | + CompletableFuture.supplyAsync( |
| 23 | + Supplier { |
| 24 | + try { |
| 25 | + task() ?: defaultValue |
| 26 | + } catch (e: Exception) { |
| 27 | + defaultValue |
| 28 | + } |
| 29 | + }, |
| 30 | + workerThread |
| 31 | + ) |
29 | 32 |
|
30 |
| - fun shutdown(awaitTermination: Boolean) { |
31 |
| - workerThread.shutdown() |
32 |
| - if (awaitTermination) { |
33 |
| - LOG.info("Awaiting async termination...") |
34 |
| - workerThread.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS) |
35 |
| - } |
36 |
| - } |
| 33 | + fun shutdown(awaitTermination: Boolean) { |
| 34 | + workerThread.shutdown() |
| 35 | + if (awaitTermination) { |
| 36 | + LOG.info("Awaiting async termination...") |
| 37 | + workerThread.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS) |
| 38 | + } |
| 39 | + } |
37 | 40 | }
|
0 commit comments