Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/ThreadLocal/TLRemoveTest.java
41152 views
1
/*
2
* Copyright (c) 2004, 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 functional test of remove method for ThreadLocal
27
* @author Seetharama Avadhanam
28
*/
29
30
public class TLRemoveTest {
31
private static final int INITIAL_VALUE = 101;
32
private static final int REMOVE_SET_VALUE = 102;
33
34
static ThreadLocal<Integer> n = new ThreadLocal<Integer>() {
35
protected synchronized Integer initialValue() {
36
return INITIAL_VALUE;
37
}
38
};
39
40
public static void main(String args[]) throws Throwable {
41
int threadCount = 100;
42
final int[] removeNode = {10,20,45,38};
43
// ThreadLocal values will be removed for these threads.
44
final int[] removeAndSet = {12,34,10};
45
// ThreadLocal values will be removed and sets new values..
46
47
Thread th[] = new Thread[threadCount];
48
final int x[] = new int[threadCount];
49
final Throwable exceptions[] = new Throwable[threadCount];
50
51
for(int i = 0; i<threadCount; i++) {
52
final int threadId = i;
53
th[i] = new Thread() {
54
public void run() {
55
try{
56
n.set(threadId); // Sets threadId as threadlocal value...
57
for (int j = 0; j<threadId; j++)
58
Thread.currentThread().yield();
59
60
// To remove the ThreadLocal ....
61
for(int removeId : removeNode)
62
if(threadId == removeId){
63
n.remove(); // Removes ThreadLocal values..
64
break;
65
}
66
67
// To remove the ThreadLocal value and set new value ...
68
for(int removeId : removeAndSet)
69
if(threadId == removeId){
70
n.remove(); // Removes the ThreadLocal Value...
71
n.set(REMOVE_SET_VALUE); /* Setting new Values to
72
ThreadLocal */
73
break;
74
}
75
/* Storing the threadlocal values in 'x'
76
...so that it can be used for checking results... */
77
x[threadId] = n.get();
78
}
79
catch(Throwable ex){
80
exceptions[threadId] = ex;
81
}
82
}
83
};
84
th[i].start();
85
}
86
87
// Wait for the threads to finish
88
for(int i = 0; i<threadCount; i++)
89
th[i].join();
90
91
// Check results
92
for(int i = 0; i<threadCount; i++){
93
int checkValue = i;
94
95
/* If the remove method is called then the ThreadLocal value will
96
* be its initial value */
97
for(int removeId : removeNode)
98
if(removeId == i){
99
checkValue = INITIAL_VALUE;
100
break;
101
}
102
103
for(int removeId : removeAndSet)
104
if(removeId == i){
105
checkValue = REMOVE_SET_VALUE;
106
break;
107
}
108
109
if(exceptions[i] != null)
110
throw(exceptions[i]);
111
if(x[i] != checkValue)
112
throw(new Throwable("x[" + i + "] =" + x[i]));
113
}
114
}
115
}
116
117