Sam
11/24/2018, 5:04 PMDominaezzz
11/24/2018, 5:16 PMrunBlocking
to block the calling thread until all the `launch`ed child coroutines finish?Sam
11/24/2018, 5:44 PMrunBlocking {
launch {
delay( 200 )
println( "first launch done" )
}
launch {
delay( 100 )
println( "second launch done" )
}
}
Dominaezzz
11/24/2018, 5:45 PMlaunch
to finish before the second launch
?Sam
11/24/2018, 5:45 PMbdawg.io
11/24/2018, 5:47 PMDominaezzz
11/24/2018, 5:47 PMSam
11/24/2018, 5:48 PMbdawg.io
11/24/2018, 5:48 PMlaunch
is just overhead at this pointSam
11/24/2018, 5:50 PMrepeat( 1000 ) {
runBlocking {
launch {
println( "first launch done" )
}
launch {
println( "second launch done" )
}
}
}
Dominaezzz
11/24/2018, 5:51 PMSam
11/24/2018, 5:54 PMrepeat( 10 ) {
GlobalScope.launch {
launch {
println( "first launch done" )
}
launch {
println( "second launch done" )
}
}
Thread.sleep (100 )
}
runBlocking {
launch {
var sum = 0
repeat( 100_000_0000 ) {
sum += 1
}
println( "first launch sum $sum" )
}
launch {
var sum = 0
repeat( 10 ) {
sum += 1
}
println( "second launch sum $sum" )
}
}
Dominaezzz
11/24/2018, 6:44 PMSam
11/24/2018, 6:49 PMrunBlocking {
launch {
val time = measureTimeMillis {
BigInteger(3500, Random()).nextProbablePrime()
}
println( "first launch sum $time" )
}
launch {
println( "second launch sum" )
}
}
bdawg.io
11/24/2018, 7:20 PMrunBlocking(Dispatchers.Default) { ... }
?Sam
11/24/2018, 7:23 PMbdawg.io
11/24/2018, 7:24 PMGlobalScope.launch
will give you a different result because it uses Dispatchers.Default/CommonPool to obtain additional threads which allows more of your concurrent jobs to execute in parallel.Sam
11/24/2018, 7:25 PMbdawg.io
11/24/2018, 7:28 PMrunBlocking
by itself, it only has the current thread, so your first launch goes first on that single thread until it completes or suspends (which is why your 1 billion sum completes before the second starts), there's no additional threads for the second job to go in parallelSam
11/24/2018, 7:30 PMbdawg.io
11/24/2018, 7:37 PMSam
11/24/2018, 7:38 PMDaniel Tam
11/25/2018, 10:16 AMjoin
on itpankajrai
11/25/2018, 12:15 PMbdawg.io
11/25/2018, 10:25 PMrunBlocking