Embedded Jetty why to use join

Loner shushman picture Loner shushman · Apr 10, 2013 · Viewed 12.6k times · Source

An example taken from an Embedded Jetty tutorial suggests the following:

public static void main(String[] args) throws Exception
{
   Server server = new Server(8080);
   server.setHandler(new HelloHandler());

   server.start();
   server.join();}

Why do I need to add the line server.join()? It works fine without it.

Answer

AlexR picture AlexR · Apr 10, 2013

join() is blocking until server is ready. It behaves like Thread.join() and indeed calls join() of Jetty's thread pool. Everything works without this because jetty starts very quickly. However if your application is heavy enough the start might take some time. Call of join() guarantees that after it the server is indeed ready.