Here is a solution that is single-line as you only need to add one extra line. (You need to add synchronized and throws InterruptedException to your main declaration.) In addition, it does not need access or even knowledge of the threads in the library you use.
public static synchronized void main(String[] args) throws InterruptedException{ ... YourMainClass.class.wait();
It is assumed that you will never call notify in your main class and that you want to exit if you get an InterruptedException . (You can add while (true) { ... } around the wait line if you really want to protect against this.)
Eamonn O'Brien-Strain
source share