Now I use this: ``` class WorkerPool(size: Int) { ...
# coroutines
e
Now I use this:
Copy code
class WorkerPool(size: Int) {
    private val channel = Channel<suspend () -> Unit>()

    private val actors = (0..size).map { _ ->
        GlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
            channel.consumeEach { it() }
        }
    }

    suspend fun <T> execute(inv: suspend () -> T): Deferred<T> {
        val result = CompletableDeferred<T>()
        val mdc = MDC.getCopyOfContextMap() ?: emptyMap()

        channel.send {
            try {
                val res = withContext(MDCContext(mdc)) {
                    inv()
                }
                result.complete(res)
            } catch (e: Exception) {
                result.cancel(e)
            }
        }

        return result
    }

    fun shutdown() {
        channel.close()

        actors.forEach { it.cancel() }
    }
}