I have an abstract Java class which runs as a Thread. It does not have a main method. Only run(), start(), stop(), etc.
I want to add a SignalHandler to the class. I've tried this:
Add this to the initialize method:
addSignalHandler();
That executes this method:
public void addSignalHandler() {
Signal.handle(new Signal("INT"), new SignalHandler() {
public void handle(Signal sig) {
System.out.println("I shall exit now");
keepRunning = false;
System.out.println("I shall exit now - keepRunning is false");
}
});
System.out.println("Added signal handler");
}
And I see the 'Added signal handler' in my log file.
Now I want to kill the process from my Linux server, so I run this:
kill -SIGINT PID
Nothing happens. The process still runs and I don't see 'I shall exit now'.
EDIT:
My run() method contains this:
while (keepRunning) {
// do stuff
}