J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | /* |
| 24 | * @test |
| 25 | * @bug 4518797 |
| 26 | * @summary Make sure that hashCode() and read/writeObject() are thread-safe. |
| 27 | * @run main/timeout=200 Bug4518797 |
| 28 | */ |
| 29 | |
| 30 | import java.util.*; |
| 31 | import java.io.*; |
| 32 | |
| 33 | public class Bug4518797 { |
| 34 | static volatile boolean runrun = true; |
| 35 | static volatile String message = null; |
| 36 | |
| 37 | public static void main(String[] args) { |
| 38 | final Locale loc = new Locale("ja", "US"); |
| 39 | final int hashcode = loc.hashCode(); |
| 40 | |
| 41 | System.out.println("correct hash code: " + hashcode); |
| 42 | Thread t1 = new Thread(new Runnable() { |
| 43 | public void run() { |
| 44 | while (runrun) { |
| 45 | int hc = loc.hashCode(); |
| 46 | if (hc != hashcode) { |
| 47 | runrun = false; |
| 48 | message = "t1: wrong hashcode: " + hc; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | }); |
| 53 | |
| 54 | Thread t2 = new Thread(new Runnable() { |
| 55 | public void run() { |
| 56 | // Repeat serialization and deserialization. And get the |
| 57 | // hash code from a deserialized Locale object. |
| 58 | while (runrun) { |
| 59 | try { |
| 60 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 61 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 62 | oos.writeObject(loc); |
| 63 | byte[] b = baos.toByteArray(); |
| 64 | oos.close(); |
| 65 | ByteArrayInputStream bais = new ByteArrayInputStream(b); |
| 66 | ObjectInputStream ois = new ObjectInputStream(bais); |
| 67 | Locale loc2 = (Locale) ois.readObject(); |
| 68 | int hc = loc2.hashCode(); |
| 69 | if (hc != hashcode) { |
| 70 | runrun = false; |
| 71 | message = "t2: wrong hashcode: " + hc; |
| 72 | } |
| 73 | } catch (IOException ioe) { |
| 74 | runrun = false; |
| 75 | throw new RuntimeException("t2: can't perform test", ioe); |
| 76 | } catch (ClassNotFoundException cnfe) { |
| 77 | runrun = false; |
| 78 | throw new RuntimeException("t2: can't perform test", cnfe); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | }); |
| 83 | |
| 84 | t1.start(); |
| 85 | t2.start(); |
| 86 | try { |
| 87 | for (int i = 0; runrun && i < 180; i++) { |
| 88 | Thread.sleep(1000); |
| 89 | } |
| 90 | runrun = false; |
| 91 | t1.join(); |
| 92 | t2.join(); |
| 93 | } catch (InterruptedException e) { |
| 94 | } |
| 95 | if (message != null) { |
| 96 | throw new RuntimeException(message); |
| 97 | } |
| 98 | } |
| 99 | } |