bitkid
04/05/2018, 11:30 AMbitkid
04/05/2018, 11:33 AMbitkid
04/05/2018, 11:57 AMstreetsofboston
04/05/2018, 1:30 PMJoey Heck
04/05/2018, 6:42 PMval gson = GsonBuilder().create()
val jsonArray: JSONArray = responseJsonObject.getJSONArray("Value")
val parsedItemArray = List<Deferred<Item>>(jsonArray.length()) {
async { gson.fromJson(jsonArray.get(it).toString(), Item::class.java) }
}
runBlocking {
parsedItemArray .forEach { defered ->
val item = defered.await()
}
}
So I'm parsing an array of json objects in a JSON array using gson into a list of Deferreds. I call the parsing in an async, then I wait until they are all done. I call a timestamp before and after it's done
the same code without coroutines runs faster
val gson = GsonBuilder().create()
val jsonArray: JSONArray = responseJsonObject.getJSONArray("Value")
val parsedItemArray = List<Item>(jsonArray.length()) {
gson.fromJson(jsonArray.get(it).toString(), Item::class.java)
}
I was using a parallel class (found on stack overflow) previously with much better results
object Parallel {
private val iCPU = Runtime.getRuntime().availableProcessors()
interface LoopBody<T> {
fun run(i: T)
}
fun For(start: Int, stop: Int, loopBody: LoopBody<Int>) {
val executor = Executors.newFixedThreadPool(iCPU)
val futures = LinkedList<Future<*>>()
for (i in start until stop) {
val future = executor.submit { loopBody.run(i) }
futures.add(future)
}
for (f in futures) {
try {
f.get()
} catch (e: InterruptedException) {
} catch (e: ExecutionException) {
}
}
executor.shutdown()
}
}
I call it with this
val gson = GsonBuilder().create()
val jsonArray: JSONArray = responseJsonObject.getJSONArray("Value")
val parsedItemArray = arrayOfNulls<Item>(jsonArray.length())
Parallel.For(0, jsonArray.length(), object : Parallel.LoopBody<Int> {
override fun run(i: Int) {
parsedItemArray[i] = gson.fromJson(jsonArray.get(i).toString(), Item::class.java)
}
})
Joey Heck
04/05/2018, 6:47 PMjw
04/06/2018, 2:24 AMaltavir
04/06/2018, 11:26 AMselect
clause this way:
val job = launch {
while (true) {
select<Unit> {
stateHolder.forEach { state ->
state.future.onAwait {
// do something
}
}
}
}
}
But it does not seem to enter the select clause even once. What am I doing wrong?Kulwinder Singh
04/06/2018, 1:17 PMvenkat
04/07/2018, 10:50 PMDmytro Danylyk
04/09/2018, 10:25 AMCoroutineContext#DEBUG
field public and mutable? In android we usually rely on generated BuildConfig.DEBUG
field.pakoito
04/10/2018, 10:25 AMjw
04/10/2018, 4:20 PMjkbbwr
04/11/2018, 11:45 AMtapchicoma
04/12/2018, 6:44 AMBjarne Gelotte
04/13/2018, 9:04 AMval first: Deferred<Unit> = async { firstTask() }
val second: Deferred<Unit> = async { secondTask() }
first.await().let { doFirstStuff() }
second.await().let { doSecondStuff() }
In this example, will the doFirstStuff()
always be called before doSecondStuff()
? Or can doSecondStuff
trigger as soon as second
is evaluated (this is what I want to achieve)?gildor
04/13/2018, 9:18 AMval first: Deferred<Unit> = async {
firstTask().let { doFirstStuff() }
}
val second: Deferred<Unit> = async {
secondTask().let { doSecondStuff() }
}
first.await()
second.await()
gildor
04/13/2018, 9:48 AMDaniel Tam
04/13/2018, 12:37 PMDeferred<T>
?uli
04/16/2018, 5:08 PMAndrew Gazelka
04/16/2018, 5:16 PMresult
. I want to quickly find a test case, where result < c
, where c
is a constant. Is there any way I can cleanly structure my code to achieve this?Andrew Gazelka
04/16/2018, 9:02 PMJsoup
connections uses blocking (Thread.sleep()
I think)... this effectively makes each coroutine the same as a thread. Is there anyway to turn these API `sleep()`s into `delay()`s?Andrew Gazelka
04/16/2018, 9:14 PMDaniel Tam
04/17/2018, 12:57 PMclose
or cancel
on them?Robert Menke
04/17/2018, 1:39 PMPromise.all
in the standard library?
Something kind of along the lines of this… but better 😛
fun <T>all(list : List<suspend () -> T>) : Deferred<List<T>> {
return async {
list.map { async { it() } }
.map { it.await() }
}
}
withoutclass
04/17/2018, 5:54 PMlouiscad
04/18/2018, 6:54 AMmutex.withLock(owner = coroutineContext) {
// Some suspend calls
}
Dario Pellegrini
04/18/2018, 8:05 AMpetersommerhoff
04/19/2018, 9:14 AMuli
04/19/2018, 7:39 PMwhile (deferredExports.any { !it.isCompleted && !it.isCompletedExceptionally }) {
select<Unit> {
deferredExports.forEach {
if (!it.isCompleted && !it.isCompletedExceptionally) {
it.onAwait {
}
}
}
}
}
uli
04/19/2018, 7:39 PMwhile (deferredExports.any { !it.isCompleted && !it.isCompletedExceptionally }) {
select<Unit> {
deferredExports.forEach {
if (!it.isCompleted && !it.isCompletedExceptionally) {
it.onAwait {
}
}
}
}
}
Vsevolod Tolstopyatov [JB]
04/20/2018, 8:02 AM!it.isCompleted && !it.isCompletedExceptionally
is not necessary here in any sense: await
performs same check and in this code it’s not atomic, so it’s possible to await
deferred in Completed
state after checking for !isCompleted
.
Check doesn’t improve neither readability nor performanceuli
04/20/2018, 8:14 AM