https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

dave08

03/06/2018, 4:51 PM
But in Android
FutureTask
can receive a callback when it's finished, so you wouldn't have to block an extra thread to wait for the result... and it implements Future from api level 1...
j

Jonathan

03/06/2018, 4:54 PM
So you can write your own suspend await() on it.
Yes. But anyway can link the doc of this
FutureTask
please ?
I only found this: https://developer.android.com/reference/java/util/concurrent/FutureTask.html and it is not possible to install any callback on it.
d

dave08

03/06/2018, 5:00 PM
Look at the constructor..
FutureTask(Callable<V> callable)
j

Jonathan

03/06/2018, 5:01 PM
That is what will be executed... this is not a callback.
There is no way install a callback on it once the instance exist. And if you want to create a future you could anyway directly use "kotlinx.coroutines"
async { doSomethingUseful() }
d

dave08

03/06/2018, 5:05 PM
Ohh, you're right... I was in the middle of something else and didn't notice... the truth is that my use case was in using Koush/Ion http client, but then they have their own future implementation w/ a callback... it's funny that Android doesn't have one.. I guess that's back to AsysncTask 🤒
Which is like async {}
...
j

Jonathan

03/06/2018, 5:06 PM
😉
d

dave08

03/06/2018, 5:07 PM
I guess when the result is not so important to get right away one can do a loop w/ delay and isDone check... to avoid blocking the thread but that's probably overkill
j

Jonathan

03/06/2018, 5:10 PM
Yeah... I think I would rather create an async task which wait on the result and maybe use a dedicated thread pool for it. But the best would be to not have to deal with such future. Only dealing with
Deferred
and eventually
CompletableFuture
would be my aim.
👍🏼 1
4 Views