- Learning Concurrency in Kotlin
- Miguel Angel Castiblanco Torres
- 174字
- 2021-08-05 10:46:45
Coroutine dispatcher
In order to decide what thread to start or resume a coroutine on, a coroutine dispatcher is used. All coroutine dispatchers must implement the CoroutineDispatcher interface:
- DefaultDispatcher: Currently it is the same as CommonPool. In the future, this may change.
- CommonPool: Executes and resumes the coroutines in a pool of shared background threads. Its default size makes it optimal for use in CPU-bound operations.
- Unconfined: Starts the coroutine in the current thread (the thread from which the coroutine was invoked), but will resume the coroutine in any thread. No thread policy is used by this dispatcher.
Along with these dispatchers, there are a couple of builders that can be used to define pools or threads as needed:
- newSingleThreadContext() to build a dispatcher with a single thread; the coroutines executed here will be started and resumed in the same thread, always.
- newFixedThreadPoolContext() creates a dispatcher with a pool of threads of the given size. The runtime will decide which thread to use when starting and resuming the coroutines executed in the dispatcher.
推薦閱讀
- Learning Single:page Web Application Development
- 移動UI設計(微課版)
- 大學計算機應用基礎實踐教程
- Python爬蟲開發與項目實戰
- 重學Java設計模式
- Learning Hunk
- Spring Boot Cookbook
- H5頁面設計:Mugeda版(微課版)
- HoloLens與混合現實開發
- Visual Basic語言程序設計基礎(第3版)
- C/C++代碼調試的藝術(第2版)
- Java語言程序設計實用教程(第2版)
- micro:bit軟件指南
- Mastering Machine Learning with scikit-learn
- Mastering PostgreSQL 11(Second Edition)