Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/StringInternSyncWithGC/StringGenerator.java
41155 views
1
/*
2
* Copyright (c) 2017, 2018, 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
package gc.gctests.StringInternSyncWithGC;
24
25
import java.lang.ref.Reference;
26
import java.lang.ref.SoftReference;
27
import java.lang.ref.WeakReference;
28
import java.util.List;
29
import java.util.concurrent.locks.ReadWriteLock;
30
import java.util.concurrent.locks.ReentrantReadWriteLock;
31
import nsk.share.TestBug;
32
import nsk.share.TestFailure;
33
import nsk.share.gc.gp.string.RandomStringProducer;
34
import nsk.share.test.ExecutionController;
35
import nsk.share.test.LocalRandom;
36
37
class StringGenerator implements Runnable {
38
39
private final RandomStringProducer gp;
40
private final List<String> stringsToIntern;
41
private final int threadNumber;
42
private final int numberOfActions;
43
private final int maxStringSize;
44
private final StringInternSyncWithGC base;
45
private static final ReadWriteLock RWLOCK = new ReentrantReadWriteLock();
46
47
public StringGenerator(int threadId, StringInternSyncWithGC base) {
48
this.base = base;
49
threadNumber = threadId;
50
stringsToIntern = base.getStringsToIntern();
51
numberOfActions = base.getNumberOfThreads() > 4 ? base.getNumberOfThreads() : 4;
52
gp = base.getGarbageProducer();
53
maxStringSize = base.getMaxStringSize();
54
}
55
56
/* This field is public just to be not optimized */
57
public String copy;
58
59
@Override
60
public void run() {
61
ExecutionController stresser = base.getExecController();
62
try {
63
RWLOCK.readLock().lock();
64
Object[] refToInterned = new Object[stringsToIntern.size()];
65
for (int i = 0; i < stringsToIntern.size(); i++) {
66
if (!stresser.continueExecution()) {
67
return;
68
}
69
int index = LocalRandom.nextInt(stringsToIntern.size());
70
String str = stringsToIntern.get(index);
71
int action = LocalRandom.nextInt(numberOfActions);
72
73
/* We want to provoke a lot of collections for each interned copy
74
* so map should be "sparse".
75
*/
76
copy = new String(str);
77
switch (action) {
78
case 0:
79
refToInterned[index] = copy.intern();
80
break;
81
case 1:
82
refToInterned[index] = new WeakReference(copy.intern());
83
break;
84
case 2:
85
refToInterned[index] = new SoftReference(copy.intern());
86
break;
87
default:
88
refToInterned[index] = null;
89
break;
90
}
91
}
92
for (int index = 0; index < stringsToIntern.size(); index++) {
93
verify(refToInterned[index], stringsToIntern.get(index));
94
}
95
} finally {
96
RWLOCK.readLock().unlock();
97
}
98
99
if (threadNumber == 0) {
100
try {
101
RWLOCK.writeLock().lock();
102
for (int index = 0; index < stringsToIntern.size(); index++) {
103
stringsToIntern.set(index, gp.create(maxStringSize).intern());
104
}
105
} finally {
106
RWLOCK.writeLock().unlock();
107
}
108
}
109
}
110
111
/*
112
* Verify that all exist interned strings in a map
113
* a same objects.
114
*/
115
private void verify(Object obj, String str) {
116
if (obj == null) {
117
return;
118
}
119
if (obj instanceof Reference) {
120
obj = ((Reference) obj).get();
121
if (obj == null) {
122
return;
123
}
124
}
125
if (!(obj instanceof String)) {
126
throw new TestBug("Expected String. Find :" + obj.getClass());
127
}
128
String interned = (String) obj;
129
if (!interned.equals(str)) {
130
throw new TestFailure("Interned not equals to original string.");
131
}
132
if (obj != str.intern()) {
133
throw new TestFailure("Interned not same as original string.");
134
}
135
}
136
}
137
138