Skip to content

Commit 3d79771

Browse files
committed
slight modification of solution 3
1 parent 26d739a commit 3d79771

File tree

1 file changed

+10
-14
lines changed
  • src/main/kotlin/cub/async/answers

1 file changed

+10
-14
lines changed

src/main/kotlin/cub/async/answers/Task3.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package cub.async.answers
22

33
import cub.async.tasks.Server
44
import 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
97
import java.util.concurrent.ExecutorService
108
import java.util.concurrent.Executors
119
import java.util.concurrent.TimeUnit
@@ -47,19 +45,17 @@ fun threadsSolution() {
4745
}
4846

4947
fun 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

6561
fun main() {

0 commit comments

Comments
 (0)