0N/A/*
3969N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A * @test
0N/A * @bug 4518797
0N/A * @summary Make sure that hashCode() and read/writeObject() are thread-safe.
3969N/A * @run main Bug4518797 10
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/A
3969N/A// Usage: java Bug4518797 [duration]
0N/Apublic class Bug4518797 {
0N/A static volatile boolean runrun = true;
0N/A static volatile String message = null;
0N/A
0N/A public static void main(String[] args) {
3969N/A int duration = 180;
3969N/A if (args.length == 1) {
3969N/A duration = Math.max(5, Integer.parseInt(args[0]));
3969N/A }
0N/A final Locale loc = new Locale("ja", "US");
0N/A final int hashcode = loc.hashCode();
0N/A
0N/A System.out.println("correct hash code: " + hashcode);
0N/A Thread t1 = new Thread(new Runnable() {
0N/A public void run() {
0N/A while (runrun) {
0N/A int hc = loc.hashCode();
0N/A if (hc != hashcode) {
0N/A runrun = false;
0N/A message = "t1: wrong hashcode: " + hc;
0N/A }
0N/A }
0N/A }
0N/A });
0N/A
0N/A Thread t2 = new Thread(new Runnable() {
0N/A public void run() {
0N/A // Repeat serialization and deserialization. And get the
0N/A // hash code from a deserialized Locale object.
0N/A while (runrun) {
0N/A try {
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A ObjectOutputStream oos = new ObjectOutputStream(baos);
0N/A oos.writeObject(loc);
0N/A byte[] b = baos.toByteArray();
0N/A oos.close();
0N/A ByteArrayInputStream bais = new ByteArrayInputStream(b);
0N/A ObjectInputStream ois = new ObjectInputStream(bais);
0N/A Locale loc2 = (Locale) ois.readObject();
0N/A int hc = loc2.hashCode();
0N/A if (hc != hashcode) {
0N/A runrun = false;
0N/A message = "t2: wrong hashcode: " + hc;
0N/A }
0N/A } catch (IOException ioe) {
0N/A runrun = false;
0N/A throw new RuntimeException("t2: can't perform test", ioe);
0N/A } catch (ClassNotFoundException cnfe) {
0N/A runrun = false;
0N/A throw new RuntimeException("t2: can't perform test", cnfe);
0N/A }
0N/A }
0N/A }
0N/A });
0N/A
0N/A t1.start();
0N/A t2.start();
0N/A try {
3969N/A for (int i = 0; runrun && i < duration; i++) {
0N/A Thread.sleep(1000);
0N/A }
0N/A runrun = false;
0N/A t1.join();
0N/A t2.join();
0N/A } catch (InterruptedException e) {
0N/A }
0N/A if (message != null) {
0N/A throw new RuntimeException(message);
0N/A }
0N/A }
0N/A}