Path: blob/master/test/jdk/java/util/Locale/Bug4518797.java
41149 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 451879725* @summary Make sure that hashCode() and read/writeObject() are thread-safe.26* @run main Bug4518797 1027*/2829import java.util.*;30import java.io.*;3132// Usage: java Bug4518797 [duration]33public class Bug4518797 {34static volatile boolean runrun = true;35static volatile String message = null;3637public static void main(String[] args) {38int duration = 180;39if (args.length == 1) {40duration = Math.max(5, Integer.parseInt(args[0]));41}42final Locale loc = new Locale("ja", "US");43final int hashcode = loc.hashCode();4445System.out.println("correct hash code: " + hashcode);46Thread t1 = new Thread(new Runnable() {47public void run() {48while (runrun) {49int hc = loc.hashCode();50if (hc != hashcode) {51runrun = false;52message = "t1: wrong hashcode: " + hc;53}54}55}56});5758Thread t2 = new Thread(new Runnable() {59public void run() {60// Repeat serialization and deserialization. And get the61// hash code from a deserialized Locale object.62while (runrun) {63try {64ByteArrayOutputStream baos = new ByteArrayOutputStream();65ObjectOutputStream oos = new ObjectOutputStream(baos);66oos.writeObject(loc);67byte[] b = baos.toByteArray();68oos.close();69ByteArrayInputStream bais = new ByteArrayInputStream(b);70ObjectInputStream ois = new ObjectInputStream(bais);71Locale loc2 = (Locale) ois.readObject();72int hc = loc2.hashCode();73if (hc != hashcode) {74runrun = false;75message = "t2: wrong hashcode: " + hc;76}77} catch (IOException ioe) {78runrun = false;79throw new RuntimeException("t2: can't perform test", ioe);80} catch (ClassNotFoundException cnfe) {81runrun = false;82throw new RuntimeException("t2: can't perform test", cnfe);83}84}85}86});8788t1.start();89t2.start();90try {91for (int i = 0; runrun && i < duration; i++) {92Thread.sleep(1000);93}94runrun = false;95t1.join();96t2.join();97} catch (InterruptedException e) {98}99if (message != null) {100throw new RuntimeException(message);101}102}103}104105106