Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Thread/ITLConstructor.java
38812 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
/*
25
* @test
26
* @summary Basic test for Thread(ThreadGroup,Runnable,String,long,boolean)
27
*/
28
29
import sun.misc.SharedSecrets;
30
31
import java.security.AccessControlContext;
32
import java.security.AccessController;
33
import java.security.PrivilegedAction;
34
35
public class ITLConstructor {
36
static InheritableThreadLocal<Integer> n = new InheritableThreadLocal<Integer>() {
37
protected Integer initialValue() {
38
return 0;
39
}
40
41
protected Integer childValue(Integer parentValue) {
42
return parentValue + 1;
43
}
44
};
45
46
static final int CHILD_THREAD_COUNT = 10;
47
48
public static void main(String args[]) throws Exception {
49
test();
50
}
51
52
static void test() throws Exception {
53
// concurrent access to separate indexes is ok
54
int[] x = new int[CHILD_THREAD_COUNT];
55
Thread child = createThread(new AnotherRunnable(0, x));
56
child.start();
57
child.join(); // waits for *all* threads to complete
58
59
// Check results
60
for(int i=0; i<CHILD_THREAD_COUNT; i++) {
61
int expectedValue = 1;
62
if (x[i] != expectedValue)
63
throw (new Exception("Got x[" + i + "] = " + x[i]
64
+ ", expected: " + expectedValue));
65
}
66
}
67
68
static class AnotherRunnable implements Runnable {
69
final int threadId;
70
final int[] x;
71
AnotherRunnable(int threadId, int[] x) {
72
this.threadId = threadId;
73
this.x = x;
74
}
75
76
public void run() {
77
int itlValue = n.get();
78
79
if (threadId < CHILD_THREAD_COUNT-1) {
80
Thread child = createThread(
81
new AnotherRunnable(threadId+1, x));
82
child.start();
83
try {
84
child.join();
85
} catch(InterruptedException e) {
86
throw(new RuntimeException("Interrupted", e));
87
}
88
}
89
90
x[threadId] = itlValue+1;
91
}
92
}
93
94
static Thread createThread(final Runnable r) {
95
final AccessControlContext acc = AccessController.getContext();
96
// 4290486: doPrivileged is needed to create a thread in
97
// an environment that restricts "modifyThreadGroup".
98
return AccessController.doPrivileged(
99
new PrivilegedAction<Thread>() {
100
public Thread run() {
101
return SharedSecrets.getJavaLangAccess()
102
.newThreadWithAcc(r, acc);
103
}
104
}
105
);
106
}
107
}
108
109