By one line I mean at most 100 chars per line.
(I basically need this to keep the program alive. The main thread registers callback listeners that are run in separate threads. I just need the main one to hang forever and let the other threads do their work)
synchronized(this) {
while (true) {
this.wait();
}
}
(thanks to Carlos Heuberger. Exception handling omitted in the above code)
This will make the current thread wait on the monitor of the current class until someone calls notify()
, or forever.
There are a few things you could do that would be better than hanging the initial thread forever:
otherThread.join()
. This will cause the current thread you are running in to sleep until the other thread has finished executing.otherThread.setDaemon(false)
and simply let your initial thread exit. This will set your new threads as user threads. Java will not shut down until the only threads running are daemon threads.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With