官术网_书友最值得收藏!

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.
主站蜘蛛池模板: 汝南县| 南宁市| 德庆县| 运城市| 肇东市| 滁州市| 安化县| 章丘市| 东阿县| 辉县市| 连江县| 颍上县| 南开区| 太白县| 日照市| 惠来县| 六安市| 疏附县| 石首市| 临城县| 岑巩县| 连云港市| 靖远县| 历史| 万安县| 和林格尔县| 四子王旗| 古浪县| 涞源县| 竹山县| 鄂托克旗| 奉新县| 汝城县| 西昌市| 无极县| 木兰县| 都昌县| 清新县| 三亚市| 揭阳市| 弋阳县|