1 17 18 package org.apache.tools.ant.taskdefs; 19 20 24 public class TestProcess 25 implements Runnable 26 { 27 private boolean run = true; 28 private boolean done = false; 29 30 public void shutdown() 31 { 32 if (!done) 33 { 34 System.out.println("shutting down TestProcess"); 35 run = false; 36 37 synchronized(this) 38 { 39 while (!done) 40 { 41 try { wait(); } catch (InterruptedException ie) {} 42 } 43 } 44 45 System.out.println("TestProcess shut down"); 46 } 47 } 48 49 public void run() 50 { 51 for (int i = 0; i < 5 && run; i++) 52 { 53 System.out.println(Thread.currentThread().getName()); 54 55 try { Thread.sleep(2000); } catch (InterruptedException ie) {} 56 } 57 58 synchronized(this) 59 { 60 done = true; 61 notifyAll(); 62 } 63 } 64 65 public Thread getShutdownHook() 66 { 67 return new TestProcessShutdownHook(); 68 } 69 70 private class TestProcessShutdownHook 71 extends Thread 72 { 73 public void run() 74 { 75 shutdown(); 76 } 77 } 78 79 public static void main(String [] args) 80 { 81 TestProcess tp = new TestProcess(); 82 new Thread (tp, "TestProcess thread").start(); 83 Runtime.getRuntime().addShutdownHook(tp.getShutdownHook()); 84 } 85 } 86 | Popular Tags |