...
Code Block | ||
---|---|---|
| ||
class ControlledStop implements Runnable{ protected volatile boolean done = false; public void run() { while(!done) { try { Thread.currentThread().sleep(1000); } catch(InterruptedException ie) { System.out.println("Interrupted Exception"); // Handle the exception } finally { System.out.println("Closing resources"); } done = true;} done = } false; } // Reset System.out.println("Done!"); } protected void shutdown(){ done = true; } } class Controller { public static void main(String[] args) throws InterruptedException { ControlledStop c = new ControlledStop(); Thread t = new Thread(c); t.start(); t.interrupt(); // Artificially induce an InterruptedException Thread.sleep(1000); // Wait for some time to allow the exception // to be caught (demonstration only) c.shutdown(); } } |
...