File tree Expand file tree Collapse file tree 1 file changed +10
-14
lines changed
src/main/kotlin/cub/async/answers Expand file tree Collapse file tree 1 file changed +10
-14
lines changed Original file line number Diff line number Diff line change @@ -2,10 +2,8 @@ package cub.async.answers
22
33import cub.async.tasks.Server
44import cub.async.tasks.posts
5- import kotlinx.coroutines.CoroutineScope
6- import kotlinx.coroutines.Dispatchers
7- import kotlinx.coroutines.launch
8- import kotlinx.coroutines.runBlocking
5+ import kotlinx.coroutines.*
6+ import java.lang.Runnable
97import java.util.concurrent.ExecutorService
108import java.util.concurrent.Executors
119import java.util.concurrent.TimeUnit
@@ -47,19 +45,17 @@ fun threadsSolution() {
4745}
4846
4947fun coroutinesSolution () {
50- val scope = CoroutineScope (Dispatchers .IO )
51- val job = scope.launch {
52- for (text in posts) {
53- launch {
54- val token = suspendCoroutine { it.resume(Server .getToken()) }
55- val meta = suspendCoroutine { it.resume(Server .submitPost(text, token)) }
48+ runBlocking {
49+ coroutineScope {
50+ for (text in posts) {
51+ launch(Dispatchers .IO ) {
52+ val token = suspendCoroutine { it.resume(Server .getToken()) }
53+ val meta = suspendCoroutine { it.resume(Server .submitPost(text, token)) }
54+ }
5655 }
5756 }
5857 }
59- runBlocking {
60- job.join()
61- println (" All posts processed" )
62- }
58+ println (" All posts processed" )
6359}
6460
6561fun main () {
You can’t perform that action at this time.
0 commit comments