- 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.
推薦閱讀
- 零起步玩轉掌控板與Mind+
- 編寫高質量代碼:改善C程序代碼的125個建議
- 概率成形編碼調制技術理論及應用
- Raspberry Pi Robotic Blueprints
- Learning JavaScript Data Structures and Algorithms(Second Edition)
- Android Studio Cookbook
- 交互式程序設計(第2版)
- 視窗軟件設計和開發自動化:可視化D++語言
- Learning Unreal Engine Game Development
- Python應用與實戰
- C語言程序設計教程
- 軟件工程實用教程 (第3版)
- 新手學ASP動態網頁開發
- C/C++程序設計教程:面向過程分冊
- C#面向對象程序設計(微課版)