0

Using Embedded Jetty, I don't understand what server.join() does.

If I remove it, nothing happen, my server continue to work and keep serving requests.

What server.join() does? What happens if I remove it? In which case I would like to have it?

Here an example from the official Jetty documentation:


    public static void main(String[] args) throws Exception
    {
        int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
        Server server = new Server(port);
        server.setHandler(new HelloWorld());

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

I know the question was already asked here but the accepted answer has -6 upvotes so I thought it's worth asking it again.

David
  • 135
  • 3
  • 7

1 Answers1

3

The call server.start() starts a new thread for the Server.

The call server.join() is to have the current thread to wait until the server is done before executing the next step on it's thread.

This is the normal java.lang.Thread.join() behavior.

In short, it makes the current thread wait for the conclusion of the thread it's asked to join.

If you had an action after server.join() you would see that it doesn't execute until the server thread has completed. (usually due to a graceful shutdown action).

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Hi @JoakimErdfelt :) - That's awesome that you're the same person who put the comment with the most upvotes in the same question asked 7 years ago. Thanks a lot! – David Dec 01 '20 at 10:01