Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/util/calendar/CalendarSystemDeadLockTest.java
66644 views
1
/*
2
* Copyright (c) 2021, 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.ArrayList;
25
import java.util.Collections;
26
import java.util.List;
27
import java.util.concurrent.Callable;
28
import java.util.concurrent.CountDownLatch;
29
import java.util.concurrent.ExecutorService;
30
import java.util.concurrent.Executors;
31
import java.util.concurrent.Future;
32
33
/**
34
* @test
35
* @bug 8273790
36
* @summary Verify that concurrent classloading of sun.util.calendar.Gregorian and
37
* sun.util.calendar.CalendarSystem doesn't lead to a deadlock
38
* @modules java.base/sun.util.calendar:open
39
* @run main/othervm CalendarSystemDeadLockTest
40
* @run main/othervm CalendarSystemDeadLockTest
41
* @run main/othervm CalendarSystemDeadLockTest
42
* @run main/othervm CalendarSystemDeadLockTest
43
* @run main/othervm CalendarSystemDeadLockTest
44
*/
45
public class CalendarSystemDeadLockTest {
46
47
public static void main(final String[] args) throws Exception {
48
testConcurrentClassLoad();
49
}
50
51
/**
52
* Loads {@code sun.util.calendar.Gregorian} and {@code sun.util.calendar.CalendarSystem}
53
* and invokes {@code sun.util.calendar.CalendarSystem#getGregorianCalendar()} concurrently
54
* in a thread of their own and expects the classloading of both those classes
55
* to succeed. Additionally, after these tasks are done, calls the
56
* sun.util.calendar.CalendarSystem#getGregorianCalendar() and expects it to return a singleton
57
* instance
58
*/
59
private static void testConcurrentClassLoad() throws Exception {
60
final int numTasks = 7;
61
final CountDownLatch taskTriggerLatch = new CountDownLatch(numTasks);
62
final List<Callable<?>> tasks = new ArrayList<>();
63
// add the sun.util.calendar.Gregorian and sun.util.calendar.CalendarSystem for classloading.
64
// there are main 2 classes which had a cyclic call in their static init
65
tasks.add(new ClassLoadTask("sun.util.calendar.Gregorian", taskTriggerLatch));
66
tasks.add(new ClassLoadTask("sun.util.calendar.CalendarSystem", taskTriggerLatch));
67
// add a few other classes for classloading, those which call CalendarSystem#getGregorianCalendar()
68
// or CalendarSystem#forName() during their static init
69
tasks.add(new ClassLoadTask("java.util.GregorianCalendar", taskTriggerLatch));
70
tasks.add(new ClassLoadTask("java.util.Date", taskTriggerLatch));
71
tasks.add(new ClassLoadTask("java.util.JapaneseImperialCalendar", taskTriggerLatch));
72
// add a couple of tasks which directly invoke sun.util.calendar.CalendarSystem#getGregorianCalendar()
73
tasks.add(new GetGregorianCalTask(taskTriggerLatch));
74
tasks.add(new GetGregorianCalTask(taskTriggerLatch));
75
// before triggering the tests make sure we have created the correct number of tasks
76
// the countdown latch uses/expects
77
if (numTasks != tasks.size()) {
78
throw new RuntimeException("Test setup failure - unexpected number of tasks " + tasks.size()
79
+ ", expected " + numTasks);
80
}
81
final ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
82
try {
83
final Future<?>[] results = new Future[tasks.size()];
84
// submit
85
int i = 0;
86
for (final Callable<?> task : tasks) {
87
results[i++] = executor.submit(task);
88
}
89
// wait for completion
90
for (i = 0; i < tasks.size(); i++) {
91
results[i].get();
92
}
93
} finally {
94
executor.shutdownNow();
95
}
96
// check that the sun.util.calendar.CalendarSystem#getGregorianCalendar() does indeed return
97
// a proper instance
98
final Object gCal = callCalSystemGetGregorianCal();
99
if (gCal == null) {
100
throw new RuntimeException("sun.util.calendar.CalendarSystem#getGregorianCalendar()" +
101
" unexpectedly returned null");
102
}
103
// now verify that each call to getGregorianCalendar(), either in the tasks or here, returned the exact
104
// same instance
105
if (GetGregorianCalTask.instances.size() != 2) {
106
throw new RuntimeException("Unexpected number of results from call " +
107
"to sun.util.calendar.CalendarSystem#getGregorianCalendar()");
108
}
109
// intentional identity check since sun.util.calendar.CalendarSystem#getGregorianCalendar() is
110
// expected to return a singleton instance
111
if ((gCal != GetGregorianCalTask.instances.get(0)) || (gCal != GetGregorianCalTask.instances.get(1))) {
112
throw new RuntimeException("sun.util.calendar.CalendarSystem#getGregorianCalendar()" +
113
" returned different instances");
114
}
115
}
116
117
/**
118
* Reflectively calls sun.util.calendar.CalendarSystem#getGregorianCalendar() and returns
119
* the result
120
*/
121
private static Object callCalSystemGetGregorianCal() throws Exception {
122
final Class<?> k = Class.forName("sun.util.calendar.CalendarSystem");
123
return k.getDeclaredMethod("getGregorianCalendar").invoke(null);
124
}
125
126
private static class ClassLoadTask implements Callable<Class<?>> {
127
private final String className;
128
private final CountDownLatch latch;
129
130
private ClassLoadTask(final String className, final CountDownLatch latch) {
131
this.className = className;
132
this.latch = latch;
133
}
134
135
@Override
136
public Class<?> call() {
137
System.out.println(Thread.currentThread().getName() + " loading " + this.className);
138
try {
139
// let the other tasks know we are ready to trigger our work
140
latch.countDown();
141
// wait for the other task to let us know they are ready to trigger their work too
142
latch.await();
143
return Class.forName(this.className);
144
} catch (Exception e) {
145
throw new RuntimeException(e);
146
}
147
}
148
}
149
150
private static class GetGregorianCalTask implements Callable<Object> {
151
// keeps track of the instances returned by calls to sun.util.calendar.CalendarSystem#getGregorianCalendar()
152
// by this task
153
private static final List<Object> instances = Collections.synchronizedList(new ArrayList<>());
154
private final CountDownLatch latch;
155
156
private GetGregorianCalTask(final CountDownLatch latch) {
157
this.latch = latch;
158
}
159
160
@Override
161
public Object call() {
162
System.out.println(Thread.currentThread().getName()
163
+ " calling sun.util.calendar.CalendarSystem#getGregorianCalendar()");
164
try {
165
// let the other tasks know we are ready to trigger our work
166
latch.countDown();
167
// wait for the other task to let us know they are ready to trigger their work too
168
latch.await();
169
final Object inst = callCalSystemGetGregorianCal();
170
instances.add(inst);
171
return inst;
172
} catch (Exception e) {
173
throw new RuntimeException(e);
174
}
175
}
176
}
177
}
178
179