KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > TestProcess


1 /*
2  * Copyright 2001,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.taskdefs;
19
20 /**
21  * Interactive Testcase for Processdestroyer.
22  *
23  */

24 public class TestProcess
25     implements Runnable JavaDoc
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 JavaDoc 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 JavaDoc ie) {}
56         }
57
58         synchronized(this)
59         {
60             done = true;
61             notifyAll();
62         }
63     }
64
65     public Thread JavaDoc getShutdownHook()
66     {
67         return new TestProcessShutdownHook();
68     }
69
70     private class TestProcessShutdownHook
71         extends Thread JavaDoc
72     {
73         public void run()
74         {
75             shutdown();
76         }
77     }
78
79     public static void main(String JavaDoc[] args)
80     {
81         TestProcess tp = new TestProcess();
82         new Thread JavaDoc(tp, "TestProcess thread").start();
83         Runtime.getRuntime().addShutdownHook(tp.getShutdownHook());
84     }
85 }
86
Popular Tags