|
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 /* |
|
25 * @test |
|
26 * @bug 7017458 |
|
27 * @summary Test of multithreaded serialization/deserialization of Calendar. |
|
28 */ |
|
29 |
|
30 import java.io.*; |
|
31 import java.util.Calendar; |
|
32 |
|
33 public class Bug7017458 { |
|
34 |
|
35 static volatile boolean err = false; |
|
36 |
|
37 public static void main(String[] args) { |
|
38 try { |
|
39 new Bug7017458().perform(); |
|
40 } |
|
41 catch (Exception e) { |
|
42 e.printStackTrace(); |
|
43 err = true; |
|
44 } |
|
45 |
|
46 if (err) { |
|
47 throw new RuntimeException("Multithreaded serialization/deserialization test failed."); |
|
48 } else { |
|
49 System.out.println("Multithreaded serialization/deserialization test passed."); |
|
50 } |
|
51 } |
|
52 |
|
53 public void perform() throws Exception { |
|
54 int nbThreads = 8; |
|
55 Calendar cal = Calendar.getInstance(); |
|
56 SerializationThread[] threads = new SerializationThread[nbThreads]; |
|
57 for (int i = 0; i < nbThreads; i++) { |
|
58 threads[i] = new SerializationThread(cal); |
|
59 } |
|
60 for (int i = 0; i < nbThreads; i++) { |
|
61 threads[i].start(); |
|
62 } |
|
63 for (int i = 0; i < nbThreads; i++) { |
|
64 threads[i].join(); |
|
65 } |
|
66 |
|
67 DeserializationThread[] threads2 = new DeserializationThread[nbThreads]; |
|
68 for (int i = 0; i < nbThreads; i++) { |
|
69 threads2[i] = new DeserializationThread(threads[i].data); |
|
70 } |
|
71 for (int i = 0; i < nbThreads; i++) { |
|
72 threads2[i].start(); |
|
73 } |
|
74 for (int i = 0; i < nbThreads; i++) { |
|
75 threads2[i].join(); |
|
76 } |
|
77 } |
|
78 |
|
79 public class SerializationThread extends Thread { |
|
80 private Calendar cal; |
|
81 public byte[] data; |
|
82 |
|
83 public SerializationThread(Calendar cal) { |
|
84 this.cal = cal; |
|
85 } |
|
86 |
|
87 public void run() { |
|
88 try { |
|
89 ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
90 ObjectOutputStream oos = new ObjectOutputStream(baos); |
|
91 oos.writeObject(cal); |
|
92 oos.flush(); |
|
93 oos.close(); |
|
94 data = baos.toByteArray(); |
|
95 } |
|
96 catch (Exception e) { |
|
97 e.printStackTrace(); |
|
98 err = true; |
|
99 } |
|
100 } |
|
101 } |
|
102 |
|
103 public class DeserializationThread extends Thread { |
|
104 public Calendar cal; |
|
105 public byte[] data; |
|
106 |
|
107 public DeserializationThread(byte[] data) { |
|
108 this.data = data; |
|
109 } |
|
110 |
|
111 public void run() { |
|
112 try { |
|
113 ByteArrayInputStream bais = new ByteArrayInputStream(data); |
|
114 ObjectInputStream ois = new ObjectInputStream(bais); |
|
115 cal = (Calendar) ois.readObject(); |
|
116 ois.close(); |
|
117 } |
|
118 catch (Exception e) { |
|
119 e.printStackTrace(); |
|
120 err = true; |
|
121 } |
|
122 } |
|
123 } |
|
124 } |
|
125 |