Edgars
12/05/2018, 8:50 PMandyb
12/05/2018, 9:06 PMJoris PZ
12/05/2018, 9:19 PMJoris PZ
12/05/2018, 9:21 PM('a'..'z')
.map { unit ->
GlobalScope.async {
firstPolymer.reduce(unit).count()
}
}
.map { it.await() }
.min()
.print { "Minimum length : $it" }
Joris PZ
12/05/2018, 9:22 PMJob
instances, with each Job taking care of ignoring a character. (Note that using GlobalScope
is bad form in general, but good enough for now.)
In the second map, we wait for each Job
in turn to finish, giving us our list of IntsJoris PZ
12/05/2018, 9:24 PMEdgars
12/05/2018, 9:28 PMGlobalScope.
to the async command from the post I linked. Why did it not work before, though?Joris PZ
12/05/2018, 9:29 PMJoris PZ
12/05/2018, 9:40 PMrunBlocking
defines a scopeJoris PZ
12/05/2018, 9:45 PMrunBlocking
uses a single thread for execution (the current calling thread), which means all Jobs would still use the same thread.
By using GlobalScope
, the Jobs are started on a different scope, which does use the default dispatcher and therefore multiple threads.
The same could be achieved by changing the code in the blog post to runBlocking(Dispatchers.Default) { ... }
, which makes runBlocking
use the default dispatcher as well.
I suspect the original code worked fine before the introduction of structured concurrency, but should be updated now.Edgars
12/06/2018, 6:43 AM