Path: blob/master/test/jdk/java/util/Calendar/Bug7017458.java
41149 views
/*1* Copyright (c) 2012, 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*/2223/*24* @test25* @bug 701745826* @summary Test of multithreaded serialization/deserialization of Calendar.27*/2829import java.io.*;30import java.util.Calendar;3132public class Bug7017458 {3334static volatile boolean err = false;3536public static void main(String[] args) {37try {38new Bug7017458().perform();39}40catch (Exception e) {41e.printStackTrace();42err = true;43}4445if (err) {46throw new RuntimeException("Multithreaded serialization/deserialization test failed.");47} else {48System.out.println("Multithreaded serialization/deserialization test passed.");49}50}5152public void perform() throws Exception {53int nbThreads = 8;54Calendar cal = Calendar.getInstance();55SerializationThread[] threads = new SerializationThread[nbThreads];56for (int i = 0; i < nbThreads; i++) {57threads[i] = new SerializationThread(cal);58}59for (int i = 0; i < nbThreads; i++) {60threads[i].start();61}62for (int i = 0; i < nbThreads; i++) {63threads[i].join();64}6566DeserializationThread[] threads2 = new DeserializationThread[nbThreads];67for (int i = 0; i < nbThreads; i++) {68threads2[i] = new DeserializationThread(threads[i].data);69}70for (int i = 0; i < nbThreads; i++) {71threads2[i].start();72}73for (int i = 0; i < nbThreads; i++) {74threads2[i].join();75}76}7778public class SerializationThread extends Thread {79private Calendar cal;80public byte[] data;8182public SerializationThread(Calendar cal) {83this.cal = cal;84}8586public void run() {87try {88ByteArrayOutputStream baos = new ByteArrayOutputStream();89ObjectOutputStream oos = new ObjectOutputStream(baos);90oos.writeObject(cal);91oos.flush();92oos.close();93data = baos.toByteArray();94}95catch (Exception e) {96e.printStackTrace();97err = true;98}99}100}101102public class DeserializationThread extends Thread {103public Calendar cal;104public byte[] data;105106public DeserializationThread(byte[] data) {107this.data = data;108}109110public void run() {111try {112ByteArrayInputStream bais = new ByteArrayInputStream(data);113ObjectInputStream ois = new ObjectInputStream(bais);114cal = (Calendar) ois.readObject();115ois.close();116}117catch (Exception e) {118e.printStackTrace();119err = true;120}121}122}123}124125126127