Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/gc/FinalizationRunner.java
41153 views
1
/*
2
* Copyright (c) 2015, 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
import java.util.concurrent.CountDownLatch;
25
26
import jdk.test.lib.dcmd.CommandExecutor;
27
import jdk.test.lib.dcmd.JMXExecutor;
28
29
public class FinalizationRunner {
30
public static final String FAILED = "Failed";
31
public static final String PASSED = "Passed";
32
33
static volatile boolean wasFinalized = false;
34
private static final CountDownLatch finRunLatch = new CountDownLatch(1);
35
private static final CountDownLatch finBlockLatch = new CountDownLatch(1);
36
37
static class MyObject {
38
@Override
39
protected void finalize() {
40
if (Thread.currentThread().getName().equals("Finalizer")) {
41
try {
42
System.out.println("inside the regular finalizer thread; blocking");
43
// 'regular' finalizer thread is ready to be effectively blocked -
44
// we can continue with the GC.run_finalization test
45
finRunLatch.countDown();
46
// prevent the 'regular' finalizer from finalizing this instance
47
// until the GC.run_finalization has had its chance to do so
48
finBlockLatch.await();
49
} catch (InterruptedException e) {
50
}
51
} else {
52
if (Thread.currentThread().getName().equals("Secondary finalizer")) {
53
System.out.println("finalizing the test instance");
54
// finalizing on behalf of GC.run_finalization -
55
// unblock the 'regular' finalizer and the test main method
56
wasFinalized = true;
57
} else {
58
fail("Unexpected finalizer thread name: " +
59
Thread.currentThread().getName());
60
}
61
finBlockLatch.countDown();
62
}
63
}
64
}
65
66
// this instance will be used to provoke the regular finalization
67
// so the finalizer thread can be blocked for the duration of
68
// GC.run_finalization test
69
public static MyObject o1;
70
71
// this instance will be used to perform the GC.run_finalization test
72
public static MyObject o2;
73
74
private static void run(CommandExecutor executor) {
75
o2 = new MyObject();
76
o2 = null;
77
System.out.println("running GC.run_finalization");
78
System.gc();
79
executor.execute("GC.run_finalization");
80
81
System.out.println("Waiting for finalization");
82
83
try {
84
finBlockLatch.await();
85
if (wasFinalized) {
86
System.out.println(PASSED + ": Object was finalized");
87
} else {
88
fail("Object was not finalized");
89
}
90
} catch (InterruptedException e) {
91
fail("Interrupted while waiting for finalization", e);
92
}
93
}
94
95
public static void main(String ... args) {
96
System.out.println("\n=== FinalizationRunner");
97
try {
98
blockFinalizerThread();
99
100
Runtime.getRuntime().addShutdownHook(new Thread(()->{
101
run(new JMXExecutor());
102
}));
103
} catch (InterruptedException e) {
104
fail("Interrupted while trying to block the finalizer thread", e);
105
}
106
}
107
108
private static void blockFinalizerThread() throws InterruptedException {
109
System.out.println("trying to block the finalizer thread");
110
o1 = new MyObject();
111
o1 = null;
112
System.gc();
113
finRunLatch.await();
114
}
115
116
private static void fail(String msg, Exception e) {
117
fail(msg);
118
e.printStackTrace(System.err);
119
}
120
121
private static void fail(String msg) {
122
System.err.println(FAILED + ": " + msg);
123
}
124
}
125
126