Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Future.get() a replacement for Thread.join()?

Tags:

People also ask

What does Future get do?

A Future interface provides methods to check if the computation is complete, to wait for its completion and to retrieve the results of the computation. The result is retrieved using Future's get() method when the computation has completed, and it blocks until it is completed.

Does Future create thread?

Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache.

Does the thread join () method do?

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 Future multithreading?

Think of a Future as an object that holds the result – it may not hold it right now, but it will do so in the future (once the Callable returns). Thus, a Future is basically one way the main thread can keep track of the progress and result from other threads.


I want to write a command line daemon that runs forever. I understand that if I want the JVM to be able to shutdown gracefully in linux, one needs to wrap the bootstrap via some C code. I think I'll be ok with a shutdown hook for now.

On to my questions:

  1. My main(String[]) block will fire off a separate Superdaemon.
  2. The Superdaemon will poll and loop forever.

So normally I would do:

class Superdaemon extends Thread { ... }

class Bootstrap
{
    public static void main( String[] args )
    {
        Thread t = new Superdaemon();
        t.start();

        t.join();
    }
}

Now I figured that if I started Superdaemon via an Executor, I can do

Future<?> f = exec.submit( new Superdaemon() );

f.get();

Is Future.get() implemented with Thread.join() ? If not, does it behave equivalently ?

Regards,

ashitaka