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

  • 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.
主站蜘蛛池模板: 三穗县| 和平区| 儋州市| 扶沟县| 乡宁县| 漯河市| 安西县| 翁牛特旗| 镇安县| 绍兴市| 泉州市| 五大连池市| 青铜峡市| 左权县| 滨海县| 安溪县| 南和县| 新乐市| 土默特右旗| 嘉荫县| 屏南县| 定兴县| 美姑县| 赫章县| 常宁市| 调兵山市| 商洛市| 开封市| 沁水县| 贵阳市| 新宾| 谷城县| 和顺县| 桦川县| 平潭县| 巴塘县| 子洲县| 新兴县| 晋中市| 广德县| 句容市|