18

The following code is taken from an example in the Jersey project. See here.

public class App {

    private static final URI BASE_URI = URI.create("http://localhost:8080/base/");
    public static final String ROOT_PATH = "helloworld";

    public static void main(String[] args) {
        try {
            System.out.println("\"Hello World\" Jersey Example App");

            final ResourceConfig resourceConfig = new ResourceConfig(HelloWorldResource.class);
            final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, false);
            Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                @Override
                public void run() {
                    server.shutdownNow();
                }
            }));
            server.start();

            System.out.println(String.format("Application started.\nTry out %s%s\nStop the application using CTRL+C",
                    BASE_URI, ROOT_PATH));

            //////////////////////////////
            Thread.currentThread().join();
            //////////////////////////////

        } catch (IOException | InterruptedException ex) {
            //
        }

    }
}

I understand what is going on apart from the use of Thread.currentThread().join();.

I'm a Java newbie and my understanding is that this will block the execution of the current thread (in this case, the main thread), and effectively deadlock it. i.e. it will cause the current (main) thread to block until the current (main) thread finishes, which will never happen.

Is this correct? If so, why is it there?

ksl
  • 4,519
  • 11
  • 65
  • 106
  • from the log message you see that the server is started and the main thread will wait until a signal for interrupt is not received, – awsome Jan 06 '16 at 09:02
  • 1
    `Use of Thread.currentThread().join()` is completely, utterly, and entirely meaningless, and a bug. Remove. – user207421 Jan 06 '16 at 09:23

3 Answers3

27

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);.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • 2
    Or make the background thread it is "waiting" non-daemon threads. – Peter Lawrey Jan 06 '16 at 09:04
  • So CTRL+C causes the shutdown hook to be invoked on another thread, which shuts down the server, and then the program terminates? Without the main thread ever running to completion? – ksl Jan 06 '16 at 09:05
  • 1
    @PeterLawrey Indeed - in this case I suspect the thread is a daemon thread and you can't change that - but I don't know this specific library. – assylias Jan 06 '16 at 09:08
10

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

It's an example. It's just not a very good one.

They're trying to show you how to make a thread that runs forever.

Thread.currentThread().join(); is a statement that takes forever to complete. You're supposed to replace it with your own code that runs forever and, presumeably does something useful.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57