Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/System/finalization/FinThreads.java
41153 views
1
/*
2
* Copyright (c) 1998, 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
/* @test
25
@bug 4026895
26
@summary Ensure that System.runFinalization does not run finalizers in the
27
thread that invokes it
28
*/
29
30
31
public class FinThreads {
32
33
static Thread mainThread;
34
35
static Object lock = new Object(); /* Protects following two fields */
36
static Thread finalizerThread = null;
37
static Thread finalizedBy = null;
38
39
40
static class Foo {
41
42
boolean catchFinalizer = false;
43
44
/* Instances are only created in an auxiliary thread, in order to
45
guard against stray references from the current thread's stack
46
*/
47
public static void create(final boolean catchFinalizer)
48
throws InterruptedException
49
{
50
Thread t = new Thread(new Runnable() {
51
public void run() {
52
new Foo(catchFinalizer);
53
}});
54
t.start();
55
t.join();
56
}
57
58
public Foo(boolean catchFinalizer) {
59
this.catchFinalizer = catchFinalizer;
60
}
61
62
public void finalize() throws InterruptedException {
63
if (catchFinalizer) {
64
boolean gotFinalizer = false;
65
synchronized (lock) {
66
if (finalizerThread == null) {
67
finalizerThread = Thread.currentThread();
68
gotFinalizer = true;
69
}
70
}
71
if (gotFinalizer) {
72
System.err.println("Caught finalizer thread; sleeping...");
73
Thread.sleep(Long.MAX_VALUE);
74
}
75
} else {
76
synchronized (lock) {
77
finalizedBy = Thread.currentThread();
78
}
79
System.err.println("Test object finalized by " + finalizedBy);
80
}
81
}
82
83
}
84
85
86
static void alarm(final Thread sleeper, final long delay)
87
throws InterruptedException
88
{
89
Thread t = new Thread(new Runnable() {
90
public void run() {
91
try {
92
Thread.sleep(delay);
93
System.err.println("Waking " + sleeper);
94
sleeper.interrupt();
95
} catch (InterruptedException x) { }
96
}});
97
t.setDaemon(true);
98
t.start();
99
}
100
101
102
public static void main(String[] args) throws Exception {
103
104
mainThread = Thread.currentThread();
105
106
/* Find the finalizer thread and put it to sleep */
107
for (;;) {
108
Foo.create(true);
109
System.gc();
110
synchronized (lock) {
111
if (finalizerThread != null) break;
112
}
113
}
114
115
/* Now create a finalizable object and run finalization */
116
alarm(finalizerThread, 5000);
117
Foo.create(false);
118
for (;;) {
119
System.gc();
120
System.runFinalization();
121
synchronized (lock) {
122
if (finalizedBy != null) break;
123
}
124
}
125
126
if (finalizedBy == mainThread)
127
throw new Exception("Finalizer run in main thread");
128
129
}
130
131
}
132
133