- 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.
推薦閱讀
- JavaScript前端開(kāi)發(fā)模塊化教程
- Learning Cython Programming(Second Edition)
- 圖解Java數(shù)據(jù)結(jié)構(gòu)與算法(微課視頻版)
- Mastering Kotlin
- Raspberry Pi 2 Server Essentials
- Kinect for Windows SDK Programming Guide
- C語(yǔ)言程序設(shè)計(jì)
- iOS編程基礎(chǔ):Swift、Xcode和Cocoa入門(mén)指南
- Learning OpenStack Networking(Neutron)(Second Edition)
- Building Machine Learning Systems with Python(Second Edition)
- Learning JavaScript Data Structures and Algorithms(Second Edition)
- Illustrator CS6設(shè)計(jì)與應(yīng)用任務(wù)教程
- 大數(shù)據(jù)時(shí)代的企業(yè)升級(jí)之道(全3冊(cè))
- Android 游戲開(kāi)發(fā)大全(第二版)
- Mastering PowerCLI