|
| 1 | +package cub.async.answers |
| 2 | + |
| 3 | +import cub.async.tasks.* |
| 4 | +import kotlinx.coroutines.* |
| 5 | +import kotlinx.coroutines.channels.BufferOverflow |
| 6 | +import kotlinx.coroutines.channels.Channel |
| 7 | +import kotlinx.coroutines.flow.MutableSharedFlow |
| 8 | +import kotlinx.coroutines.sync.Mutex |
| 9 | +import kotlinx.coroutines.sync.withLock |
| 10 | +import java.lang.Exception |
| 11 | + |
| 12 | +private suspend fun <T> theRetry(arg: Int, block: suspend (Int, Int) -> T): T { |
| 13 | + repeat(4) { // try 4 times |
| 14 | + try { |
| 15 | + return withTimeout(500) { // with timeout |
| 16 | + block(arg, it) |
| 17 | + } |
| 18 | + } catch (e: TimeoutCancellationException) { /* retry */ } |
| 19 | + } |
| 20 | + return block(arg, 5) // last time just invoke without timeout |
| 21 | +} |
| 22 | + |
| 23 | +@OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class) |
| 24 | +fun List<Int>.coroutineSumThread(): Int { |
| 25 | + val sumContext = newSingleThreadContext("sum") |
| 26 | + var sum = 0 |
| 27 | + runBlocking { |
| 28 | + supervisorScope { |
| 29 | + forEach { i -> |
| 30 | + launch(Dispatchers.Default + CoroutineExceptionHandler { _, e -> }) { |
| 31 | + val res1 = async { theRetry(i, ::f1) } |
| 32 | + val res2 = async { theRetry(i, ::f2) } |
| 33 | + val res = f3(res1.await(), res2.await()) |
| 34 | + withContext(sumContext) { |
| 35 | + sum += res |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + return sum |
| 42 | +} |
| 43 | + |
| 44 | +fun List<Int>.coroutineSumMutex(): Int { |
| 45 | + val mutex = Mutex() |
| 46 | + var sum = 0 |
| 47 | + runBlocking { |
| 48 | + supervisorScope { |
| 49 | + forEach { i -> |
| 50 | + launch(Dispatchers.Default + CoroutineExceptionHandler { _, e -> }) { |
| 51 | + val res1 = async { theRetry(i, ::f1) } |
| 52 | + val res2 = async { theRetry(i, ::f2) } |
| 53 | + val res = f3(res1.await(), res2.await()) |
| 54 | + mutex.withLock { |
| 55 | + sum += res |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + return sum |
| 62 | +} |
| 63 | + |
| 64 | +fun List<Int>.coroutineSumChannel(): Int { |
| 65 | + val channel = Channel<Int>(capacity = 10, onBufferOverflow = BufferOverflow.SUSPEND) |
| 66 | + var sum = 0 |
| 67 | + runBlocking { |
| 68 | + coroutineScope { |
| 69 | + launch { |
| 70 | + for (msg in channel) { |
| 71 | + sum += msg |
| 72 | + } |
| 73 | + } |
| 74 | + supervisorScope { |
| 75 | + forEach { i -> |
| 76 | + launch(Dispatchers.Default + CoroutineExceptionHandler { _, e -> }) { |
| 77 | + val res1 = async { theRetry(i, ::f1) } |
| 78 | + val res2 = async { theRetry(i, ::f2) } |
| 79 | + val res = f3(res1.await(), res2.await()) |
| 80 | + channel.send(res) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + // will be called when supervisorScope is done |
| 85 | + channel.close() |
| 86 | + } |
| 87 | + } |
| 88 | + return sum |
| 89 | +} |
| 90 | + |
| 91 | +fun List<Int>.coroutineSumSharedFlow(): Int { |
| 92 | + val flow = MutableSharedFlow<Int>(onBufferOverflow = BufferOverflow.SUSPEND) |
| 93 | + var sum = 0 |
| 94 | + runBlocking { |
| 95 | + coroutineScope { |
| 96 | + val collectorJob = launch { |
| 97 | + flow.collect { |
| 98 | + sum += it |
| 99 | + } |
| 100 | + } |
| 101 | + supervisorScope { |
| 102 | + forEach { i -> |
| 103 | + launch(Dispatchers.Default + CoroutineExceptionHandler { _, e -> }) { |
| 104 | + val res1 = async { theRetry(i, ::f1) } |
| 105 | + val res2 = async { theRetry(i, ::f2) } |
| 106 | + val res = f3(res1.await(), res2.await()) |
| 107 | + flow.emit(res) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + collectorJob.cancel() |
| 112 | + } |
| 113 | + } |
| 114 | + return sum |
| 115 | +} |
| 116 | + |
| 117 | +fun main() { |
| 118 | + val data = getData() |
| 119 | + val answer = data.map { |
| 120 | + try { |
| 121 | + val v1 = f1(it, 23) |
| 122 | + val v2 = f2(it, 23) |
| 123 | + f3(v1, v2) |
| 124 | + } catch (e: Exception) { |
| 125 | + 0 |
| 126 | + } |
| 127 | + }.sum() |
| 128 | + testSolution(answer, data, List<Int>::coroutineSumThread, "Special thread") |
| 129 | + testSolution(answer, data, List<Int>::coroutineSumMutex, "Mutex") |
| 130 | + testSolution(answer, data, List<Int>::coroutineSumChannel, "Channel") |
| 131 | + testSolution(answer, data, List<Int>::coroutineSumSharedFlow, "Flow") |
| 132 | +} |
0 commit comments