3

More specifically, I have a multithreaded command line Java application which runs and collects data until the user terminates it.

The obvious way for the user to terminate it is by pushing Control-C, but then I need to install a shutdown hook in the VM and deal with all the threads.

Is there a nicer / more appropriate way for the user to inform the application that it's time to shutdown?

For example, is there a way to capture some other key combination and set a boolean flag in my application?

As a further clarification, I seek something functionally similar to signal handling in C.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • I don't know whether this is proper or not: extend Thread's `run() { while(!isInterrupted()) { /* run your java codes here */ }}}` Then, call `interrupt()` for the extended Thread class. – ee. Jan 09 '12 at 07:17
  • you cannot rely on shutdown hook.look at Java Monitoring and Management – Dead Programmer Jan 09 '12 at 07:44
  • @merlin: again merlin ,according to your question you want graceful ( clean up the resources and say bye bye and come out ) .if you want to detect abnormal termination , you need to handle outside JVM boundary i.e. at OS level. To detect whether your process launch java process inside a Perl script, but have the script wait for the JVM using the waitpid system call. – Dead Programmer Jan 09 '12 at 09:53

4 Answers4

2

One way can be to create a new thread which will "listen" to standard input. Based on whatever key pattern you decide, that thread can set the flag in the main application.

Manish
  • 3,913
  • 2
  • 29
  • 45
1

Is there a nicer / more appropriate way for the user to inform the application that it's time to shutdown?

The best way is to use Java Monitoring and Management

Look at this post for example.

It is best not to rely on shutdown hook.Shutdown hook in java works for KILL -15 AND KILL and do not work for KILL -9 (HARD KILL)

Community
  • 1
  • 1
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
1

Consider using shutdown hook like this:

Runtime.getRuntime().addShutdownHook(shutdownHook);

to have your own code that runs whenever the JVM terminates under 1 of the following conditions:

  1. The program exits normally, such as when the last non-daemon thread exits or when the Runtime.exit() method is invoked.
  2. The virtual machine is terminated in response to a user interrupt, such as typing CTRL-C, or a system-wide event, such as user logoff or system shutdown (for example, the JVM receives one of the interrupt signals SIGHUP (Unix Only), SIGINT, or SIGTERM).

You can refer to: http://www.ibm.com/developerworks/ibm/library/i-signalhandling/ for more details (Disclaimer: very old article pertains to JDK 1.3.1)

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • When do the other threads run with respect to the shutdown hook's running time? That is, does the shutdown hook or the VM wait for active threads to finish? – merlin2011 Jan 09 '12 at 07:47
  • If a non-daemon thread is running then JVM does wait for that thread to finish before exiting. – anubhava Jan 09 '12 at 08:27
0

This is not a Java specific solution but (atleast on Linux) during shutdown, the operating system sends a SIGTERM to all processes (following by a SIGKILL after a grace period). Your application should install a handler for this and then shutdown gracefully. Once you do this, it will automatically take care of itself when you shutdown your VM.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169