- Learning Concurrency in Kotlin
- Miguel Angel Castiblanco Torres
- 192字
- 2021-08-05 10:46:44
Leveraged
Creating and managing threads is one of the difficult parts of writing concurrent code in many languages. As seen before, it's important to know when to create a thread, and almost as important to know how many threads are optimal. It's also important to have threads dedicated to I/O operations, while also having threads to tackle CPU-bound operations. And communicating/syncing threads is a challenge in itself.
Kotlin has high-level functions and primitives that make it easier to implement concurrent code:
- To create a thread it's enough to call newSingleThreadContext(), a function that only takes the name of the thread. Once created, that thread can be used to run as many coroutines as needed.
- Creating a pool of threads is as easy, by calling newFixedThreadPoolContext() with the size and the name of the pool.
- CommonPool is a pool of threads optimal for CPU-bound operations. Its maximum size is the amount of cores in the machine minus one.
- The runtime will take charge of moving a coroutine to a different thread when needed .
- There are many primitives and techniques to communicate and synchronize coroutines, such as channels, mutexes, and thread confinement.
推薦閱讀
- The Complete Rust Programming Reference Guide
- Mastering Visual Studio 2017
- Mastering Entity Framework Core 2.0
- Learning C# by Developing Games with Unity 2020
- Monkey Game Development:Beginner's Guide
- MATLAB應用與實驗教程
- 基于差分進化的優化方法及應用
- 算法訓練營:提高篇(全彩版)
- 用戶體驗增長:數字化·智能化·綠色化
- HTML5+CSS3 Web前端開發技術(第2版)
- HTML5從入門到精通(第4版)
- Spring Boot實戰
- Julia數據科學應用
- Docker:容器與容器云(第2版)
- Learning Unreal Engine Game Development