Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 217
ksl Avatar asked Jan 06 '16 08:01

ksl


People also ask

What is use of join () in threads of Java?

lang. Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t. join() will make sure that t is terminated before the next instruction is executed by the program.

What is currentThread () in Java?

public static Thread currentThread() Returns a reference to the currently executing thread object. Returns: the currently executing thread.

What is the purpose of join ()?

A join() is a final method of Thread class and it can be used to join the start of a thread's execution to the end of another thread's execution so that a thread will not start running until another thread has ended.


2 Answers

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

like image 157
assylias Avatar answered Oct 01 '22 13:10

assylias


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.

like image 22
Peter Lawrey Avatar answered Oct 01 '22 11:10

Peter Lawrey