Path: blob/master/test/jdk/java/text/Format/DateFormat/Bug4407042.java
41152 views
/*1* Copyright (c) 2001, 2016, 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 440704226* @summary Make sure that cloned SimpleDateFormat objects work27* independently in multiple threads.28* @library /java/text/testlib29* @run main Bug4407042 1030*/3132import java.io.*;33import java.text.*;34import java.util.*;3536// Usage: java Bug4407042 [duration]37public class Bug4407042 {3839static final String TIME_STRING = "2000/11/18 00:01:00";40static final long UTC_LONG = 974534460000L;41static SimpleDateFormat masterFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");42static boolean runrun = true;43static int duration = 100;4445void test() {46Locale locale = Locale.getDefault();47if (!TestUtils.usesAsciiDigits(locale)48|| !TestUtils.usesGregorianCalendar(locale)) {49System.out.println("Skipping this test because locale is " + locale);50return;51}5253masterFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));54DateParseThread d1 = new DateParseThread();55DateFormatThread d2 = new DateFormatThread();56d1.start();57d2.start();58int n = Thread.activeCount();59boolean failed = false;6061for (int i = 0; i < duration; i++) {62try {63Thread.sleep(1000);64if (Thread.activeCount() != n) {65failed = true;66break;67}68} catch (InterruptedException e) {69}70}71runrun = false;72try {73d1.join();74d2.join();75} catch (InterruptedException e) {76}77if (failed) {78throw new RuntimeException("Failed");79}80}8182synchronized static SimpleDateFormat getFormatter() {83return (SimpleDateFormat) masterFormat.clone();84}8586static class DateParseThread extends Thread {87public void run() {88SimpleDateFormat sdf = getFormatter();89Calendar cal = null;9091try {92int i = 0;93while (runrun) {94Date date =sdf.parse(TIME_STRING);95long t = date.getTime();96i++;97if (t != UTC_LONG) {98throw new RuntimeException("Parse Error: " + i +99" (" + sdf.format(date) + ") " + t +100" != " + UTC_LONG);101}102}103} catch (ParseException e) {104e.printStackTrace();105throw new RuntimeException("Parse Error");106}107}108}109110static class DateFormatThread extends Thread {111public void run () {112SimpleDateFormat sdf = getFormatter();113Calendar cal = null;114115int i = 0;116while (runrun) {117i++;118String s = sdf.format(new Date(UTC_LONG));119if (!s.equals(TIME_STRING)) {120throw new RuntimeException("Format Error: " + i + " " +121s + " != " + TIME_STRING);122}123}124}125}126127public static void main (String[] args) {128if (args.length == 1) {129duration = Math.max(10, Integer.parseInt(args[0]));130}131new Bug4407042().test();132}133}134135136