Use of Thread.currentThread().join() in Java

It's a common misunderstanding that if the main thread exits, the program will exit.

This is only true if there is no non-daemon thread running. This may be true here, but usually it is better IMHO to make the background threads this main is "waiting" for non-dameon and let the main thread exit when it doesn't have anything to do. I have see developers put Thread.sleep() wrapped in an infinite loop. etc.


Thread.currentThread().join() blocks the current thread forever. In your example, that prevents the main from exiting, unless the program is killed, e.g. with CTRL+C on Windows.

Without that line, the main method would exit right after the server is started.

An alternative would have been to use Thread.sleep(Long.MAX_VALUE);.