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:
- My main(String[]) block will fire off a separate Superdaemon.
- 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
Yes, the way you’ve written these is equivalent.
However, you don’t really need to wait for the Superdaemon thread to complete. When the main thread finishes executing main(), that thread exits, but the JVM will not. The JVM will keep running until the last non-daemon thread exits its run method.
For example,
You’ll see the output:
In other words, the main thread finishes first, then the secondary thread completes and the JVM exits.