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: package java.lang;
18:
19: /**
20: * @author Mikhail Y. Fursov
21: * @version $Revision: 1.1.2.1.4.4 $
22: */
23: class EMThreadSupport {
24:
25: private static final Object lock = new Object();
26: private static boolean active = false;
27: private static int timeout = 0;
28: private static Thread profilerThread = null;
29:
30: static void initialize() {
31: boolean needThreadsSuport = needProfilerThreadSupport();
32: if (!needThreadsSuport) {
33: return;
34: }
35: timeout = getTimeout();
36: if (timeout < 0) {
37: throw new RuntimeException("Illegal timeout value:"
38: + timeout);
39: }
40: if (timeout == 0) {
41: return;
42: }
43: active = true;
44: Runnable emWorker = new Runnable() {
45: public void run() {
46: EMThreadSupport.run();
47: }
48: };
49: profilerThread = new Thread(Thread.systemThreadGroup, emWorker,
50: "profiler thread");
51: profilerThread.setDaemon(true);
52: profilerThread.start();
53: }
54:
55: static void shutdown() {
56: active = false;
57: synchronized (lock) {
58: lock.notify();
59: }
60: try {
61: if (profilerThread != null) {
62: profilerThread.join();
63: }
64: } catch (InterruptedException e) {
65: }
66: }
67:
68: static void run() {
69: while (active) {
70: onTimeout();
71: waitTimeout(timeout);
72: }
73: }
74:
75: private static void waitTimeout(long timeout) {
76: try {
77: synchronized (lock) {
78: lock.wait(timeout);
79: }
80: } catch (InterruptedException e) {
81: }
82: }
83:
84: private static native boolean needProfilerThreadSupport();
85:
86: private static native void onTimeout();
87:
88: private static native int getTimeout();
89:
90: }
|