01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.tools.ant.taskdefs;
20:
21: /**
22: * Interactive Testcase for Processdestroyer.
23: *
24: */
25: public class TestProcess implements Runnable {
26: private boolean run = true;
27: private boolean done = false;
28:
29: public void shutdown() {
30: if (!done) {
31: System.out.println("shutting down TestProcess");
32: run = false;
33:
34: synchronized (this ) {
35: while (!done) {
36: try {
37: wait();
38: } catch (InterruptedException ie) {
39: }
40: }
41: }
42:
43: System.out.println("TestProcess shut down");
44: }
45: }
46:
47: public void run() {
48: for (int i = 0; i < 5 && run; i++) {
49: System.out.println(Thread.currentThread().getName());
50:
51: try {
52: Thread.sleep(2000);
53: } catch (InterruptedException ie) {
54: }
55: }
56:
57: synchronized (this ) {
58: done = true;
59: notifyAll();
60: }
61: }
62:
63: public Thread getShutdownHook() {
64: return new TestProcessShutdownHook();
65: }
66:
67: private class TestProcessShutdownHook extends Thread {
68: public void run() {
69: shutdown();
70: }
71: }
72:
73: public static void main(String[] args) {
74: TestProcess tp = new TestProcess();
75: new Thread(tp, "TestProcess thread").start();
76: Runtime.getRuntime().addShutdownHook(tp.getShutdownHook());
77: }
78: }
|