Youssef Shoaib [MOD]
03/28/2023, 3:57 PMThread
that's already running (for legacy reasons, it needs to be a Thread
for now). Inside a suspend
function, how can I call thread.join()
in a "better" manner? As in, so that the coroutine suspends until the thread finishes.CLOVIS
03/28/2023, 4:08 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>) {
thread.join()
}
mkrussel
03/28/2023, 4:13 PMYoussef Shoaib [MOD]
03/28/2023, 4:15 PMthread.isAlive
is false, so wouldn't I be able to use that as a condition to wait for? I do have control over the thread code, but I'm trying to not be too invasiveYoussef Shoaib [MOD]
03/28/2023, 4:15 PMcoroutineScope { launch { while (thread.isAlive) delay(10) } }
simon.vergauwen
03/28/2023, 4:17 PMwithContext
.
A non-blocking option would be to somehow pass a CompletableDeferred
as the task running in the Thread
and await that.
I would personally prefer join
in favor of looping, you can potentially also use newSingleThreadContext instead of <http://Dispatchers.IO|Dispatchers.IO>
but be sure to close it.CLOVIS
03/28/2023, 4:19 PMrunInterruptible(<http://Dispatchers.IO|Dispatchers.IO>)
looks more appropriate. It's still blocking a thread.Youssef Shoaib [MOD]
03/28/2023, 4:23 PMCompletableDeferred
and completing it at the end of the thread's code, then awaiting it instead of calling join
CLOVIS
03/28/2023, 4:24 PMsimon.vergauwen
03/28/2023, 4:25 PMtry { } finally { }
block so it gets called regardless of exceptions 😉Youssef Shoaib [MOD]
03/28/2023, 4:28 PMCLOVIS
03/28/2023, 4:30 PM