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: * @author Pavel Dolgov
19: * @version $Revision$
20: */package org.apache.harmony.awt.wtk;
21:
22: /**
23: * Shutdown Watchdog
24: */
25: public final class ShutdownWatchdog {
26:
27: private boolean nativeQueueEmpty = true;
28: private boolean awtQueueEmpty = true;
29: private boolean windowListEmpty = true;
30:
31: private boolean forcedShutdown = false;
32:
33: private ShutdownThread thread;
34:
35: public synchronized void setNativeQueueEmpty(boolean empty) {
36: nativeQueueEmpty = empty;
37: checkShutdown();
38: }
39:
40: public synchronized void setAwtQueueEmpty(boolean empty) {
41: awtQueueEmpty = empty;
42: checkShutdown();
43: }
44:
45: public synchronized void setWindowListEmpty(boolean empty) {
46: windowListEmpty = empty;
47: checkShutdown();
48: }
49:
50: public synchronized void forceShutdown() {
51: forcedShutdown = true;
52: shutdown();
53: }
54:
55: public synchronized void start() {
56: keepAlive();
57: }
58:
59: private void checkShutdown() {
60: if (canShutdown()) {
61: shutdown();
62: } else {
63: keepAlive();
64: }
65: }
66:
67: private boolean canShutdown() {
68: return (nativeQueueEmpty && awtQueueEmpty && windowListEmpty)
69: || forcedShutdown;
70: }
71:
72: private void keepAlive() {
73: if (thread == null) {
74: thread = new ShutdownThread();
75: thread.start();
76: }
77: }
78:
79: private void shutdown() {
80: if (thread != null) {
81: thread.shutdown();
82: thread = null;
83: }
84: }
85: }
|