{"id":490021,"date":"2026-08-06T12:17:12","date_gmt":"2026-08-06T12:17:12","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=490021"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=490021","title":{"rendered":"Kotlin: \u043f\u0440\u043e Structured Concurrency, CoroutineContext \u0438 CoroutineScope"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ccd\/1fc\/e78\/ccd1fce78eab6fc599f34adc57389912.jpeg\" width=\"648\" height=\"456\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/ccd\/1fc\/e78\/ccd1fce78eab6fc599f34adc57389912.jpeg 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ccd\/1fc\/e78\/ccd1fce78eab6fc599f34adc57389912.jpeg 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<p>\u0412 <a href=\"https:\/\/habr.com\/ru\/articles\/1053868\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u043b\u0438\u0441\u044c \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u0442\u0438\u043a\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0440\u0435\u0448\u0430\u0435\u0442 Kotlin. \u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043a\u0430\u043a\u0438\u0435 \u0438\u0434\u0435\u0438 \u0437\u0430\u043b\u043e\u0436\u0435\u043d\u044b \u043f\u0440\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 \u0432 Kotlin. <\/p>\n<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/00e\/577\/c2b\/00e577c2b4a845350e667934495a4d81.png\" alt=\"\u041f\u043e\u0447\u0435\u043c\u0443 Kotlin?\" title=\"\u041f\u043e\u0447\u0435\u043c\u0443 Kotlin?\" width=\"1125\" height=\"468\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/00e\/577\/c2b\/00e577c2b4a845350e667934495a4d81.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/00e\/577\/c2b\/00e577c2b4a845350e667934495a4d81.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041f\u043e\u0447\u0435\u043c\u0443 Kotlin?<\/figcaption><\/div>\n<\/figure>\n<p>\u0412\u0441\u043f\u043e\u043c\u043d\u0438\u043c \u0435\u0449\u0451 \u0440\u0430\u0437 \u0447\u0435\u043c \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c (parallel) \u043e\u0442 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0433\u043e(concurrent):<\/p>\n<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/df8\/fd7\/4f0\/df8fd74f010f1b83cbd6b8261197f14c.png\" alt=\"https:\/\/kotlinlang.org\/docs\/coroutines-basics.html\" title=\"https:\/\/kotlinlang.org\/docs\/coroutines-basics.html\" width=\"706\" height=\"444\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/df8\/fd7\/4f0\/df8fd74f010f1b83cbd6b8261197f14c.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/df8\/fd7\/4f0\/df8fd74f010f1b83cbd6b8261197f14c.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption><a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-basics.html\" rel=\"noopener noreferrer nofollow\">https:\/\/kotlinlang.org\/docs\/coroutines-basics.html<\/a><\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u0440\u0438 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439. <\/p>\n<p>\u041f\u0440\u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0435 \u043e\u0431\u0449\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0431\u043b\u043e\u043a\u0438-\u0437\u0430\u0434\u0430\u043d\u0438\u044f, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0431\u0435\u0440\u0435\u0442 \u043d\u0430 \u0441\u0435\u0431\u044f \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0441\u0440\u0435\u0434\u044b \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f (\u043d\u0435 OS!!), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0430\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u0411\u043b\u043e\u043a\u0438-\u0437\u0430\u0434\u0430\u043d\u0438\u044f, \u043e\u0442\u043d\u043e\u0441\u044f\u0449\u0438\u0435\u0441\u044f \u043a \u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f  \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b.<\/p>\n<h2>Structured Concurrency<\/h2>\n<p>\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0432 Kotlin \u043b\u0435\u0436\u0438\u0442 \u043f\u043e\u0434\u0445\u043e\u0434, \u0441 \u043f\u043e\u0434\u0430\u0447\u0438 \u041c\u0430\u0440\u0442\u0438\u043d\u0430 \u0421\u0443\u0441\u0442\u0440\u0438\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u0439 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 &#8212; \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Structured Concurrency <strong>(*)<\/strong>.<\/p>\n<p>\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u0435 Structured Concurrency:<\/p>\n<ul>\n<li>\n<p>\u0440\u0430\u0431\u043e\u0447\u0438\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c &#8212; \u043f\u043e\u0442\u043e\u043c\u043e\u043a (\u0434\u043b\u044f  Kotlin &#8212; \u0440\u0430\u0431\u043e\u0447\u0438\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u044d\u0442\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b)<\/p>\n<\/li>\n<li>\n<p>\u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u0433\u043e \u0441\u0432\u044f\u0437\u0430\u043d \u0441 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e:<\/p>\n<ul>\n<li>\n<p>\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f (\u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0438\u043b\u0438 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439) \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0437\u0430\u0434\u0430\u043d\u0438\u0439<\/p>\n<\/li>\n<li>\n<p>\u043e\u0442\u043c\u0435\u043d\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044e \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0437\u0430\u0434\u0430\u043d\u0438\u0439<\/p>\n<\/li>\n<li>\n<p>\u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u0435<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>* <\/strong>\u0438\u0434\u0435\u044e \u0432\u0438\u0442\u0430\u043b\u0438 \u0432 \u0432\u043e\u0437\u0434\u0443\u0445\u0435 \u0438 \u043a \u043c\u043e\u043c\u0435\u043d\u0442\u0443, \u043a\u043e\u0433\u0434\u0430 \u041c\u0430\u0440\u0442\u0438\u043d \u0421\u0443\u0441\u0442\u0440\u0438\u043a \u0441\u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043b \u0441\u0432\u043e\u0439 \u043f\u0440\u0438\u043d\u0446\u0438\u043f, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 Kotlin \u0443\u0436\u0435 \u043f\u0440\u0438\u0448\u043b\u0438 \u043a \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0438 \u0434\u0430\u0436\u0435 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 \u0432 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445. \u041d\u043e \u0441\u0430\u043c\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 &#171;Structured Concurrency&#187; \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0443\u0434\u0430\u0447\u043d\u044b\u043c \u0438 \u0437\u0430\u043a\u0440\u0435\u043f\u0438\u043b\u043e\u0441\u044c \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/p>\n<h2>CoroutineContext<\/h2>\n<p>\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043b\u0435\u0436\u0438\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 CoroutineContext \u0438 \u043d\u0430\u0431\u043e\u0440 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 (AbstractCoroutineContextElement, EmptyCoroutineContext, CombinedContext). <\/p>\n<p>\u0412\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435 (\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 CoroutineContext \u0438 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438), \u0441 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043a\u043e\u043c\u043f\u043e\u043d\u043e\u0432\u0449\u0438\u043a, \u0430 \u0441 \u0434\u0440\u0443\u0433\u043e\u0439, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c Kotlin, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432 \u043d\u0435\u043c \u043b\u0435\u0436\u0430\u0442 \u0447\u0435\u0440\u0435\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u2018+\u2019, \u2018[ ]\u2018. <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u0448\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 CoroutineContext.Element, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c CoroutineContext \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u043e\u0432\u043a\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u0432\u043e\u0439 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043d\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u044f\u0441\u044c \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 \u043a\u043e\u0440\u0443\u0442\u0438\u043d.<\/p>\n<pre><code class=\"kotlin\">class AnyContextElement(val name: String) : CoroutineContext.Element {    override val key: CoroutineContext.Key&lt;*&gt; get() = Key    companion object Key : CoroutineContext.Key&lt;AnyContextElement&gt;}class OtherContextElement : CoroutineContext.Element {    override val key: CoroutineContext.Key&lt;*&gt; get() = Key    companion object Key : CoroutineContext.Key&lt;OtherContextElement&gt;}\/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c AnyContextElement(\"name1\") var customContext = EmptyCoroutineContext + AnyContextElement(\"name1\")println(customContext[AnyContextElement]?.name) \/\/ \u0412\u044b\u0432\u043e\u0434: name1\/\/ \u0414\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 \u0435\u0449\u0451 \u043e\u0434\u043d\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442customContext += OtherContextElement()\/\/ \u0417\u0430\u043c\u0435\u043d\u0438\u043b\u0438 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 AnyContextElement \u0434\u0440\u0443\u0433\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043ccustomContext +=  AnyContextElement(\"name2\")println(customContext[AnyContextElement]?.name) \/\/ \u0412\u044b\u0432\u043e\u0434: name2<\/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<h3>CoroutineScope<\/h3>\n<p><em>CoroutineScope<\/em> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c, \u0445\u0440\u0430\u043d\u044f\u0449\u0435\u043c \u0432 \u0441\u0432\u043e\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u043a\u043e\u0440\u0443\u0442\u0438\u043d.<\/p>\n<pre><code class=\"kotlin\">public interface CoroutineScope {    public val coroutineContext: CoroutineContext}public fun CoroutineScope(context: CoroutineContext): CoroutineScope =    ContextScope(if (context[Job] != null) context else context + Job())<\/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><em>CoroutineScope<\/em> \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u0434\u0432\u0443\u043c\u044f \u0432\u0435\u0449\u0430\u043c\u0438:<\/p>\n<ul>\n<li>\n<p>\u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 <em>coroutineContext<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d<em> (\u043d\u043e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e)<\/em>, \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438, \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 <em>Job<\/em><\/p>\n<\/li>\n<li>\n<p>\u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 extension-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d &#8212; <em>launch<\/em> \u0438 <em>async<\/em><\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0435\u043a\u0443\u0449\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <em>currentCoroutineContext().<\/em><\/p>\n<p>\u041b\u044e\u0431\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e CoroutineScope.<\/p>\n<p>\u0422\u043e\u0447\u043a\u043e\u0439 \u0432\u0445\u043e\u0434\u0430 \u0432 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f <em>runBlocking. <\/em><\/p>\n<pre><code class=\"kotlin\">fun main() = runBlocking {    \/\/ \u043e\u0431\u044a\u0435\u043a\u0442 this \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 CoroutineScope \u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c     \/\/ extension-\u0444\u0443\u043d\u043a\u0446\u0438\u0438    launch {        ...    }    }<\/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\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 coroutineContext \u043d\u0430 \u0431\u0430\u0437\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e coroutineContext. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 Job, \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a Job \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b. <strong>(**)<\/strong><\/p>\n<pre><code class=\"kotlin\">val scope = CoroutineScope(EmptyCoroutineContext)val job1 = scope.launch { ... }val job2 = scope.launch { ... }val job3 = scope.launch { ... }<\/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<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/62f\/2e5\/8db\/62f2e58dbdc1e12d6ed63a9d61d08bf1.png\" alt=\"\" title=\"\" width=\"610\" height=\"310\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/62f\/2e5\/8db\/62f2e58dbdc1e12d6ed63a9d61d08bf1.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/62f\/2e5\/8db\/62f2e58dbdc1e12d6ed63a9d61d08bf1.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<p><strong>** <\/strong>\u0432 runtime \u043e\u0431\u044a\u0435\u043a\u0442, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439 \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432 coroutineContext[Job],<strong> <\/strong>\u0438 \u0435\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430; \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442\u0441\u044f \u043e\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 AbstractCoroutine, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b Job \u0438 CoroutineScope<\/p>\n<p>\u041e\u0431\u044a\u0435\u043a\u0442 Job \u0432 coroutineContext \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 Structured Concurrency \u0438, \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d.<\/p>\n<pre><code class=\"kotlin\">val scope = CoroutineScope(EmptyCoroutineContext)val job1 = scope.launch { ... }val job2 = scope.launch { ... }val job3 = scope.launch { ... }scope.cancel() \/\/ \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f scope.coroutineContext[Job].cancel()<\/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\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 Job \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d \u043f\u043e\u043a\u0430 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u0441\u044f \u0432\u0441\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b.<\/p>\n<p>\u0412\u0430\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u0447\u0442\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f Structured Concurrency \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c, \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u043c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d (\u043e\u0434\u043d\u043e\u0433\u043e \u0441oroutineContext).<\/p>\n<h2>Structured Concurrency \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445<\/h2>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 1<\/strong><\/p>\n<pre><code class=\"kotlin\">\/\/ runBlocking - \u0442\u043e\u0447\u043a\u0430 \u0432\u0445\u043e\u0434\u0430 \u0432 \u043c\u0438\u0440 \u043a\u043e\u0440\u0443\u0442\u0438\u043d; \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u0432\u043e\u0439 scoperunBlocking {  \/\/ this - \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 scope runBlocking  \/\/ \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u044e\u0442\u0441\u044f \u0432 scope runBlocking  launch { while(true) {  delay(2000)  } } \/\/ this.launch  launch { while(true) {  delay(3000)  } }  launch { while(true) {  delay(3000)  } }  \/\/ \u0412\u0418\u0421\u0418\u041c !!!}<\/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>\u041a\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 <em>launch<\/em> \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c\u0438 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0435\u0439 lambda-\u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0432 <em>runBlocking<\/em>. \u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 2<\/strong><\/p>\n<pre><code class=\"kotlin\">runBlocking {  val scope = CoroutineScope(EmptyCoroutineContext)  scope.launch { while(true) {  delay(2000)  } }  scope.launch { while(true) {  delay(3000)  } }  scope.launch { while(true) {  delay(3000)  } }  \/\/ \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0438\u0437 runBlocking \u043d\u0435 \u0434\u043e\u0436\u0438\u0434\u0430\u044f\u0441\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d}<\/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 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <em>runBlocking<\/em> \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 <em>scope<\/em>. \u041d\u043e\u0432\u044b\u0439 <em>scope<\/em> \u0438 <em>scope<\/em> <em>runBlocking<\/em> \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439. <\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 3<\/strong><\/p>\n<pre><code class=\"kotlin\">runBlocking {  val scope = CoroutineScope(EmptyCoroutineContext) + coroutineContext  scope.launch { while(true) {  delay(2000)  } }  scope.launch { while(true) {  delay(3000)  } }  scope.launch { while(true) {  delay(3000)  } }  \/\/ \u0412\u0418\u0421\u0418\u041c!!!}<\/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 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 3 \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 <em>scope<\/em>, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u0449\u0438\u0439 \u0441 <em>runBlocking<\/em> \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442. \u041a\u043e\u0440\u0443\u0442\u0438\u043d\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u0430\u044f \u0432 <em>runBlocking<\/em> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u043c \u0447\u0435\u0440\u0435\u0437 <em>scope.launch<\/em> \u0438 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 4<\/strong><\/p>\n<pre><code class=\"kotlin\">val scope1 = CoroutineScope(EmptyCoroutineContext)val job = scope1.launch {    val scope2 = CoroutineScope(EmptyCoroutineContext)    val scope2Job = scope2.launch {  ... }    val job1 = launch {  ...  }    val job2 = launch {  ...  }    val job3 = launch {  ...  }}job.cancel() <\/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 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 4 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <em>job.cancel <\/em>\u0431\u0443\u0434\u0443\u0442<em> <\/em>\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b <em>job1<\/em>,<em> job2<\/em>, <em>job3<\/em>; \u041a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 <em>scope2Job <\/em>\u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442 \u0440\u0430\u0431\u043e\u0442\u0443.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 5<\/strong><\/p>\n<pre><code class=\"kotlin\">runBlocking {    doFn() \/\/ \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043d\u0430 scope runBlocking    \/\/ \u0412\u0438\u0441\u0438\u043c!!!}suspend fun doFn() {    val currentJob = currentCoroutineContext()[Job]    (currentJob as? CoroutineScope)?.launch {        while(true) {  delay(2000)  }    }}<\/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\u0438\u043c\u0435\u0440 5 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0438 \u0442\u043e, \u0447\u0442\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 CoroutineScope \u0438 \u0441\u0432\u044f\u0437\u0430\u043d \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c runBlocking.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 6<\/strong><\/p>\n<pre><code class=\"kotlin\">runBlocking {    launch {        doFn()    }    \/\/ \u0412\u0418\u0421\u0418\u041c !!!}suspend fun doFn() = withContext(EmptyCoroutineContext) {  launch {    while(true) {  delay(2000)  }  }}<\/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 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 6 \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0432 <em>withContext<\/em> \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442(EmptyCoroutineContext), \u043d\u043e \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <em>withContext <\/em>\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d <em>runBlocking<\/em>.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 7<\/strong><\/p>\n<pre><code class=\"kotlin\">suspend fun doFn() = withContext(Job()) {    launch {        while(true) {  delay(2000)  }    }}<\/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>\u0418\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u0432\u044b\u0448\u0435\u0438\u0437\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 builder-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0431\u044a\u0435\u043a\u0442 Job \u0438\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0448\u0438\u0439 Job \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0435:<\/p>\n<blockquote>\n<p>Passing \u2018CoroutineContext\u2019 with a \u2018Job\u2019 to \u2018withContext\u2019 builder can lead to structured concurrency violations<\/p>\n<\/blockquote>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 8<\/strong> &#8212; \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d<\/p>\n<pre><code class=\"kotlin\">scope.coroutineContext[Job]?.children?.forEach { job -&gt;   \/\/ launched job   println(job)   \/\/ grandchildren   job.children.forEach {       println(job)   }}<\/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<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043b\u0435\u0436\u0438\u0442 \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u0439 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 Structured Concurrency. <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a \u041aotlin \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<ol>\n<li>\n<p>\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u0443\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c-\u043f\u043e\u0442\u043e\u043c\u043e\u043a<\/p>\n<\/li>\n<li>\n<p>\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u0434\u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d<\/p>\n<\/li>\n<li>\n<p>\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d<\/p>\n<\/li>\n<li>\n<p>\u0437\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u0443 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b &#8212; <em>coroutineContext<\/em><\/p>\n<\/li>\n<li>\n<p>\u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434  <em>currentCoroutineContext()<\/em><\/p>\n<\/li>\n<li>\n<p>\u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 Job, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <em>coroutineContext[Job]<\/em><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u0432 &#8212; \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0445 \u0432 builder-\u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/p>\n<\/li>\n<li>\n<p>\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u0430\u044f \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 Job; \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f Job,  \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a Job \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430<\/p>\n<\/li>\n<\/ol>\n<h2>\u0421\u0441\u044b\u043b\u043a\u0438 \u0438 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u043d\u043e\u0441\u0442\u0438<\/h2>\n<ul>\n<li>\n<p><a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-basics.html#create-your-first-coroutines\" rel=\"noopener noreferrer nofollow\">Coroutine scope and structured concurrency<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/rutube.ru\/video\/81bdc13bd9d2039fc2ef0562bbfd5d55\/?utm_source=embed&amp;utm_medium=referral&amp;utm_campaign=title&amp;utm_content=81bdc13bd9d2039fc2ef0562bbfd5d55&amp;utm_term=yastatic.net&amp;t=1\" rel=\"noopener noreferrer nofollow\">Roman Elizarov \u2014 Structured concurrency<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/733546\/\" rel=\"noopener noreferrer nofollow\">\u0428\u0430\u0431\u043b\u043e\u043d \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: Composite<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/grokipedia.com\/page\/Structured_concurrency\" rel=\"noopener noreferrer nofollow\">Structured concurrency  paradigm<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/883588\/\" rel=\"noopener noreferrer nofollow\">Kotlin Coroutines \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c: CoroutineContext \u0438 CoroutineScope<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/codingwithmohit.com\/coroutines\/custom-coroutine-context-uses-cases\/\" rel=\"noopener noreferrer nofollow\">Custom Kotlin Coroutine Context Uses Cases<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/www.skills.sh\/thebushidocollective\/han\/kotlin-coroutines\" rel=\"noopener noreferrer nofollow\">Kotlin Coroutines<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/codingwithmohit.com\/coroutines\/coroutines-job-structure\/\" rel=\"noopener noreferrer nofollow\">Coroutines Job Structures<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/sustrik.github.io\/250bpm\/blog:71\/\" rel=\"noopener noreferrer nofollow\">S\u00fastrik, Martin: Structured Concurrency<\/a><\/p>\n<\/li>\n<\/ul>\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\/1067510\/\">https:\/\/habr.com\/ru\/articles\/1067510\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u043b\u0438\u0441\u044c \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u0442\u0438\u043a\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0440\u0435\u0448\u0430\u0435\u0442 Kotlin. \u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043a\u0430\u043a\u0438\u0435 \u0438\u0434\u0435\u0438 \u0437\u0430\u043b\u043e\u0436\u0435\u043d\u044b \u043f\u0440\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 \u0432 Kotlin. \u041f\u043e\u0447\u0435\u043c\u0443 Kotlin?\u0412\u0441\u043f\u043e\u043c\u043d\u0438\u043c \u0435\u0449\u0451 \u0440\u0430\u0437 \u0447\u0435\u043c \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c (parallel) \u043e\u0442 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0433\u043e(concurrent):https:\/\/kotlinlang.org\/docs\/coroutines-basics.html\u041f\u0440\u0438 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439. \u041f\u0440\u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0435 \u043e\u0431\u0449\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0431\u043b\u043e\u043a\u0438-\u0437\u0430\u0434\u0430\u043d\u0438\u044f, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0431\u0435\u0440\u0435\u0442 \u043d\u0430 \u0441\u0435\u0431\u044f \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0441\u0440\u0435\u0434\u044b \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f (\u043d\u0435 OS!!), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0430\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u0411\u043b\u043e\u043a\u0438-\u0437\u0430\u0434\u0430\u043d\u0438\u044f, \u043e\u0442\u043d\u043e\u0441\u044f\u0449\u0438\u0435\u0441\u044f \u043a \u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f  \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b.Structured Concurrency\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0432 Kotlin \u043b\u0435\u0436\u0438\u0442 \u043f\u043e\u0434\u0445\u043e\u0434, \u0441 \u043f\u043e\u0434\u0430\u0447\u0438 \u041c\u0430\u0440\u0442\u0438\u043d\u0430 \u0421\u0443\u0441\u0442\u0440\u0438\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u0439 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 &#8212; \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Structured Concurrency (*).\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u0435 Structured Concurrency:\u0440\u0430\u0431\u043e\u0447\u0438\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c &#8212; \u043f\u043e\u0442\u043e\u043c\u043e\u043a (\u0434\u043b\u044f  Kotlin &#8212; \u0440\u0430\u0431\u043e\u0447\u0438\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u044d\u0442\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b)\u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u0433\u043e \u0441\u0432\u044f\u0437\u0430\u043d \u0441 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e:\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f (\u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0438\u043b\u0438 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439) \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0437\u0430\u0434\u0430\u043d\u0438\u0439\u043e\u0442\u043c\u0435\u043d\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044e \u0432\u0441\u0435\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0437\u0430\u0434\u0430\u043d\u0438\u0439\u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u0435* \u0438\u0434\u0435\u044e \u0432\u0438\u0442\u0430\u043b\u0438 \u0432 \u0432\u043e\u0437\u0434\u0443\u0445\u0435 \u0438 \u043a \u043c\u043e\u043c\u0435\u043d\u0442\u0443, \u043a\u043e\u0433\u0434\u0430 \u041c\u0430\u0440\u0442\u0438\u043d \u0421\u0443\u0441\u0442\u0440\u0438\u043a \u0441\u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043b \u0441\u0432\u043e\u0439 \u043f\u0440\u0438\u043d\u0446\u0438\u043f, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 Kotlin \u0443\u0436\u0435 \u043f\u0440\u0438\u0448\u043b\u0438 \u043a \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0438 \u0434\u0430\u0436\u0435 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 \u0432 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445. \u041d\u043e \u0441\u0430\u043c\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 &#171;Structured Concurrency&#187; \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0443\u0434\u0430\u0447\u043d\u044b\u043c \u0438 \u0437\u0430\u043a\u0440\u0435\u043f\u0438\u043b\u043e\u0441\u044c \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.CoroutineContext\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043b\u0435\u0436\u0438\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 CoroutineContext \u0438 \u043d\u0430\u0431\u043e\u0440 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 (AbstractCoroutineContextElement, EmptyCoroutineContext, CombinedContext). \u0412\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435 (\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 CoroutineContext \u0438 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438), \u0441 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043a\u043e\u043c\u043f\u043e\u043d\u043e\u0432\u0449\u0438\u043a, \u0430 \u0441 \u0434\u0440\u0443\u0433\u043e\u0439, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c Kotlin, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432 \u043d\u0435\u043c \u043b\u0435\u0436\u0430\u0442 \u0447\u0435\u0440\u0435\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u2018+\u2019, \u2018[ ]\u2018. \u0415\u0441\u043b\u0438 \u0432\u0430\u0448\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 CoroutineContext.Element, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c CoroutineContext \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u043e\u0432\u043a\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u0432\u043e\u0439 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043d\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u044f\u0441\u044c \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 \u043a\u043e\u0440\u0443\u0442\u0438\u043d.class AnyContextElement(val name: String) : CoroutineContext.Element {    override val key: CoroutineContext.Key&lt;*&gt; get() = Key    companion object Key : CoroutineContext.Key&lt;AnyContextElement&gt;}class OtherContextElement : CoroutineContext.Element {    override val key: CoroutineContext.Key&lt;*&gt; get() = Key    companion object Key : CoroutineContext.Key&lt;OtherContextElement&gt;}\/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c AnyContextElement(&#171;name1&#187;) var customContext = EmptyCoroutineContext + AnyContextElement(&#171;name1&#187;)println(customContext[AnyContextElement]?.name) \/\/ \u0412\u044b\u0432\u043e\u0434: name1\/\/ \u0414\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 \u0435\u0449\u0451 \u043e\u0434\u043d\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442customContext += OtherContextElement()\/\/ \u0417\u0430\u043c\u0435\u043d\u0438\u043b\u0438 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 AnyContextElement \u0434\u0440\u0443\u0433\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043ccustomContext +=  AnyContextElement(&#171;name2&#187;)println(customContext[AnyContextElement]?.name) \/\/ \u0412\u044b\u0432\u043e\u0434: name2CoroutineScopeCoroutineScope \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c, \u0445\u0440\u0430\u043d\u044f\u0449\u0435\u043c \u0432 \u0441\u0432\u043e\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u043a\u043e\u0440\u0443\u0442\u0438\u043d.public interface CoroutineScope {    public val coroutineContext: CoroutineContext}public fun CoroutineScope(context: CoroutineContext): CoroutineScope =    ContextScope(if (context[Job] != null) context else context + Job())CoroutineScope \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u0434\u0432\u0443\u043c\u044f \u0432\u0435\u0449\u0430\u043c\u0438:\u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 coroutineContext, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d (\u043d\u043e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e), \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438, \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 Job\u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 extension-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d &#8212; launch \u0438 async\u0422\u0435\u043a\u0443\u0449\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 currentCoroutineContext().\u041b\u044e\u0431\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e CoroutineScope.\u0422\u043e\u0447\u043a\u043e\u0439 \u0432\u0445\u043e\u0434\u0430 \u0432 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f runBlocking. fun main() = runBlocking {    \/\/ \u043e\u0431\u044a\u0435\u043a\u0442 this \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 CoroutineScope \u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c     \/\/ extension-\u0444\u0443\u043d\u043a\u0446\u0438\u0438    launch {        &#8230;    }    }\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 coroutineContext \u043d\u0430 \u0431\u0430\u0437\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e coroutineContext. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 Job, \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a Job \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b. (**)val scope = CoroutineScope(EmptyCoroutineContext)val job1 = scope.launch { &#8230; }val job2 = scope.launch { &#8230; }val job3 = scope.launch { &#8230; }** \u0432 runtime \u043e\u0431\u044a\u0435\u043a\u0442, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439 \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432 coroutineContext[Job], \u0438 \u0435\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430; \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442\u0441\u044f \u043e\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 AbstractCoroutine, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b Job \u0438 CoroutineScope\u041e\u0431\u044a\u0435\u043a\u0442 Job \u0432 coroutineContext \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 Structured Concurrency \u0438, \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d.val scope = CoroutineScope(EmptyCoroutineContext)val job1 = scope.launch { &#8230; }val job2 = scope.launch { &#8230; }val job3 = scope.launch { &#8230; }scope.cancel() \/\/ \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f scope.coroutineContext[Job].cancel()\u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 Job \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d \u043f\u043e\u043a\u0430 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u0441\u044f \u0432\u0441\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b.\u0412\u0430\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u0447\u0442\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f Structured Concurrency \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c, \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u043c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d (\u043e\u0434\u043d\u043e\u0433\u043e \u0441oroutineContext).Structured Concurrency \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445\u041f\u0440\u0438\u043c\u0435\u0440 1\/\/ runBlocking &#8212; \u0442\u043e\u0447\u043a\u0430 \u0432\u0445\u043e\u0434\u0430 \u0432 \u043c\u0438\u0440 \u043a\u043e\u0440\u0443\u0442\u0438\u043d; \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u0432\u043e\u0439 scoperunBlocking {  \/\/ this &#8212; \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 scope runBlocking  \/\/ \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u044e\u0442\u0441\u044f \u0432 scope runBlocking  launch { while(true) {  delay(2000)  } } \/\/ this.launch  launch { while(true) {  delay(3000)  } }  launch { while(true) {  delay(3000)  } }  \/\/ \u0412\u0418\u0421\u0418\u041c !!!}\u041a\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 launch \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c\u0438 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0435\u0439 lambda-\u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0432 runBlocking. \u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d.\u041f\u0440\u0438\u043c\u0435\u0440 2runBlocking {  val scope = CoroutineScope(EmptyCoroutineContext)  scope.launch { while(true) {  delay(2000)  } }  scope.launch { while(true) {  delay(3000)  } }  scope.launch { while(true) {  delay(3000)  } }  \/\/ \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0438\u0437 runBlocking \u043d\u0435 \u0434\u043e\u0436\u0438\u0434\u0430\u044f\u0441\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d}\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 runBlocking \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 scope. \u041d\u043e\u0432\u044b\u0439 scope \u0438 scope runBlocking \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439. \u041f\u0440\u0438\u043c\u0435\u0440 3runBlocking {  val scope = CoroutineScope(EmptyCoroutineContext) + coroutineContext  scope.launch { while(true) {  delay(2000)  } }  scope.launch { while(true) {  delay(3000)  } }  scope.launch { while(true) {  delay(3000)  } }  \/\/ \u0412\u0418\u0421\u0418\u041c!!!}\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 3 \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 scope, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u0449\u0438\u0439 \u0441 runBlocking \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442. \u041a\u043e\u0440\u0443\u0442\u0438\u043d\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u0430\u044f \u0432 runBlocking \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u043c \u0447\u0435\u0440\u0435\u0437 scope.launch \u0438 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d.\u041f\u0440\u0438\u043c\u0435\u0440 4val scope1 = CoroutineScope(EmptyCoroutineContext)val job = scope1.launch {    val scope2 = CoroutineScope(EmptyCoroutineContext)    val scope2Job = scope2.launch {  &#8230; }    val job1 = launch {  &#8230;  }    val job2 = launch {  &#8230;  }    val job3 = launch {  &#8230;  }}job.cancel() \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 4 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 job.cancel \u0431\u0443\u0434\u0443\u0442 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b job1, job2, job3; \u041a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 scope2Job \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442 \u0440\u0430\u0431\u043e\u0442\u0443.\u041f\u0440\u0438\u043c\u0435\u0440 5runBlocking {    doFn() \/\/ \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043d\u0430 scope runBlocking    \/\/ \u0412\u0438\u0441\u0438\u043c!!!}suspend fun doFn() {    val currentJob = currentCoroutineContext()[Job]    (currentJob as? CoroutineScope)?.launch {        while(true) {  delay(2000)  }    }}\u041f\u0440\u0438\u043c\u0435\u0440 5 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0438 \u0442\u043e, \u0447\u0442\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 CoroutineScope \u0438 \u0441\u0432\u044f\u0437\u0430\u043d \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c runBlocking.\u041f\u0440\u0438\u043c\u0435\u0440 6runBlocking {    launch {        doFn()    }    \/\/ \u0412\u0418\u0421\u0418\u041c !!!}suspend fun doFn() = withContext(EmptyCoroutineContext) {  launch {    while(true) {  delay(2000)  }  }}\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 6 \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0432 withContext \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442(EmptyCoroutineContext), \u043d\u043e \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 withContext \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d runBlocking.\u041f\u0440\u0438\u043c\u0435\u0440 7suspend fun doFn() = withContext(Job()) {    launch {        while(true) {  delay(2000)  }    }}\u0418\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u0432\u044b\u0448\u0435\u0438\u0437\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 builder-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0431\u044a\u0435\u043a\u0442 Job \u0438\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0448\u0438\u0439 Job \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0435:Passing \u2018CoroutineContext\u2019 with a \u2018Job\u2019 to \u2018withContext\u2019 builder can lead to structured concurrency violations\u041f\u0440\u0438\u043c\u0435\u0440 8 &#8212; \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043dscope.coroutineContext[Job]?.children?.forEach { job -&gt;   \/\/ launched job   println(job)   \/\/ grandchildren   job.children.forEach {       println(job)   }}\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043b\u0435\u0436\u0438\u0442 \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u0439 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 Structured Concurrency. \u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a \u041aotlin \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u0443\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c-\u043f\u043e\u0442\u043e\u043c\u043e\u043a\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u0434\u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0437\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u0443 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b &#8212; coroutineContext\u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434  currentCoroutineContext()\u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 Job, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c coroutineContext[Job]\u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u0432 &#8212; \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0445 \u0432 builder-\u0444\u0443\u043d\u043a\u0446\u0438\u044e\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u0430\u044f \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 Job; \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f Job,  \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a Job \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u0421\u0441\u044b\u043b\u043a\u0438 \u0438 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u043d\u043e\u0441\u0442\u0438Coroutine scope and structured concurrencyRoman Elizarov \u2014 Structured concurrency\u0428\u0430\u0431\u043b\u043e\u043d \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: CompositeStructured concurrency  paradigmKotlin Coroutines \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c: CoroutineContext \u0438 CoroutineScopeCustom Kotlin Coroutine Context Uses CasesKotlin CoroutinesCoroutines Job StructuresS\u00fastrik,&#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-490021","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/490021","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=490021"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/490021\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=490021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=490021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=490021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}