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.

3
  • 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. Commented Jan 9, 2012 at 7:17
  • you cannot rely on shutdown hook.look at Java Monitoring and Management Commented Jan 9, 2012 at 7: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. Commented Jan 9, 2012 at 9:53

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I thought about this too, but having a thread just listening to standard in feels hackish too. Does anyone know of a more kosher way to do this?
do not believe in thread. What if OS silently kills the thread that is listening.
What i said is a worst scenario when OS may have blocked your process for some time.
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)

2 Comments

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?
If a non-daemon thread is running then JVM does wait for that thread to finish before exiting.
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)

Comments

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.

2 Comments

I've never coded in Java so I don't know. But if this is on a UNIX, this seems to be the right way to do it.
@merlin2011 - according to anubhava's answer, yes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.