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

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.
主站蜘蛛池模板: 南漳县| 南郑县| 南木林县| 老河口市| 永新县| 吉安市| 泽州县| 克什克腾旗| 新晃| 平利县| 武城县| 米脂县| 庆安县| 措美县| 台安县| 辉县市| 西乡县| 中超| 吉首市| 理塘县| 岫岩| 北票市| 湘潭市| 揭西县| 鄯善县| 柳林县| 淮南市| 阳山县| 日照市| 龙门县| 左贡县| 皋兰县| 海南省| 富源县| 新宁县| 滁州市| 米泉市| 镇远县| 德格县| 义乌市| 冀州市|