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

Hexa

11/17/2019, 10:16 AM
Copy code
val one = GlobalScope.async(start = CoroutineStart.LAZY) { A() }
    val two = GlobalScope.async(start = CoroutineStart.LAZY) { B() }

    one.start()
    Thread.sleep(1000)
    two.start()
is there a better way to wait for
one
to start first before starting
two
without using Thread.sleep in between?
s

spand

11/17/2019, 10:59 AM
If you want to wait for
two
to start until
A()
is done then you can just do
one.join()
ie:
Copy code
val one = GlobalScope.async() { A() }
one.join()
val two = GlobalScope.async() { B() }
h

Hexa

11/17/2019, 11:59 AM
thanks, but i dont necessarily want to wait for A() to be done. I just want to guarantee that
two
will start only after
one
has started
s

spand

11/17/2019, 12:02 PM
Then I would suggest
one
to signal back using a
CompletableDeferred.complete
that you can
join
on then
u

uli

11/17/2019, 2:12 PM
What do you mean by start? When do you consider A started?
You might want to start A undispatched
s

streetsofboston

11/17/2019, 11:05 PM
What is your use-case? And when exactly do you consider
{ A() }
to be started? You'd need a third object that would allow your code to coordinate/synchronize the two.
h

hallvard

11/18/2019, 7:40 AM
You could have
one
start
two
maybe? Since
one
should be in a good position to know exactly when `two`is to be started.
u

uhe

11/18/2019, 9:37 AM
Usually in that case you would indeed use
start = CoroutineStart.UNDISPATCHED
, as Uli already said. I don't think you can use LAZY and then start UNDISPATCHED later, though.
h

Hexa

12/01/2019, 11:07 PM
In the end I just had
one
started
two like @hallvard said
4 Views