Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Warning, this is <strong>not</strong> what you should do. <a href="http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html" rel="nofollow">The usual reasons</a> for avoiding the sun package apply. Think thrice before actually using this in real code. At minimum the access to the signal handling should be done with reflection and fall back to registering the usual shutdown hook when it fails. I left that out for brevity.</p> <p>So, all hope abandon, ye who enter here:</p> <pre><code>import java.util.concurrent.atomic.AtomicBoolean; import sun.misc.Signal; import sun.misc.SignalHandler; public class Sigint { private static void prepareShutdownHandling() { // This would be the normal shutdown hook final Thread shutdown = new Thread() { @Override public void run() { // Simulate slow shutdown for (int i = 0; i &lt; 10; i++) { System.out.println("Normal shutdown running"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(System.err); } } System.out.println("Normal shutdown finished"); System.exit(0); } }; Signal.handle(new Signal("INT"), new SignalHandler() { private AtomicBoolean inShutdown = new AtomicBoolean(); public void handle(Signal sig) { if (inShutdown.compareAndSet(false, true)) { // Normal shutdown shutdown.start(); } else { System.err.println("Emergency shutdown"); System.exit(1); } } }); } public static void main(String args[]) { prepareShutdownHandling(); while (true) { System.out.println("Main program running"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(System.err); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload