Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could do the following:</p> <ul> <li>Let the shutdown hook set some AtomicBoolean (or volatile boolean) "keepRunning" to false</li> <li>(Optionally, <code>.interrupt</code> the working threads if they wait for data in some blocking call)</li> <li>Wait for the working threads (executing <code>writeBatch</code> in your case) to finish, by calling the <code>Thread.join()</code> method on the working threads.</li> <li>Terminate the program</li> </ul> <p>Some sketchy code:</p> <ul> <li>Add a <code>static volatile boolean keepRunning = true;</code></li> <li><p>In <em>run()</em> you change to</p> <pre><code>for (int i = 0; i &lt; N &amp;&amp; keepRunning; ++i) writeBatch(pw, i); </code></pre></li> <li><p>In <em>main()</em> you add:</p> <pre><code>final Thread mainThread = Thread.currentThread(); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { keepRunning = false; mainThread.join(); } }); </code></pre></li> </ul> <p>That's roughly how I do a graceful "reject all clients upon hitting Control-C" in terminal.</p> <hr> <p>From <a href="http://download.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29" rel="noreferrer">the docs</a>:</p> <blockquote> <p>When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. <strong>When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt.</strong></p> </blockquote> <p>That is, a shutdown hook keeps the JVM running until the hook has terminated (returned from the <em>run()</em>-method.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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