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/util/Properties/ConcurrentLoadAndStoreXML.java
47178 views
1
/*
2
* Copyright (c) 2012, 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 8005281
26
* @summary Test that the Properties storeToXML and loadFromXML methods are
27
* thread safe
28
* @key randomness
29
* @run main ConcurrentLoadAndStoreXML
30
* @run main/othervm -Dsun.util.spi.XmlPropertiesProvider=jdk.internal.util.xml.BasicXmlPropertiesProvider ConcurrentLoadAndStoreXML
31
32
*/
33
34
import java.io.*;
35
import java.util.Properties;
36
import java.util.Random;
37
import java.util.concurrent.Callable;
38
import java.util.concurrent.Executors;
39
import java.util.concurrent.ExecutorService;
40
import java.util.concurrent.Future;
41
42
public class ConcurrentLoadAndStoreXML {
43
44
static final Random RAND = new Random();
45
46
static volatile boolean done;
47
48
/**
49
* Simple task that bashes on storeToXML and loadFromXML until the "done"
50
* flag is set.
51
*/
52
static class Basher implements Callable<Void> {
53
final Properties props;
54
55
Basher(Properties props) {
56
this.props = props;
57
}
58
59
public Void call() throws IOException {
60
while (!done) {
61
62
// store as XML format
63
ByteArrayOutputStream out = new ByteArrayOutputStream();
64
props.storeToXML(out, null, "UTF-8");
65
66
// load from XML format
67
Properties p = new Properties();
68
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
69
p.loadFromXML(in);
70
71
// check that the properties are as expected
72
if (!p.equals(props))
73
throw new RuntimeException("Properties not equal");
74
}
75
return null;
76
}
77
}
78
79
public static void main(String[] args) throws Exception {
80
final int NTASKS = 4 + RAND.nextInt(4);
81
82
// create Bashers with Properties of random keys and values
83
Basher[] basher = new Basher[NTASKS];
84
for (int i=0; i<NTASKS; i++) {
85
Properties props = new Properties();
86
for (int j=0; j<RAND.nextInt(100); j++) {
87
String key = "k" + RAND.nextInt(1000);
88
String value = "v" + RAND.nextInt(1000);
89
props.put(key, value);
90
}
91
basher[i] = new Basher(props);
92
}
93
94
ExecutorService pool = Executors.newFixedThreadPool(NTASKS);
95
try {
96
// kick off the bashers
97
Future<Void>[] task = new Future[NTASKS];
98
for (int i=0; i<NTASKS; i++) {
99
task[i] = pool.submit(basher[i]);
100
}
101
102
// give them time to interfere with each each
103
Thread.sleep(2000);
104
done = true;
105
106
// check the result
107
for (int i=0; i<NTASKS; i++) {
108
task[i].get();
109
}
110
} finally {
111
done = true;
112
pool.shutdown();
113
}
114
115
}
116
}
117
118