{"id":484245,"date":"2026-06-19T09:44:37","date_gmt":"2026-06-19T09:44:37","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=484245"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=484245","title":{"rendered":"\u041e\u0442 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043a \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c: \u043a\u0430\u043a \u0438 \u043f\u043e\u0447\u0435\u043c\u0443 \u0432\u0438\u0434\u043e\u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438\u0441\u044c \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u044f\u0437\u044b\u043a\u0435 Kotlin (\u0427\u0430\u0441\u0442\u044c 2)"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 <a href=\"https:\/\/habr.com\/ru\/companies\/garage8\/articles\/747948\/\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438<\/a>\u00a0\u0431\u044b\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043f\u0440\u0438\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u00a0\u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438: \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0435\u043a\u0446\u0438\u0438, \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0431\u0430\u0440\u044c\u0435\u0440\u043d\u0430\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f.<\/p>\n<p>\u0412\u00a0\u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d\u044b \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u0430\u0436\u043d\u044b\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b:<\/p>\n<ul>\n<li>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430:<\/p>\n<ul>\n<li>\n<p>\u0421\u0435\u043c\u0430\u0444\u043e\u0440\u044b.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0431\u043c\u0435\u043d \u0434\u0430\u043d\u043d\u044b\u043c\u0438:<\/p>\n<ul>\n<li>\n<p>\u041a\u0430\u043d\u0430\u043b\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p>\u0413\u043e\u0440\u044f\u0447\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0438.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Thread confinement:<\/p>\n<ul>\n<li>\n<p>\u041c\u043e\u0434\u0435\u043b\u044c \u0430\u043a\u0442\u043e\u0440\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<\/li>\n<li>\n<p>Dispatcher confinement.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u0414\u043b\u044f\u00a0\u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437\u00a0\u044d\u0442\u0438\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0432\u044b\u0434\u0435\u043b\u0438\u043c \u043f\u043b\u044e\u0441\u044b \u0438 \u043c\u0438\u043d\u0443\u0441\u044b, \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0412\u00a0\u043a\u043e\u043d\u0446\u0435 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u043c \u0432\u0441\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u00a0\u043e\u0431\u0449\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443.<\/p>\n<h2>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430<\/h2>\n<p>\u0412\u00a0\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438 \u0443\u0436\u0435\u00a0\u0431\u044b\u043b\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0435\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e\u0442 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043a\u043e\u0434\u0430 \u0438 \u043d\u0435\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0435\u0433\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443 \u0438\u043b\u0438\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0435. \u0412\u00a0\u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043c\u0435\u043d\u0435\u0435 \u0441\u0442\u0440\u043e\u0433\u0438\u0439 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u0435\u043b\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430\u00a0\u2014 \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u044b.<\/p>\n<h3>\u0421\u0435\u043c\u0430\u0444\u043e\u0440\u044b<\/h3>\n<p>\u0421\u0435\u043c\u0430\u0444\u043e\u0440\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 (\u0438\u043b\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u00a0\u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u043c\u0443 \u0443\u0447\u0430\u0441\u0442\u043a\u0443 \u043a\u043e\u0434\u0430. \u0412\u00a0\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430\u00a0\u2014 kotlinx.coroutines.sync.Semaphore. \u0415\u0441\u043b\u0438 \u043d\u0435\u00a0\u0432\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u00a0\u0434\u0435\u0442\u0430\u043b\u0438, \u0442\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430 \u043d\u0435\u00a0\u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0438\u0430\u043b\u044c\u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u043e\u0442\u00a0java.util.concurrent.Semaphore, \u0437\u0430\u00a0\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0433\u043e\u00a0\u2014 \u0432\u00a0kotlinx.coroutines.sync.Semaphore \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d, \u0430\u00a0\u043d\u0435\u00a0\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0432\u00a0\u0432\u0438\u0434\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0437\u0430\u0434\u0430\u0451\u0442\u0441\u044f \u0432\u00a0\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 (\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 permits).<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u043e\u0432 \u0432\u00a0\u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438:<\/p>\n<pre><code class=\"kotlin\">val systemMemory = AtomicInteger(1_024)fun main() = runBlocking {    val coroutinesCount = 5    val semaphore = Semaphore(coroutinesCount)    Array(1_000) {        async(Dispatchers.IO) {            semaphore.withPermit {                emulateHeavyTask()            }        }    }.forEach { it.await() }    println(\"Success!\")}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u043f\u0430\u043c\u044f\u0442\u044c \u0441\u0442\u0440\u043e\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430\u00a0\u2014 systemMemory \u0440\u0430\u0432\u043d\u0430 1024. \u0415\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0430 emulateHeavyTask, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u043e\u0432\u043d\u043e 200\u00a0\u0435\u0434\u0438\u043d\u0438\u0446 \u043f\u0430\u043c\u044f\u0442\u0438. \u0418 \u0435\u0441\u043b\u0438 \u0435\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u044f\u0442\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d, \u0442\u043e \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 <code>assertTrue(\u00abNot enough memory for task!\u00bb)<\/code>. \u0417\u0434\u0435\u0441\u044c \u0441\u0435\u043c\u0430\u0444\u043e\u0440 \u0434\u0430\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u0433\u0440\u0443\u043f\u043f\u0430\u043c\u0438\u00a0\u2014 \u043d\u0435\u00a0\u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u044f\u0442\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0447\u0442\u043e\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b.<\/p>\n<p>\u0412\u043e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u0434\u043b\u044f\u00a0\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u044b: <\/p>\n<ul>\n<li>\n<p><strong>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a\u00a0\u0441\u0435\u0440\u0432\u0435\u0440\u0443.<\/strong> \u041f\u043e\u043c\u0438\u043c\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u043c\u044b \u043d\u0435\u00a0\u043f\u0440\u0435\u0432\u044b\u0441\u0438\u043c rate limit \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u043d\u0430\u00a0\u0434\u0438\u0441\u043a.<\/strong> \u0414\u0438\u0441\u043a\u0438 \u0438\u043c\u0435\u044e\u0442 \u043a\u043e\u043d\u0435\u0447\u043d\u0443\u044e \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0443\u044e \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c I\/O, \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430 \u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043d\u0430\u00a0\u0434\u0438\u0441\u043a \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0444\u0430\u0439\u043b\u043e\u0432\u00a0\u2014 \u0440\u0430\u0437\u0443\u043c\u043d\u0435\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043f\u0430\u0447\u043a\u0430\u043c\u0438 \u043f\u043e\u00a0\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0444\u0430\u0439\u043b\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p><strong>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/strong> \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0432\u00a0\u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043b\u0443\u0447\u0448\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0435\u00a0\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c OutOfMemory.<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0431\u0449\u0438\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u0432\u043e\u00a0\u0432\u0441\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043e\u0434\u0438\u043d: \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0439 \u0440\u0435\u0441\u0443\u0440\u0441 \u0438\u043c\u0435\u0435\u0442 \u043a\u043e\u043d\u0435\u0447\u043d\u0443\u044e \u0451\u043c\u043a\u043e\u0441\u0442\u044c, \u0430\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0440\u0435\u0441\u0443\u0440\u0441 \u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0442\u044c. \u0415\u0441\u043b\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u043d\u0435\u00a0\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c, \u0442\u043e \u043e\u043d\u0430\u00a0\u043b\u0438\u0431\u043e \u0434\u0435\u0433\u0440\u0430\u0434\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u00a0\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438,\u00a0\u043b\u0438\u0431\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u043e\u0442\u043a\u0430\u0437\u044b,\u00a0\u043b\u0438\u0431\u043e \u0443\u043f\u0430\u0434\u0451\u0442.<\/p>\n<h2>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0431\u043c\u0435\u043d \u0434\u0430\u043d\u043d\u044b\u043c\u0438<\/h2>\n<p>\u0412\u00a0\u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043b\u044f\u00a0\u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0438\u043b\u0438\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438. \u0412\u00a0\u044d\u0442\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u043c\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0445\u043e\u0434\u044f\u0442 \u0432\u00a0\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0438 \u043d\u0435\u00a0\u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<h3>\u041a\u0430\u043d\u0430\u043b\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>\u041a\u0430\u043d\u0430\u043b \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0435\u0448\u0430\u0435\u0442 \u0442\u0438\u043f\u0438\u0447\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 producer\u2011consumer: \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043a\u043b\u0430\u0434\u0451\u0442 \u0438\u0445 \u0432\u00a0\u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0434\u0440\u0443\u0433\u043e\u0439\u00a0\u2014 \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442 \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0430\u043d\u0430\u043b\u0430 \u0434\u043b\u044f\u00a0Threads\u00a0\u2014 \u044d\u0442\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0430\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c BlockingQueue. \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f BlockingQueue:<\/p>\n<pre><code class=\"kotlin\">fun main() {    val queue: BlockingQueue&lt;String&gt; = ArrayBlockingQueue(3)    val producer1 = Thread {        repeat(5) { index -&gt;            Thread.sleep(Random.nextLong(10, 40))            val msg = \"P1 -&gt; $index\"            queue.put(msg)            println(\"put: $msg\")        }    }    val producer2 = Thread {        repeat(5) { index -&gt;            Thread.sleep(Random.nextLong(10, 40))            val msg = \"P2 -&gt; $index\"            queue.put(msg)            println(\"put: $msg\")        }    }    val consumer = Thread {        while (true) {            val msg = queue.take()            if (msg == \"STOP\") break            println(\"take: $msg\")            Thread.sleep(200)        }        println(\"consumer: done\")    }    consumer.start()    producer1.start()    producer2.start()    producer1.join()    producer2.join()    println(\"main: sending STOP\")    queue.put(\"STOP\")    consumer.join()    println(\"main: done\")}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c ArrayBlockingQueue \u0441\u00a0<code>capacity=3<\/code>\u00a0\u2014 \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e\u00a0\u0432\u00a0\u043d\u0435\u0439 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0440\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0430\u00a0\u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0431\u0443\u0434\u0435\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u0434\u043e\u00a0\u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043c\u0435\u0441\u0442\u043e \u043d\u0435\u00a0\u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u0441\u044f.<\/p>\n<ul>\n<li>\n<p>\u0414\u0432\u0430 producer \u043f\u043e\u0442\u043e\u043a\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u043e\u043c 10\u201340\u00a0\u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434, \u0430\u00a0\u0442\u0440\u0435\u0442\u0438\u0439 \u043f\u043e\u0442\u043e\u043a consumer \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043a\u0430\u0436\u0434\u044b\u0435 200\u00a0\u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<\/li>\n<li>\n<p>\u0426\u0438\u043a\u043b \u043f\u043e\u0442\u043e\u043a\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043d\u0435\u00a0\u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f, \u043f\u043e\u043a\u0430 \u043d\u0435\u00a0\u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0441\u0438\u0433\u043d\u0430\u043b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 STOP.<\/p>\n<\/li>\n<li>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0438\u0433\u043d\u0430\u043b \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0442\u00a0producer1\u00a0\u0438 producer2, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043e\u043d\u0438\u00a0\u0431\u044b\u043b\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b \u0440\u0430\u043d\u044c\u0448\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f:<\/p>\n<blockquote>\n<p>put: P2 -&gt; 0<br \/>take: P2 -&gt; 0<br \/>put: P1 -&gt; 0<br \/>put: P1 -&gt; 1<br \/>put: P2 -&gt; 1<br \/>take: P1 -&gt; 0<br \/>put: P2 -&gt; 2<br \/>take: P1 -&gt; 1<br \/>put: P1 -&gt; 2<br \/>take: P2 -&gt; 1<br \/>put: P2 -&gt; 3<br \/>take: P2 -&gt; 2<br \/>put: P1 -&gt; 3<br \/>take: P1 -&gt; 2<br \/>put: P2 -&gt; 4<br \/>take: P2 -&gt; 3<br \/>put: P1 -&gt; 4<br \/>main: sending STOP<br \/>take: P1 -&gt; 3<br \/>take: P2 -&gt; 4<br \/>take: P1 -&gt; 4<br \/>consumer: done<br \/>main: done<\/p>\n<\/blockquote>\n<p>\u041f\u043e\u00a0\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0<code>capacity=3<\/code>\u00a0\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u00a0\u2014 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u00a0\u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043b\u0435\u0436\u0438\u0442 \u043d\u0435\u00a0\u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0440\u0435\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e\u00a0\u043c\u0435\u0436\u0434\u0443 <code>main: sending STOP<\/code> \u0438 <code>consumer: done<\/code> \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u00a0\u043e\u0447\u0435\u0440\u0435\u0434\u0438.<\/p>\n<p>\u0414\u043b\u044f\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043a\u043b\u0430\u0441\u0441 Channel, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430\u00a0\u043a\u043b\u0430\u0441\u0441 ArrayBlockingQueue. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435: \u043a\u043e\u0433\u0434\u0430 \u043a\u0430\u043d\u0430\u043b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432\u00a0\u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043e\u043d \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443 \u0432\u00a0\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 suspend, \u0432\u00a0\u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442\u00a0ArrayBlockingQueue, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u0442\u043e\u043a. \u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u00a0ArrayBlockingQueue, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435, \u043e\u0434\u0438\u043d \u0432\u00a0\u043e\u0434\u0438\u043d \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0438 \u043a\u0430\u043d\u0430\u043b\u044b:<\/p>\n<pre><code class=\"kotlin\">fun main() = runBlocking {    val channel = Channel&lt;String&gt;(capacity = 3)    val producer1 = launch {        repeat(5) { index -&gt;            delay(Random.nextLong(10, 40))            val msg = \"P1 -&gt; $index\"            channel.send(msg)            println(\"send: $msg\")        }    }    val producer2 = launch {        repeat(5) { index -&gt;            delay(Random.nextLong(10, 40))            val msg = \"P2 -&gt; $index\"            channel.send(msg)            println(\"send: $msg\")        }    }    val consumer = launch {        for (msg in channel) {            println(\"recv: $msg\")            delay(200)        }        println(\"consumer: done\")    }    joinAll(producer1, producer2)    println(\"main: closing channel\")    channel.close()    consumer.join()    println(\"main: done\")}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0441\u00a0ArrayBlockingQueue. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0432\u00a0\u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u00a0\u043a\u0430\u043d\u0430\u043b\u0430\u043c\u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0438\u0433\u043d\u0430\u043b STOP (\u0442\u0430\u043a \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0439 Poison pill) \u0443\u0436\u0435 \u043d\u0435\u00a0\u043d\u0443\u0436\u0435\u043d\u00a0\u2014 \u0435\u0441\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 close(), \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043a\u0430\u043d\u0430\u043b. \u0415\u0441\u043b\u0438 \u043a\u0430\u043d\u0430\u043b \u0437\u0430\u043a\u0440\u044b\u0442, \u0442\u043e \u043f\u0440\u0438\u00a0\u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0435 \u043d\u043e\u0432\u044b\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 ClosedSendChannelException. \u041f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c \u0432\u0441\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0434\u043e\u00a0\u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442\u00a0\u0431\u044b\u0442\u044c \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043d\u044b \u0438 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f \u043a\u0430\u043d\u0430\u043b\u0430.<\/p>\n<h3>\u0413\u043e\u0440\u044f\u0447\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0438<\/h3>\n<p>\u0413\u043e\u0440\u044f\u0447\u0438\u0439 \u043f\u043e\u0442\u043e\u043a\u00a0\u2014 \u044d\u0442\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442\u00a0\u043d\u0430\u043b\u0438\u0447\u0438\u044f \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432. \u041d\u043e\u0432\u044b\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u0438 \u0431\u0443\u0434\u0443\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u043e\u00a0\u043d\u0435\u00a0\u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435\u00a0\u0431\u044b\u043b\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b \u0434\u043e\u00a0\u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438.<\/p>\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0433\u043e\u0440\u044f\u0447\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043b\u044f\u00a0Threads \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 <code>java.util.concurrent.Flow.Publisher \/ Subscriber<\/code> \u0438\u0437\u00a0Java Flow API, \u0430\u00a0\u0442\u0430\u043a\u0436\u0435 PublishSubject \u0438\u0437\u00a0\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 rxjava.<\/p>\n<p>\u0412\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043e\u0440\u044f\u0447\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u00a0\u2014 SharedFlow. \u041f\u043e\u043c\u0438\u043c\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d, \u0443\u00a0SharedFlow \u0438\u0437\u00a0\u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u0435\u0441\u0442\u044c \u043d\u0430\u0431\u043e\u0440 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u043f\u0440\u043e\u0449\u0430\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441\u00a0\u0433\u043e\u0440\u044f\u0447\u0438\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438:<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0438\u00a0\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 MutableSharedFlow \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c replay\u2011\u043a\u044d\u0448, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438, \u0430\u00a0\u0442\u0430\u043a\u0436\u0435 extraBufferCapacity\u00a0\u2014 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440 \u0441\u0432\u0435\u0440\u0445 replay. \u041e\u043d \u043d\u0443\u0436\u0435\u043d, \u0447\u0442\u043e\u0431\u044b emit \u043d\u0435\u00a0\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u043b\u0441\u044f \u0441\u0440\u0430\u0437\u0443, \u0435\u0441\u043b\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0438 \u043d\u0435\u00a0\u0443\u0441\u043f\u0435\u0432\u0430\u044e\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u0443\u0444\u0435\u0440\u0430:<\/p>\n<ul>\n<li>\n<p>SUSPEND\u00a0\u2014 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c emitter.<\/p>\n<\/li>\n<li>\n<p>DROP_OLDEST\u00a0\u2014 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0430\u043c\u043e\u0435 \u0441\u0442\u0430\u0440\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>DROP_LATEST\u00a0\u2014 \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u043d\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>SharedFlow \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u0441\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 kotlinx.coroutines.flow: \u0432\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0442\u044c dispatcher, \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445, \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b SharedFlow \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b \u043a\u0430\u043a\u00a0thread\u2011safe \u0438 \u043d\u0435\u00a0\u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p>SharedFlow \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u00a0Kotlin Multiplatform \u0432\u00a0\u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442\u00a0\u0441\u0432\u043e\u0438\u0445 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0445 \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u0432 \u0434\u043b\u044f\u00a0Threads.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b SharedFlow:<\/p>\n<pre><code class=\"kotlin\">fun main() = runBlocking {    val bus = MutableSharedFlow&lt;String&gt;(        replay = 0,        extraBufferCapacity = 3,        onBufferOverflow = BufferOverflow.SUSPEND    )    val consumer = launch {        bus.take(10).collect { message -&gt;            println(\"recv: $message\")            delay(100L)        }        println(\"consumer: done\")    }    suspend fun produce(name: String) {        repeat(5) { index -&gt;            delay(Random.nextLong(10, 30))            val message = \"$name -&gt; $index\"            bus.emit(message)            println(\"emitted: $message\")        }    }    val p1 = launch { produce(\"P1\") }    val p2 = launch { produce(\"P2\") }    joinAll(p1, p2)    consumer.join()}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f MutableSharedFlow \u0441\u00a0\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 <code>extraBufferCapacity=3<\/code> \u0438 <code>onBufferOverflow=SUSPEND<\/code>. \u041a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0434\u0432\u0430\u00a0\u0431\u044b\u0441\u0442\u0440\u044b\u0445 producer \u0438 \u043e\u0434\u0438\u043d \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439 consumer, \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>extraBufferCapacity=3<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 producer\u00a0\u0431\u044b\u0441\u0442\u0440\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0442\u0440\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u00a0SharedFlow \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u00a0\u0447\u0435\u0442\u0432\u0451\u0440\u0442\u043e\u043c \u0443\u0439\u0442\u0438 \u0432\u00a0suspend.<\/p>\n<blockquote>\n<p>emitted: P2 -&gt; 0<br \/>recv: P2 -&gt; 0<br \/>emitted: P1 -&gt; 0<br \/>emitted: P2 -&gt; 1<br \/>emitted: P1 -&gt; 1<br \/>recv: P1 -&gt; 0<br \/>emitted: P1 -&gt; 2<br \/>recv: P2 -&gt; 1<br \/>emitted: P2 -&gt; 2<br \/>recv: P1 -&gt; 1<br \/>emitted: P1 -&gt; 3<br \/>recv: P1 -&gt; 2<br \/>emitted: P2 -&gt; 3<br \/>recv: P2 -&gt; 2<br \/>emitted: P1 -&gt; 4<br \/>recv: P1 -&gt; 3<br \/>emitted: P2 -&gt; 4<br \/>recv: P2 -&gt; 3<br \/>recv: P1 -&gt; 4<br \/>recv: P2 -&gt; 4<br \/>consumer: done<\/p>\n<\/blockquote>\n<p>\u041a\u0430\u043a\u00a0\u0438 \u043e\u0436\u0438\u0434\u0430\u043b\u043e\u0441\u044c, \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u00a0SharedFlow \u043d\u0435\u00a0\u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0440\u0435\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0430\u00a0\u0442\u0430\u043a\u0436\u0435 \u043d\u0438 \u043e\u0434\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u00a0\u043f\u043e\u0442\u0435\u0440\u044f\u043b\u043e\u0441\u044c \u0438\u0437\u2011\u0437\u0430 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0439 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u0443\u0444\u0435\u0440\u0430 SUSPEND. \u041f\u0440\u0438\u00a0\u0432\u044b\u0431\u043e\u0440\u0435 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437\u00a0\u043f\u043e\u043b\u0438\u0442\u0438\u043a DROP_* \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u043e\u00a0\u0431\u044b \u0442\u0435\u0440\u044f\u043b\u0438\u0441\u044c.<\/p>\n<p>\u041d\u0430\u00a0\u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, SharedFlow \u043f\u043e\u0445\u043e\u0436 \u043d\u0430\u00a0Channel, \u043d\u043e\u00a0\u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435:<\/p>\n<ul>\n<li>\n<p>\u041d\u0430\u00a0\u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0432\u00a0Channel \u043e\u0431\u044a\u0435\u043a\u0442 \u043c\u043e\u0436\u0435\u0442\u00a0\u0431\u044b\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0435\u043b\u044c, \u0432\u00a0\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a\u00a0\u043a\u0430\u0436\u0434\u043e\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432\u00a0SharedFlow \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0432\u0441\u0435 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0423\u00a0Channel \u0435\u0441\u0442\u044c \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u043a\u043e\u043d\u0446\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f \u043a\u0430\u043d\u0430\u043b\u0430, \u0432\u00a0\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a\u00a0SharedFlow \u043d\u0435\u043b\u044c\u0437\u044f \u00ab\u0437\u0430\u043a\u0440\u044b\u0442\u044c\u00bb \u0432\u00a0\u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e\u043c \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0438\u00a0\u2014 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043e\u0442\u00a0\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>Channel \u043d\u0435\u00a0\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f Flow, \u0430\u00a0\u0437\u043d\u0430\u0447\u0438\u0442, \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442. \u041d\u043e\u00a0\u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 receiveAsFlow, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 Channel \u0432\u043e\u00a0Flow. \u041e\u00a0\u043d\u044e\u0430\u043d\u0441\u0430\u0445 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines.flow\/receive-as-flow.html\">\u0432\u00a0\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043a\u00a0\u043c\u0435\u0442\u043e\u0434\u0443<\/a>.<\/p>\n<\/li>\n<\/ul>\n<h2>Thread confinement<\/h2>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434 thread confinement \u043d\u0435\u00a0\u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u044f\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u043e\u00a0\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u0438\u0441\u0446\u0438\u043f\u043b\u0438\u043d\u044b \u0434\u043b\u044f\u00a0\u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f. \u0421\u0443\u0442\u044c \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432\u00a0\u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432\u00a0\u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0446\u0438\u0438 \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0432\u0435\u0442\u0432\u044f\u043c\u0438 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430.<\/p>\n<h3>\u041c\u043e\u0434\u0435\u043b\u044c \u0430\u043a\u0442\u043e\u0440\u043e\u0432<\/h3>\n<p>\u041c\u043e\u0434\u0435\u043b\u044c \u0430\u043a\u0442\u043e\u0440\u043e\u0432 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u043a\u0430\u043a\u00a0\u043d\u0430\u0431\u043e\u0440 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 (\u0430\u043a\u0442\u043e\u0440\u043e\u0432), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u00a0\u0434\u0435\u043b\u044f\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043e\u043d\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0440\u0435\u0437 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0438\u0445 \u043f\u043e\u00a0\u043e\u0434\u043d\u043e\u043c\u0443\u00a0\u2014 \u044d\u0442\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0433\u043e\u043d\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438.<\/p>\n<p>\u041d\u0430\u00a0\u0443\u0440\u043e\u0432\u043d\u0435 \u044f\u0437\u044b\u043a\u0430 Kotlin \u043d\u0435\u00a0\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0430\u043a\u0442\u043e\u0440\u043e\u0432 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043e\u0431\u0449\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0435\u00a0\u043c\u043e\u0436\u0435\u0442\u00a0\u0431\u044b\u0442\u044c \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0438\u043a\u0442\u043e \u043d\u0435\u00a0\u043f\u043e\u043c\u0435\u0448\u0430\u0435\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 \u043e\u0431\u043c\u0435\u043d\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u0435\u0436\u0434\u0443 \u0430\u043a\u0442\u043e\u0440\u0430\u043c\u0438 \u0447\u0435\u0440\u0435\u0437 \u043b\u044e\u0431\u0443\u044e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e. \u041f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e \u043c\u043e\u0434\u0435\u043b\u044c \u0430\u043a\u0442\u043e\u0440\u043e\u0432 \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0441\u0435\u0431\u044f \u0432\u00a0\u044f\u0437\u044b\u043a\u0430\u0445 \u0442\u0438\u043f\u0430 Erlang, \u0433\u0434\u0435 \u0435\u0451 \u043d\u0435\u043b\u044c\u0437\u044f \u043e\u0431\u043e\u0439\u0442\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043e\u0431\u0449\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c. \u0422\u0435\u043c \u043d\u0435\u00a0\u043c\u0435\u043d\u0435\u0435 \u043f\u0440\u0438\u00a0\u0443\u0441\u043b\u043e\u0432\u0438\u0438 \u0441\u043e\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f \u0434\u043e\u0433\u043e\u0432\u043e\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c\u0438, \u0442\u0430\u043a\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u043f\u043e\u043b\u043d\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u0430 \u0438 \u043a\u00a0Kotlin + coroutines.<\/p>\n<p>\u0412\u00a0\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 Java \u043d\u0435\u0442 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u043a\u0442\u043e\u0440\u043e\u0432. \u041e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0441\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 <code>Executors.newSingleThreadExecutor() + BlockingQueue<\/code> \u0438\u043b\u0438\u00a0\u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0441\u00a0\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u0430\u043a\u0442\u043e\u0440\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 Akka \u0438\u043b\u0438\u00a0Apache Pekko.<\/p>\n<p><a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines.channels\/actor.html\">\u0414\u043b\u044f\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0430\u043a\u0442\u043e\u0440\u044b\u00a0\u0431\u044b\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b<\/a> (kotlinx.coroutines.channels.actor), \u043d\u043e\u00a0\u043f\u043e\u00a0\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \u043d\u0430\u00a0\u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 2026\u00a0\u0433\u043e\u0434\u0430 \u043e\u043d\u0438 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 <code>@ObsoleteCoroutinesApi<\/code> \u0438 \u0438\u0445 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0438\u043b\u0438\u00a0\u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u043f\u0440\u0435\u0442\u0435\u0440\u043f\u044f\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u00a0\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e\u0442\u0441\u044f \u043a\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e.<\/p>\n<p>\u041f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u0430\u043a\u0442\u043e\u0440 \u0434\u043b\u044f\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043b\u0435\u0433\u043a\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u043d\u0430\u043b\u043e\u0432:<\/p>\n<pre><code class=\"kotlin\">class Actor&lt;S, T&gt;(    scope: CoroutineScope,    capacity: Int = Channel.BUFFERED,    private val state: S,    private val handler: suspend (S, T) -&gt; Unit) {    private val mailbox = Channel&lt;T&gt;(capacity)     private val job = scope.launch {        for (message in mailbox) {            handler(state, message)        }    }     suspend fun send(message: T) {        mailbox.send(message)    }     fun close() {        mailbox.close()    }     suspend fun join() {        job.join()    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0442\u0430\u043a\u0438\u043c \u0430\u043a\u0442\u043e\u0440\u043e\u043c \u0441\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Deferred \u0434\u043b\u044f\u00a0\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u043e\u0442\u0432\u0435\u0442\u0430:<\/p>\n<pre><code class=\"kotlin\">class BalanceState(var balance: Double)sealed class BankMessage {    class Deposit(val amount: Double) : BankMessage()    class Withdraw(val amount: Double, val reply: CompletableDeferred&lt;Boolean&gt;) : BankMessage()    class GetBalance(val reply: CompletableDeferred&lt;Double&gt;) : BankMessage()}class BankManager(private val scope: CoroutineScope) {    private val actor = Actor&lt;BalanceState, BankMessage&gt;(        scope = scope,        state = BalanceState(0.0)    ) { state, message -&gt;         when (message) {            is BankMessage.Deposit -&gt; {                state.balance += message.amount                println(\"Deposited ${message.amount}, balance = ${state.balance}\")            }            is BankMessage.Withdraw -&gt; {                if (state.balance &gt;= message.amount) {                    state.balance -= message.amount                    println(\"Withdrew ${message.amount}, balance = ${state.balance}\")                    message.reply.complete(true)                } else {                    println(\"Insufficient funds for ${message.amount}, balance = ${state.balance}\")                    message.reply.complete(false)                }            }            is BankMessage.GetBalance -&gt; {                message.reply.complete(state.balance)            }        }    }     suspend fun deposit(amount: Double) =        actor.send(BankMessage.Deposit(amount))     suspend fun withdraw(amount: Double): Boolean {        val reply = CompletableDeferred&lt;Boolean&gt;()        actor.send(BankMessage.Withdraw(amount, reply))        return reply.await()    }     suspend fun getBalance(): Double {        val reply = CompletableDeferred&lt;Double&gt;()        actor.send(BankMessage.GetBalance(reply))        return reply.await()    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0430\u043a\u0442\u043e\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f\u00a0\u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0446\u0435\u043b\u0438\u00a0\u2014 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u00a0\u0431\u0430\u043b\u0430\u043d\u0441\u043e\u043c. \u041e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0432\u00a0\u0430\u043a\u0442\u043e\u0440\u00a0\u2014 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0438\u0437\u0432\u043d\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441\u00a0\u0431\u0430\u043b\u0430\u043d\u0441\u043e\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u00a0\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f\u00a0\u0430\u043a\u0442\u043e\u0440\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u043a\u0430\u043a\u00a0\u043f\u043e\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 BankMessage, \u0430\u00a0\u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u043e\u0442\u0432\u0435\u0442\u0430 \u0438\u0437\u00a0\u0430\u043a\u0442\u043e\u0440\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e CompletableDeferred.<\/p>\n<h3>\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0437\u0430\u0434\u0430\u0447<\/h3>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430\u00a0\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438\u043b\u0438\u00a0Dispatcher. \u0412\u00a0\u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u00a0\u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"kotlin\">...scope.launch(dispatcher) {    \/\/ Task1.}scope.launch(dispatcher) {    \/\/ Task2.}...<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u043d\u0435\u00a0\u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u00a0Task2\u00a0\u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 Task1. \u041c\u043e\u0436\u043d\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0447\u0435\u0440\u0435\u0437 withContext \u0438\u043b\u0438\u00a0async\/await \u0432\u043c\u0435\u0441\u0442\u043e launch. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u043b\u044f\u00a0\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043d\u043e\u00a0\u0447\u0442\u043e\u00a0\u0435\u0441\u043b\u0438 \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u043d\u0435\u00a0\u0434\u0432\u0435, \u0430\u00a0\u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435? \u0427\u0442\u043e, \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u043d\u0435\u00a0\u0432\u00a0\u043e\u0434\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435, \u0430\u00a0\u043c\u043e\u0433\u0443\u0442\u00a0\u0431\u044b\u0442\u044c \u0437\u0430\u0434\u0430\u043d\u044b \u0438\u0437\u00a0\u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0441\u0442 \u043a\u043e\u0434\u0430?<\/p>\n<p>\u0412\u00a0Android\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0435\u0441\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0437\u0430\u0434\u0430\u0447\u00a0\u2014 android.os.Handler. \u041c\u0435\u0442\u043e\u0434 post \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u00a0\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0434\u043b\u044f\u00a0\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043d\u0430\u00a0\u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c HandlerThread. \u042d\u0442\u043e \u043d\u0435\u00a0\u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435, \u0447\u0442\u043e\u00a0\u0443\u043c\u0435\u0435\u0442 Handler, \u043d\u043e\u00a0\u043a\u0430\u043a\u00a0\u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0438 \u043d\u0430\u00a0\u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u00a0\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d:<\/p>\n<pre><code class=\"kotlin\">class CoroutineHandler(baseDispatcher: CoroutineDispatcher) {    private val exceptionHandler =        CoroutineExceptionHandler { _, error -&gt; onError(error) }    private val scope = CoroutineScope(        context = baseDispatcher.limitedParallelism(1) + SupervisorJob() + exceptionHandler,    )    private val queue = Channel&lt;suspend () -&gt; Unit&gt;(Channel.UNLIMITED)    init {        scope.launch {            while (currentCoroutineContext().isActive) {                receiveQueueElement()            }        }    }    fun postAction(action: () -&gt; Unit): Boolean =        postSuspendAction { action() }    fun postSuspendAction(action: suspend () -&gt; Unit): Boolean =        queue.trySend(action).isSuccess    private suspend fun receiveQueueElement() {        queue.receiveCatching()            .onSuccess { action -&gt;                runCatching {                    action.invoke()                }.onFailure { throwable -&gt;                    onErrorOrCancellation(throwable)                }            }.onFailure { throwable -&gt;                 onErrorOrCancellation(throwable)            }.onClosed {                throw CancellationException()            }    }    private fun onErrorOrCancellation(throwable: Throwable) {             if (throwable is CancellationException) {                throw throwable            } else {                onError(throwable)            }    }    private fun onError(throwable: Throwable) {        \/\/ Log error    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412\u00a0CoroutineHandler \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e HandlerThread \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>baseDispatcher.limitedParallelism(1)<\/code>, \u0430\u00a0\u0432\u00a0\u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0437\u0430\u0434\u0430\u0447\u00a0\u2014 Channel.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f CoroutineHandler \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u00a0\u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u00a0\u2014 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0438:<\/p>\n<pre><code class=\"kotlin\">class AnalyticsTracker(analytics: AnalyticsManager) {    private val trackHandler: CoroutineHandler =  CoroutineHandler(Dispatchers.IO)    init {        analytics.initAnalytics()        trackHandler.postSuspendAction {            analytics.startSuspend()        }    }    fun trackSingleEvent(eventName: String, eventBody: String) =        trackHandler.postSuspendAction  {            analytics.trackSingleEventSuspend(eventName, eventBody)        }    fun trackMultipleEvents(events: List&lt;Event&gt;) =        trackHandler.postSuspendAction {            analytics.trackMultipleEventsSuspend(events)        }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412\u00a0\u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f initAnalytics. \u0414\u0430\u043b\u0435\u0435 \u043a\u0430\u043a\u00a0\u043f\u0435\u0440\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u00a0\u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f startSuspend. \u0422\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b trackSingleEvent \u0438 trackMultipleEvents. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0442\u043e\u043c\u0443, \u0447\u0442\u043e\u00a0CoroutineHandler \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f.<\/p>\n<h3>Dispatcher confinement<\/h3>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434 thread confinement \u043b\u0435\u0436\u0438\u0442 \u0432\u00a0\u043e\u0441\u043d\u043e\u0432\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 Android \u0438 iOS. \u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u043d\u0430\u044f \u0447\u0435\u0440\u0442\u0430\u00a0\u2014 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 main, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u00a0\u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0434\u043b\u044f\u00a0\u0437\u0430\u0434\u0430\u0447, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u00a0UI (\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u043c\u0435\u0442\u043e\u0434\u044b lifecycle \u0438 \u0434\u0440.). \u041f\u043e\u043f\u044b\u0442\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f\u00a0\u0434\u043e\u043b\u0433\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 (\u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439, \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441\u00a0\u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438\u00a0\u0441\u0435\u0442\u044c\u044e) \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0443\u0442 \u043a\u00a0\u0437\u0430\u0432\u0438\u0441\u0430\u043d\u0438\u044e \u0438\u043b\u0438\u00a0\u0432\u044b\u043b\u0435\u0442\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0430 \u0434\u043e\u0433\u043e\u0432\u043e\u0440: \u0435\u0441\u043b\u0438 \u0441\u0442\u0440\u043e\u0433\u043e \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u0443\u044e \u0438\u0434\u0435\u044e \u0441\u00a0main\u2011\u043f\u043e\u0442\u043e\u043a\u043e\u043c, \u0442\u043e \u043d\u0435\u00a0\u043d\u0443\u0436\u043d\u043e \u0434\u0443\u043c\u0430\u0442\u044c, \u043a\u0443\u0434\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c synchronized \u0438\u043b\u0438\u00a0Mutex\u00a0\u2014 \u0432\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u0435\u0437\u00a0\u043d\u0438\u0445. \u041d\u043e\u00a0\u0435\u0441\u043b\u0438 \u0434\u043e\u0433\u043e\u0432\u043e\u0440 \u043d\u0430\u0440\u0443\u0448\u0438\u0442\u044c, \u0442\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c.<\/p>\n<p>\u0412\u00a0\u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441\u00a0CoroutineHandler \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u00a0\u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u0430\u00a0\u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 CoroutineScope \u0441\u00a0limitedParallelism(1), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e\u00a0\u043b\u044e\u0431\u044b\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0435 \u043d\u0430\u00a0\u0434\u0430\u043d\u043d\u043e\u043c CoroutineScope, \u043d\u0435\u00a0\u0431\u0443\u0434\u0443\u0442 \u043a\u043e\u043d\u043a\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 limitedParallelism(1) \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u0434\u0430\u043f\u0442\u0430\u0446\u0438\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 thread confinement \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c. \u041d\u0430\u0437\u043e\u0432\u0451\u043c \u044d\u0442\u043e\u0442 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 dispatcher confinement.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 dispatcher confinement \u043d\u0430\u00a0\u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441\u043a\u043b\u0430\u0434\u0430 \u0438 \u043e\u0442\u0434\u0435\u043b\u0430 \u043f\u0440\u043e\u0434\u0430\u0436:<\/p>\n<pre><code class=\"kotlin\">fun main() = runBlocking {    val warehouseDispatcher = Dispatchers.Default.limitedParallelism(1)    val cashDispatcher = Dispatchers.Default.limitedParallelism(1)    val warehouse = mutableMapOf(\"apple\" to 10, \"banana\" to 5)    var revenue = 0    suspend fun sell(item: String, quantity: Int, price: Int) {        val forSale = withContext(warehouseDispatcher) {            val remainingInStock = warehouse[item] ?: 0            val forSale = minOf(remainingInStock, quantity)            val left = remainingInStock - forSale            warehouse[item] = left            println(\"Warehouse: forSale=$forSale $item, left=$left\")            forSale        }        if (forSale &gt; 0) {            withContext(cashDispatcher) {                revenue += forSale * price                println(\"cash: +${forSale * price}, revenue=$revenue\")            }        }    }    coroutineScope {        launch { sell(\"apple\", 3, 2) }        launch { sell(\"banana\", 2, 4) }        launch { sell(\"apple\", 8, 2) }    }    println(\"Result: warehouse=$warehouse\")    println(\"Result: revenue=$revenue\")}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u0442\u043e\u0432\u0430\u0440\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437\u00a0\u0434\u0432\u0443\u0445 \u044d\u0442\u0430\u043f\u043e\u0432: \u043d\u0430\u00a0\u043f\u0435\u0440\u0432\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0442\u043e\u0432\u0430\u0440 \u0441\u043e \u0441\u043a\u043b\u0430\u0434\u0430, \u043d\u0430\u00a0\u0432\u0442\u043e\u0440\u043e\u043c\u00a0\u2014 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0432\u044b\u0440\u0443\u0447\u043a\u0443. \u0421\u043a\u043b\u0430\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u00a0warehouseDispatcher, \u0430\u00a0\u043e\u0442\u0434\u0435\u043b \u043f\u0440\u043e\u0434\u0430\u0436\u00a0\u2014 \u043d\u0430\u00a0cashDispatcher (\u043e\u0431\u0430 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b <code>limitedParallelism(1)<\/code>). \u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u043f\u0440\u043e\u0434\u0430\u0436 \u0447\u0435\u0440\u0435\u0437 launch.<\/p>\n<p>\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u043a\u0430\u043a\u00a0\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0435\u043c \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0441\u043a\u043b\u0430\u0434\u0430 \u0438 \u043e\u0442\u0434\u0435\u043b\u0430 \u043f\u0440\u043e\u0434\u0430\u0436, \u043e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u0434\u0440\u0443\u0433 \u043e\u0442\u00a0\u0434\u0440\u0443\u0433\u0430. \u0422\u0430\u043a\u0430\u044f \u0434\u0438\u0441\u0446\u0438\u043f\u043b\u0438\u043d\u0430 \u043f\u0440\u0438\u00a0\u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043a\u043e\u0434\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b\u0430 \u043d\u0430\u043c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0442\u00a0\u043a\u0430\u043a\u043e\u0439\u2011\u043b\u0438\u0431\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 warehouse \u0438 revenue.<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430:<\/p>\n<blockquote>\n<p>Warehouse: forSale=3 apple, left=7<br \/>Warehouse: forSale=2 banana, left=3<br \/>Warehouse: forSale=7 apple, left=0<br \/>cash: +6, revenue=6<br \/>cash: +8, revenue=14<br \/>cash: +14, revenue=28<br \/>Result: warehouse={apple=0, banana=3}<br \/>Result: revenue=28<\/p>\n<\/blockquote>\n<p>\u041f\u043e\u00a0\u0432\u044b\u0432\u043e\u0434\u0443 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0433\u043e\u043d\u043e\u043a \u043f\u043e\u0442\u043e\u043a\u043e\u0432\/\u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043f\u0440\u0438\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u043a\u043b\u0430\u0434\u0430 \u0438 \u043e\u0442\u0434\u0435\u043b\u0430 \u043f\u0440\u043e\u0434\u0430\u0436 \u043d\u0435\u0442\u00a0\u2014 \u0432\u0441\u0435 \u043e\u0442\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u0438 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e.<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412\u044b\u0432\u043e\u0434 \u0432\u0440\u044f\u0434\u00a0\u043b\u0438 \u0443\u0434\u0438\u0432\u0438\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043f\u043b\u043e\u0442\u043d\u0443\u044e \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0441\u00a0\u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c\u00a0\u2014 The best synchronization is no synchronization. \u0415\u0441\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0439 \u043a\u043e\u0434 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u00a0\u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0430\u0441\u044c \u0432\u043e\u0432\u0441\u0435, \u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u0442\u0430\u043a \u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043f\u043e\u0434\u0445\u043e\u0434 thread confinement (\u0438\u043b\u0438, \u043a\u0430\u043a\u00a0\u043c\u044b \u0435\u0433\u043e \u043d\u0430\u0437\u0432\u0430\u043b\u0438 \u0434\u043b\u044f\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d, dispatcher confinement) \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0438\u0433\u0440\u044b\u0448\u043d\u044b\u043c.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u0441\u0451\u00a0\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430, \u0442\u043e kotlinx.coroutines \u0438\u0437\u00a0\u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u0430\u043a\u00a0\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 (Semaphore, Mutex), \u0442\u0430\u043a \u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u0434\u043b\u044f\u00a0\u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 (Channel, StateFlow, SharedFlow\u2026).<\/p>\n<p>\u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0432\u0430\u043c \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f\u00a0\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, CountDownLatch, CyclicBarrier, Actor\u2026) \u043d\u0435\u00a0\u0431\u044b\u043b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c, \u0442\u043e \u043d\u0430\u00a0\u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0430\u043d\u0430\u043b\u043e\u0433 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0438\u0442\u043e\u0433\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0430\u043c\u0438:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\"><strong>\u041f\u043e\u0434\u0445\u043e\u0434<\/strong><\/p>\n<\/td>\n<td data-colwidth=\"306\" width=\"306\">\n<p align=\"left\"><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f\u00a0\u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/strong><\/p>\n<\/td>\n<td>\n<p align=\"left\"><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\">\u041a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0435\u043a\u0446\u0438\u0438<\/p>\n<\/td>\n<td data-colwidth=\"306\" width=\"306\">\n<p align=\"left\"><code>synchronized \/ @Synchronized<\/code><\/p>\n<p align=\"left\"><code>ReentrantLock<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>kotlinx.coroutines.sync.Mutex<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\">\u041a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0435\u043a\u0446\u0438\u0438 \u0441\u00a0\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u0447\u0442\u0435\u043d\u0438\u044f\/\u0437\u0430\u043f\u0438\u0441\u0438<\/p>\n<\/td>\n<td data-colwidth=\"306\" width=\"306\">\n<p align=\"left\"><code>ReentrantReadWriteLock<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u043d\u0435\u0442, \u0435\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e <a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/pull\/2045\">Pull Request \u043a\u00a0GitHub Kotlin\/kotlinx.coroutines<\/a>.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\">\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430<\/p>\n<\/td>\n<td data-colwidth=\"306\" width=\"306\">\n<p align=\"left\"><code>java.util.concurrent.Semaphore<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>kotlinx.coroutines.sync.Semaphore<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\">\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435<\/p>\n<\/td>\n<td colspan=\"2\" data-colwidth=\"306,0\" width=\"306\">\n<p align=\"left\"><code>volatile \/ @Volatile<\/code> (\u043d\u0435\u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0434\u043b\u044f\u00a0\u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0441\u0442\u0438) <\/p>\n<p align=\"left\"><code>java.util.concurrent.atomic.*<\/code><\/p>\n<p align=\"left\"><code>kotlinx.atomicfu.*<\/code> (\u0431\u043e\u043b\u0435\u0435 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u00a0\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 Kotlin Multiplatform)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\">\u0411\u0430\u0440\u044c\u0435\u0440\u043d\u0430\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f<\/p>\n<\/td>\n<td data-colwidth=\"306\" width=\"306\">\n<p align=\"left\"><code>CyclicBarrier<\/code><\/p>\n<p align=\"left\"><code>CountDownLatch<\/code><\/p>\n<p align=\"left\"><code>Phaser<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u043d\u0435\u0442, <a href=\"https:\/\/habr.com\/ru\/companies\/garage8\/articles\/747948\/\">\u0435\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u00a0\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438<\/a><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\">\u0420\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435<\/p>\n<\/td>\n<td data-colwidth=\"306\" width=\"306\">\n<p align=\"left\"><code>LiveData (Android)<\/code><\/p>\n<p align=\"left\"><code>BehaviorSubject (RxJava)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>StateFlow \/ MutableStateFlow<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\">\u0413\u043e\u0440\u044f\u0447\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0438<\/p>\n<\/td>\n<td data-colwidth=\"306\" width=\"306\">\n<p align=\"left\"><code>SubmissionPublisher<\/code><\/p>\n<p align=\"left\"><code>PublishSubject (RxJava)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>SharedFlow \/ MutableSharedFlow<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\">\u041a\u0430\u043d\u0430\u043b\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<\/td>\n<td data-colwidth=\"306\" width=\"306\">\n<p align=\"left\"><code>ArrayBlockingQueue<\/code><\/p>\n<p align=\"left\"><code>LinkedBlockingQueue<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>Channel<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\">\u041c\u043e\u0434\u0435\u043b\u044c \u0430\u043a\u0442\u043e\u0440\u043e\u0432<\/p>\n<\/td>\n<td data-colwidth=\"306\" width=\"306\">\n<p align=\"left\"><code>Akka \/ Apache Pekko<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u0430 Obsolete \u0438 \u043d\u0435\u00a0\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u043d\u0430 \u043a\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e, \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u00a0\u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"141\" width=\"141\">\n<p align=\"left\">\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0437\u0430\u0434\u0430\u0447<\/p>\n<\/td>\n<td data-colwidth=\"306\" width=\"306\">\n<p align=\"left\"><code>newSingleThreadExecutor()<\/code><\/p>\n<p align=\"left\"><code>Handler (Android)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u043d\u0435\u0442, \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u00a0\u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441\u044b, \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u00a0\u0442\u0435\u043c\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442\u0441\u044f \u0432\u00a0\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1049508\/\">https:\/\/habr.com\/ru\/articles\/1049508\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438\u00a0\u0431\u044b\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043f\u0440\u0438\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u00a0\u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438: \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0435\u043a\u0446\u0438\u0438, \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0431\u0430\u0440\u044c\u0435\u0440\u043d\u0430\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f.\u0412\u00a0\u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d\u044b \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u0430\u0436\u043d\u044b\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b:\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430:\u0421\u0435\u043c\u0430\u0444\u043e\u0440\u044b.\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0431\u043c\u0435\u043d \u0434\u0430\u043d\u043d\u044b\u043c\u0438:\u041a\u0430\u043d\u0430\u043b\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.\u0413\u043e\u0440\u044f\u0447\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0438.Thread confinement:\u041c\u043e\u0434\u0435\u043b\u044c \u0430\u043a\u0442\u043e\u0440\u043e\u0432.\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0437\u0430\u0434\u0430\u0447.Dispatcher confinement.\u0414\u043b\u044f\u00a0\u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437\u00a0\u044d\u0442\u0438\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0432\u044b\u0434\u0435\u043b\u0438\u043c \u043f\u043b\u044e\u0441\u044b \u0438 \u043c\u0438\u043d\u0443\u0441\u044b, \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0412\u00a0\u043a\u043e\u043d\u0446\u0435 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u043c \u0432\u0441\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u00a0\u043e\u0431\u0449\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443.\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430\u0412\u00a0\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438 \u0443\u0436\u0435\u00a0\u0431\u044b\u043b\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0435\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e\u0442 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043a\u043e\u0434\u0430 \u0438 \u043d\u0435\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0435\u0433\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443 \u0438\u043b\u0438\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0435. \u0412\u00a0\u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043c\u0435\u043d\u0435\u0435 \u0441\u0442\u0440\u043e\u0433\u0438\u0439 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u0435\u043b\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430\u00a0\u2014 \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u044b.\u0421\u0435\u043c\u0430\u0444\u043e\u0440\u044b\u0421\u0435\u043c\u0430\u0444\u043e\u0440\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 (\u0438\u043b\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u00a0\u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u043c\u0443 \u0443\u0447\u0430\u0441\u0442\u043a\u0443 \u043a\u043e\u0434\u0430. \u0412\u00a0\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430\u00a0\u2014 kotlinx.coroutines.sync.Semaphore. \u0415\u0441\u043b\u0438 \u043d\u0435\u00a0\u0432\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u00a0\u0434\u0435\u0442\u0430\u043b\u0438, \u0442\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430 \u043d\u0435\u00a0\u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0438\u0430\u043b\u044c\u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u043e\u0442\u00a0java.util.concurrent.Semaphore, \u0437\u0430\u00a0\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0433\u043e\u00a0\u2014 \u0432\u00a0kotlinx.coroutines.sync.Semaphore \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d, \u0430\u00a0\u043d\u0435\u00a0\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0432\u00a0\u0432\u0438\u0434\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0437\u0430\u0434\u0430\u0451\u0442\u0441\u044f \u0432\u00a0\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 (\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 permits).\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u043e\u0432 \u0432\u00a0\u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438:val systemMemory = AtomicInteger(1_024)fun main() = runBlocking {    val coroutinesCount = 5    val semaphore = Semaphore(coroutinesCount)    Array(1_000) {        async(Dispatchers.IO) {            semaphore.withPermit {                emulateHeavyTask()            }        }    }.forEach { it.await() }    println(&#171;Success!&#187;)}\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u043f\u0430\u043c\u044f\u0442\u044c \u0441\u0442\u0440\u043e\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430\u00a0\u2014 systemMemory \u0440\u0430\u0432\u043d\u0430 1024. \u0415\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0430 emulateHeavyTask, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u043e\u0432\u043d\u043e 200\u00a0\u0435\u0434\u0438\u043d\u0438\u0446 \u043f\u0430\u043c\u044f\u0442\u0438. \u0418 \u0435\u0441\u043b\u0438 \u0435\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u044f\u0442\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d, \u0442\u043e \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 assertTrue(\u00abNot enough memory for task!\u00bb). \u0417\u0434\u0435\u0441\u044c \u0441\u0435\u043c\u0430\u0444\u043e\u0440 \u0434\u0430\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u0433\u0440\u0443\u043f\u043f\u0430\u043c\u0438\u00a0\u2014 \u043d\u0435\u00a0\u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u044f\u0442\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0447\u0442\u043e\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b.\u0412\u043e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u0434\u043b\u044f\u00a0\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u044b: \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a\u00a0\u0441\u0435\u0440\u0432\u0435\u0440\u0443. \u041f\u043e\u043c\u0438\u043c\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u043c\u044b \u043d\u0435\u00a0\u043f\u0440\u0435\u0432\u044b\u0441\u0438\u043c rate limit \u0441\u0435\u0440\u0432\u0435\u0440\u0430.\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u043d\u0430\u00a0\u0434\u0438\u0441\u043a. \u0414\u0438\u0441\u043a\u0438 \u0438\u043c\u0435\u044e\u0442 \u043a\u043e\u043d\u0435\u0447\u043d\u0443\u044e \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0443\u044e \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c I\/O, \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430 \u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043d\u0430\u00a0\u0434\u0438\u0441\u043a \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0444\u0430\u0439\u043b\u043e\u0432\u00a0\u2014 \u0440\u0430\u0437\u0443\u043c\u043d\u0435\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043f\u0430\u0447\u043a\u0430\u043c\u0438 \u043f\u043e\u00a0\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0444\u0430\u0439\u043b\u043e\u0432.\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0432\u00a0\u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043b\u0443\u0447\u0448\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0435\u00a0\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c OutOfMemory.\u041e\u0431\u0449\u0438\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u0432\u043e\u00a0\u0432\u0441\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043e\u0434\u0438\u043d: \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0439 \u0440\u0435\u0441\u0443\u0440\u0441 \u0438\u043c\u0435\u0435\u0442 \u043a\u043e\u043d\u0435\u0447\u043d\u0443\u044e \u0451\u043c\u043a\u043e\u0441\u0442\u044c, \u0430\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0440\u0435\u0441\u0443\u0440\u0441 \u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0442\u044c. \u0415\u0441\u043b\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u043d\u0435\u00a0\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c, \u0442\u043e \u043e\u043d\u0430\u00a0\u043b\u0438\u0431\u043e \u0434\u0435\u0433\u0440\u0430\u0434\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u00a0\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438,\u00a0\u043b\u0438\u0431\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u043e\u0442\u043a\u0430\u0437\u044b,\u00a0\u043b\u0438\u0431\u043e \u0443\u043f\u0430\u0434\u0451\u0442.\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0431\u043c\u0435\u043d \u0434\u0430\u043d\u043d\u044b\u043c\u0438\u0412\u00a0\u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043b\u044f\u00a0\u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0438\u043b\u0438\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438. \u0412\u00a0\u044d\u0442\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u043c\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0445\u043e\u0434\u044f\u0442 \u0432\u00a0\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0438 \u043d\u0435\u00a0\u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438.\u041a\u0430\u043d\u0430\u043b\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445\u041a\u0430\u043d\u0430\u043b \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0435\u0448\u0430\u0435\u0442 \u0442\u0438\u043f\u0438\u0447\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 producer\u2011consumer: \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043a\u043b\u0430\u0434\u0451\u0442 \u0438\u0445 \u0432\u00a0\u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0434\u0440\u0443\u0433\u043e\u0439\u00a0\u2014 \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442 \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0430\u043d\u0430\u043b\u0430 \u0434\u043b\u044f\u00a0Threads\u00a0\u2014 \u044d\u0442\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0430\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c BlockingQueue. \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f BlockingQueue:fun main() {    val queue: BlockingQueue&lt;String&gt; = ArrayBlockingQueue(3)    val producer1 = Thread {        repeat(5) { index -&gt;            Thread.sleep(Random.nextLong(10, 40))            val msg = &#171;P1 -&gt; $index&#187;            queue.put(msg)            println(&#171;put: $msg&#187;)        }    }    val producer2 = Thread {        repeat(5) { index -&gt;            Thread.sleep(Random.nextLong(10, 40))            val msg = &#171;P2 -&gt; $index&#187;            queue.put(msg)            println(&#171;put: $msg&#187;)        }    }    val consumer = Thread {        while (true) {            val msg = queue.take()            if (msg == &#171;STOP&#187;) break            println(&#171;take: $msg&#187;)            Thread.sleep(200)        }        println(&#171;consumer: done&#187;)    }    consumer.start()    producer1.start()    producer2.start()    producer1.join()    producer2.join()    println(&#171;main: sending STOP&#187;)    queue.put(&#171;STOP&#187;)    consumer.join()    println(&#171;main: done&#187;)}\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c ArrayBlockingQueue \u0441\u00a0capacity=3\u00a0\u2014 \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e\u00a0\u0432\u00a0\u043d\u0435\u0439 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0440\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0430\u00a0\u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0431\u0443\u0434\u0435\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u0434\u043e\u00a0\u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043c\u0435\u0441\u0442\u043e \u043d\u0435\u00a0\u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u0441\u044f.\u0414\u0432\u0430 producer \u043f\u043e\u0442\u043e\u043a\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u043e\u043c 10\u201340\u00a0\u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434, \u0430\u00a0\u0442\u0440\u0435\u0442\u0438\u0439 \u043f\u043e\u0442\u043e\u043a consumer \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043a\u0430\u0436\u0434\u044b\u0435 200\u00a0\u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434.\u0426\u0438\u043a\u043b \u043f\u043e\u0442\u043e\u043a\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043d\u0435\u00a0\u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f, \u043f\u043e\u043a\u0430 \u043d\u0435\u00a0\u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0441\u0438\u0433\u043d\u0430\u043b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 STOP.\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0438\u0433\u043d\u0430\u043b \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0442\u00a0producer1\u00a0\u0438 producer2, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043e\u043d\u0438\u00a0\u0431\u044b\u043b\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b \u0440\u0430\u043d\u044c\u0448\u0435.\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f:put: P2 -&gt; 0take: P2 -&gt; 0put: P1 -&gt; 0put: P1 -&gt; 1put: P2 -&gt; 1take: P1 -&gt; 0put: P2 -&gt; 2take: P1 -&gt; 1put: P1 -&gt; 2take: P2 -&gt; 1put: P2 -&gt; 3take: P2 -&gt; 2put: P1 -&gt; 3take: P1 -&gt; 2put: P2 -&gt; 4take: P2 -&gt; 3put: P1 -&gt; 4main: sending STOPtake: P1 -&gt; 3take: P2 -&gt; 4take: P1 -&gt; 4consumer: donemain: done\u041f\u043e\u00a0\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0capacity=3\u00a0\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u00a0\u2014 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u00a0\u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043b\u0435\u0436\u0438\u0442 \u043d\u0435\u00a0\u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0440\u0435\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e\u00a0\u043c\u0435\u0436\u0434\u0443 main: sending STOP \u0438 consumer: done \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u00a0\u043e\u0447\u0435\u0440\u0435\u0434\u0438.\u0414\u043b\u044f\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043a\u043b\u0430\u0441\u0441 Channel, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430\u00a0\u043a\u043b\u0430\u0441\u0441 ArrayBlockingQueue. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435: \u043a\u043e\u0433\u0434\u0430 \u043a\u0430\u043d\u0430\u043b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432\u00a0\u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043e\u043d \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443 \u0432\u00a0\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 suspend, \u0432\u00a0\u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442\u00a0ArrayBlockingQueue, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u0442\u043e\u043a. \u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u00a0ArrayBlockingQueue, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435, \u043e\u0434\u0438\u043d \u0432\u00a0\u043e\u0434\u0438\u043d \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0438 \u043a\u0430\u043d\u0430\u043b\u044b:fun main() = runBlocking {    val channel = Channel&lt;String&gt;(capacity = 3)    val producer1 = launch {        repeat(5) { index -&gt;            delay(Random.nextLong(10, 40))            val msg = &#171;P1 -&gt; $index&#187;            channel.send(msg)            println(&#171;send: $msg&#187;)        }    }    val producer2 = launch {        repeat(5) { index -&gt;            delay(Random.nextLong(10, 40))            val msg = &#171;P2 -&gt; $index&#187;            channel.send(msg)            println(&#171;send: $msg&#187;)        }    }    val consumer = launch {        for (msg in channel) {            println(&#171;recv: $msg&#187;)            delay(200)        }        println(&#171;consumer: done&#187;)    }    joinAll(producer1, producer2)    println(&#171;main: closing channel&#187;)    channel.close()    consumer.join()    println(&#171;main: done&#187;)}\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0441\u00a0ArrayBlockingQueue. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0432\u00a0\u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u00a0\u043a\u0430\u043d\u0430\u043b\u0430\u043c\u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0438\u0433\u043d\u0430\u043b STOP (\u0442\u0430\u043a \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0439 Poison pill) \u0443\u0436\u0435 \u043d\u0435\u00a0\u043d\u0443\u0436\u0435\u043d\u00a0\u2014 \u0435\u0441\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 close(), \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043a\u0430\u043d\u0430\u043b. \u0415\u0441\u043b\u0438 \u043a\u0430\u043d\u0430\u043b \u0437\u0430\u043a\u0440\u044b\u0442, \u0442\u043e \u043f\u0440\u0438\u00a0\u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0435 \u043d\u043e\u0432\u044b\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 ClosedSendChannelException. \u041f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c \u0432\u0441\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0434\u043e\u00a0\u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442\u00a0\u0431\u044b\u0442\u044c \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043d\u044b \u0438 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f \u043a\u0430\u043d\u0430\u043b\u0430.\u0413\u043e\u0440\u044f\u0447\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0438\u0413\u043e\u0440\u044f\u0447\u0438\u0439 \u043f\u043e\u0442\u043e\u043a\u00a0\u2014 \u044d\u0442\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442\u00a0\u043d\u0430\u043b\u0438\u0447\u0438\u044f \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432. \u041d\u043e\u0432\u044b\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u0438 \u0431\u0443\u0434\u0443\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u043e\u00a0\u043d\u0435\u00a0\u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435\u00a0\u0431\u044b\u043b\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b \u0434\u043e\u00a0\u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438.\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0433\u043e\u0440\u044f\u0447\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043b\u044f\u00a0Threads \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 java.util.concurrent.Flow.Publisher \/ Subscriber \u0438\u0437\u00a0Java Flow API, \u0430\u00a0\u0442\u0430\u043a\u0436\u0435 PublishSubject \u0438\u0437\u00a0\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 rxjava.\u0412\u00a0\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043e\u0440\u044f\u0447\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u00a0\u2014 SharedFlow. \u041f\u043e\u043c\u0438\u043c\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d, \u0443\u00a0SharedFlow \u0438\u0437\u00a0\u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u0435\u0441\u0442\u044c \u043d\u0430\u0431\u043e\u0440 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u043f\u0440\u043e\u0449\u0430\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441\u00a0\u0433\u043e\u0440\u044f\u0447\u0438\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438:\u041f\u0440\u0438\u00a0\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 MutableSharedFlow \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c replay\u2011\u043a\u044d\u0448, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438, \u0430\u00a0\u0442\u0430\u043a\u0436\u0435 extraBufferCapacity\u00a0\u2014 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440 \u0441\u0432\u0435\u0440\u0445 replay. \u041e\u043d \u043d\u0443\u0436\u0435\u043d, \u0447\u0442\u043e\u0431\u044b emit \u043d\u0435\u00a0\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u043b\u0441\u044f \u0441\u0440\u0430\u0437\u0443, \u0435\u0441\u043b\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0438 \u043d\u0435\u00a0\u0443\u0441\u043f\u0435\u0432\u0430\u044e\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.\u0415\u0441\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u0443\u0444\u0435\u0440\u0430:SUSPEND\u00a0\u2014 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c emitter.DROP_OLDEST\u00a0\u2014 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0430\u043c\u043e\u0435 \u0441\u0442\u0430\u0440\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.DROP_LATEST\u00a0\u2014 \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u043d\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.SharedFlow \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u0441\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 kotlinx.coroutines.flow: \u0432\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0442\u044c dispatcher, \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445, \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435.\u0412\u0441\u0435 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b SharedFlow \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b \u043a\u0430\u043a\u00a0thread\u2011safe \u0438 \u043d\u0435\u00a0\u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438.SharedFlow \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u00a0Kotlin Multiplatform \u0432\u00a0\u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442\u00a0\u0441\u0432\u043e\u0438\u0445 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0445 \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u0432 \u0434\u043b\u044f\u00a0Threads.\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b SharedFlow:fun main() = runBlocking {    val bus = MutableSharedFlow&lt;String&gt;(        replay = 0,        extraBufferCapacity = 3,        onBufferOverflow = BufferOverflow.SUSPEND    )    val consumer = launch {        bus.take(10).collect { message -&gt;            println(&#171;recv: $message&#187;)            delay(100L)        }        println(&#171;consumer: done&#187;)    }    suspend fun produce(name: String) {        repeat(5) { index -&gt;            delay(Random.nextLong(10, 30))            val message = &#171;$name -&gt; $index&#187;            bus.emit(message)            println(&#171;emitted: $message&#187;)        }    }    val p1 = launch { produce(&#171;P1&#187;) }    val p2 = launch { produce(&#171;P2&#187;) }    joinAll(p1, p2)    consumer.join()}\u0412\u00a0\u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f MutableSharedFlow \u0441\u00a0\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 extraBufferCapacity=3 \u0438 onBufferOverflow=SUSPEND. \u041a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0434\u0432\u0430\u00a0\u0431\u044b\u0441\u0442\u0440\u044b\u0445 producer \u0438 \u043e\u0434\u0438\u043d \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439 consumer, \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 extraBufferCapacity=3 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 producer\u00a0\u0431\u044b\u0441\u0442\u0440\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0442\u0440\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u00a0SharedFlow \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u00a0\u0447\u0435\u0442\u0432\u0451\u0440\u0442\u043e\u043c \u0443\u0439\u0442\u0438 \u0432\u00a0suspend.emitted: P2 -&gt; 0recv: P2 -&gt; 0emitted: P1 -&gt; 0emitted: P2 -&gt; 1emitted: P1 -&gt; 1recv: &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-484245","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/484245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=484245"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/484245\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=484245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=484245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=484245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}