- 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.
推薦閱讀
- 黑客攻防從入門到精通(實戰(zhàn)秘笈版)
- PaaS程序設(shè)計
- MATLAB圖像處理超級學習手冊
- Learning Informatica PowerCenter 10.x(Second Edition)
- Java程序設(shè)計與實踐教程(第2版)
- 老“碼”識途
- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- Scala謎題
- 重學Java設(shè)計模式
- Haxe Game Development Essentials
- 從零開始學Selenium自動化測試:基于Python:視頻教學版
- Tableau Dashboard Cookbook
- 交互設(shè)計師成長手冊:從零開始學交互
- Spring Web Services 2 Cookbook
- C語言王者歸來