2
|
1 |
/*
|
9227
|
2 |
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
/*
|
|
24 |
* @test
|
|
25 |
* @bug 4518797
|
|
26 |
* @summary Make sure that hashCode() and read/writeObject() are thread-safe.
|
9227
|
27 |
* @run main Bug4518797 10
|
2
|
28 |
*/
|
|
29 |
|
|
30 |
import java.util.*;
|
|
31 |
import java.io.*;
|
|
32 |
|
9227
|
33 |
// Usage: java Bug4518797 [duration]
|
2
|
34 |
public class Bug4518797 {
|
|
35 |
static volatile boolean runrun = true;
|
|
36 |
static volatile String message = null;
|
|
37 |
|
|
38 |
public static void main(String[] args) {
|
9227
|
39 |
int duration = 180;
|
|
40 |
if (args.length == 1) {
|
|
41 |
duration = Math.max(5, Integer.parseInt(args[0]));
|
|
42 |
}
|
2
|
43 |
final Locale loc = new Locale("ja", "US");
|
|
44 |
final int hashcode = loc.hashCode();
|
|
45 |
|
|
46 |
System.out.println("correct hash code: " + hashcode);
|
|
47 |
Thread t1 = new Thread(new Runnable() {
|
|
48 |
public void run() {
|
|
49 |
while (runrun) {
|
|
50 |
int hc = loc.hashCode();
|
|
51 |
if (hc != hashcode) {
|
|
52 |
runrun = false;
|
|
53 |
message = "t1: wrong hashcode: " + hc;
|
|
54 |
}
|
|
55 |
}
|
|
56 |
}
|
|
57 |
});
|
|
58 |
|
|
59 |
Thread t2 = new Thread(new Runnable() {
|
|
60 |
public void run() {
|
|
61 |
// Repeat serialization and deserialization. And get the
|
|
62 |
// hash code from a deserialized Locale object.
|
|
63 |
while (runrun) {
|
|
64 |
try {
|
|
65 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
66 |
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
|
67 |
oos.writeObject(loc);
|
|
68 |
byte[] b = baos.toByteArray();
|
|
69 |
oos.close();
|
|
70 |
ByteArrayInputStream bais = new ByteArrayInputStream(b);
|
|
71 |
ObjectInputStream ois = new ObjectInputStream(bais);
|
|
72 |
Locale loc2 = (Locale) ois.readObject();
|
|
73 |
int hc = loc2.hashCode();
|
|
74 |
if (hc != hashcode) {
|
|
75 |
runrun = false;
|
|
76 |
message = "t2: wrong hashcode: " + hc;
|
|
77 |
}
|
|
78 |
} catch (IOException ioe) {
|
|
79 |
runrun = false;
|
|
80 |
throw new RuntimeException("t2: can't perform test", ioe);
|
|
81 |
} catch (ClassNotFoundException cnfe) {
|
|
82 |
runrun = false;
|
|
83 |
throw new RuntimeException("t2: can't perform test", cnfe);
|
|
84 |
}
|
|
85 |
}
|
|
86 |
}
|
|
87 |
});
|
|
88 |
|
|
89 |
t1.start();
|
|
90 |
t2.start();
|
|
91 |
try {
|
9227
|
92 |
for (int i = 0; runrun && i < duration; i++) {
|
2
|
93 |
Thread.sleep(1000);
|
|
94 |
}
|
|
95 |
runrun = false;
|
|
96 |
t1.join();
|
|
97 |
t2.join();
|
|
98 |
} catch (InterruptedException e) {
|
|
99 |
}
|
|
100 |
if (message != null) {
|
|
101 |
throw new RuntimeException(message);
|
|
102 |
}
|
|
103 |
}
|
|
104 |
}
|