what is the correct way to wait for a thread to fi...
# announcements
j
what is the correct way to wait for a thread to finish? Is this ok or is there a better way to do it ?
Copy code
val mythread = thread { .... some code here that runs in this thread ... }
... more code here ...
while (mythread.isAlive) {
    Thread.sleep(10)
}
do something else that depends on things setup during the thread
s
Thread.join()
2
j
ah, that's pretty neat, just
mythread.join()
and it waits. Thanks Johannes!!
c
why would you offload the work to other thread if you're blocking current thread to wait for the results?
j
Both processes are running at the same time and it makes a huge improvement in performance, I only need to ensure the order in which they finish since the last line requires some data from the finished thread.
c
Ah, the ... more code here ... part 🙂 I see
I suggest looking at coroutines though, manual thread management is dangerous.
j
This is just an application bootstrap script, one part starts up an ORM and the other part sets up the rest of the app. The last bit starts listening for messages on a queue, that part should wait for the ORM before starting to accept messages. CO-routines sounds like an option as well.