If I have a ```Channel<Unit>(capacity = Chan...
# coroutines
d
If I have a
Copy code
Channel<Unit>(capacity = Channel.UNLIMITED)
and I never close it, is it guaranteed that
offer
will always succeed?
z
yes, until you hit a memory issue
or if the scope is cancelled
oops, channels don’t have a scope haha
d
and specifically, if I call
receive()
at most once, I should be in good shape?
(basically just trying to make a latch where one coroutine can
wait
on it, and various other places (which aren't all coroutines) can allow it to continue)
z
receive like in a while loop?
d
nah, just once
z
why not use a deferred then?
d
just like a one-time synchronization primitive, "wait until some other coro lets me continue"
well, it's fulfillable by multiple other places
this is like "let the main loop finish if any of these various places tell us the program should be over", that sort of thing
z
CompletableDeferred<Unit>()
should be effectively the same thing but with the guarantee it can only emit once
d
with
.complete(Unit)
and
.await()
?
z
yeah
d
and i can just do
parent = null
happily?
z
not sure what you mean by parent
d
initialize it with
CompletableDeferred<Unit>(parent = null)
rather than somethign else. this is all kinda at top program level
z
even if you are top level, you still have a top level scope
d
so i'm ok with no cancellation happening (in fact, i would rather not think about this Deferred being cancellable)
z
oh yeah,
parent
is
null
by default - that’s fine
since you are synchronizing between 2 coroutines