CommonPool

First, it's worth mentioning that isDispatchNeeded() is not overridden in CommonPool, so it will always return true. This guarantees that the dispatch() function is always called when resuming a Continuation, and at this point the Runnable block will be executed using the pool. So let's see the implementation of dispatch() in CommonPool:

override fun dispatch(context: CoroutineContext, block: Runnable) =
try {
(
pool ?: getOrCreatePoolSync())
.
execute(timeSource.trackTask(block))
}
catch (e: RejectedExecutionException) {
timeSource.unTrackTask()
DefaultExecutor.execute(block)
}

Notice that in the JVM, pool is an instance of java.util.concurrent.Executor, and it's created by either using java.util.concurrent.ForkJoinPool or by using newFixedThreadPool() from Executor. The highlighted call to execute() is what is actually having the block run in the thread pool.

If there is a SecurityManager or there are errors trying to use ForkJoinPool, newFixedThreadPool() will be used.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.66