https://kotlinlang.org logo
s

Sergey Chikin

02/19/2018, 9:20 AM
@Michael Kotlikov what this code does is essentially
async
coroutine builder, in other words in your code instead of calling
asyncResult {lambda}
you can do simply
async {lambda}
and not even build this function. You can specify where the coroutine will be executed by passing
CoroutineContext
to
async
like this:
async(routineContext) { lambda }
, where I have
routineContext
defined as follows:
Copy code
internal val routineContext: CoroutineContext by lazy {
        log.debug("Initializing pool with configuration $configuration")
        when (configuration) {
            is SingleThreadConfiguration -> newSingleThreadContext(configuration.name)
            is FixedThreadPoolConfiguration -> newFixedThreadPoolContext(configuration.threads, configuration.name)
            is CachedThreadPoolConfiguration -> CachedCoroutineDispatcher(configuration.name)
            is ForkJoinPoolConfiguration -> ForkJoinCoroutineDispatcher(configuration.name, configuration.parallelism)
            else -> throw ConfigurationException("Unknown thread pool requested")
        }
    }