Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/Finalizer.java
40948 views
1
/*
2
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package nsk.share;
25
26
import java.util.Stack;
27
28
/**
29
* Finalizer performs object finalization when virtual mashine shuts down.
30
* Finalizer is a thread that acts as a VM shutdown hook.
31
* This thread will be activated as VM shuts down because of
32
* invocation of <code>exit()</code> or termination. After activation
33
* Finalizer just calls <code>finalizeAtExit()</code> method of the specified object.
34
* The finalizable object should implement interface <code>Finalizable</code>.
35
*
36
* @see Finalizable
37
*/
38
public class Finalizer {
39
40
/** Finalizer thread to register as a VM shutdown hook. */
41
private static FinalizerThread finalizerThread = null;
42
43
/** An object to finalize. */
44
private Finalizable object;
45
46
/**
47
* Create finalizer for the specified object.
48
*/
49
public Finalizer(Finalizable object) {
50
this.object = object;
51
}
52
53
/**
54
* Register finalizer for finalization at VM shutdown.
55
*/
56
public void activate() {
57
if (finalizerThread == null) {
58
finalizerThread = new FinalizerThread("FinalizerThread for Finalizable objects");
59
finalizerThread.activate();
60
}
61
finalizerThread.add(object);
62
}
63
64
/**
65
* Unregister finalizer for finalization at VM shutdown.
66
*/
67
public void deactivate() {
68
if (finalizerThread == null)
69
return;
70
finalizerThread.remove(object);
71
}
72
73
/**
74
* Static inner thread that is registered as a VM shutdown hook
75
* and performs finalization of all registered finalizable objects.
76
*/
77
private static class FinalizerThread extends Thread {
78
79
/** Stack of objects registered for finalization. */
80
private Stack<Object> objects = new Stack<Object>();
81
82
/** Make new instance of FinalizerThread with given thread name. */
83
public FinalizerThread(String threadName) {
84
super(threadName);
85
}
86
87
/**
88
* Push an object to the stack of registered objects.
89
*/
90
public void add(Finalizable object) {
91
objects.push(object);
92
}
93
94
/**
95
* Remove an object from the stack of registered objects.
96
*/
97
public void remove(Finalizable object) {
98
objects.remove(object);
99
}
100
101
/**
102
* Register finalizer thread as a VM shutdown hook.
103
*/
104
public void activate() {
105
Runtime.getRuntime().addShutdownHook( this );
106
}
107
108
/**
109
* Unregister finalizer thread as a VM shutdown hook.
110
*/
111
public void deactivate() {
112
Runtime.getRuntime().removeShutdownHook( this );
113
}
114
115
/**
116
* Pop all registered objects from the stack and finalize them.
117
*/
118
public void run() {
119
while (!objects.empty()) {
120
Finalizable object = (Finalizable)objects.pop();
121
try {
122
object.finalizeAtExit();
123
} catch (ThreadDeath e) {
124
throw e;
125
} catch (Throwable ex) {
126
ex.printStackTrace();
127
}
128
}
129
}
130
131
} // end of FinalizerThread
132
133
} // end of Finalizer
134
135