Using an embedded Jetty server, I’m unsure about the purpose of server.join().
The server continues to run and serve requests even without calling it.
What exactly does it do, and what happens if I don't call 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.
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).