Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/text/Format/DateFormat/Bug4407042.java
48795 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* @run main Bug4407042 1029*/3031import java.io.*;32import java.text.*;33import java.util.*;3435// Usage: java Bug4407042 [duration]36public class Bug4407042 {3738static final String TIME_STRING = "2000/11/18 00:01:00";39static final long UTC_LONG = 974534460000L;40static SimpleDateFormat masterFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");41static boolean runrun = true;42static int duration = 100;4344void test() {45Locale locale = Locale.getDefault();46if (locale.equals(new Locale("th", "TH")) ||47locale.equals(new Locale("hi", "IN"))) {48return;49}5051masterFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));52DateParseThread d1 = new DateParseThread();53DateFormatThread d2 = new DateFormatThread();54d1.start();55d2.start();56int n = Thread.activeCount();57boolean failed = false;5859for (int i = 0; i < duration; i++) {60try {61Thread.sleep(1000);62if (Thread.activeCount() != n) {63failed = true;64break;65}66} catch (InterruptedException e) {67}68}69runrun = false;70try {71d1.join();72d2.join();73} catch (InterruptedException e) {74}75if (failed) {76throw new RuntimeException("Failed");77}78}7980synchronized static SimpleDateFormat getFormatter() {81return (SimpleDateFormat) masterFormat.clone();82}8384static class DateParseThread extends Thread {85public void run() {86SimpleDateFormat sdf = getFormatter();87Calendar cal = null;8889try {90int i = 0;91while (runrun) {92Date date =sdf.parse(TIME_STRING);93long t = date.getTime();94i++;95if (t != UTC_LONG) {96throw new RuntimeException("Parse Error: " + i +97" (" + sdf.format(date) + ") " + t +98" != " + UTC_LONG);99}100}101} catch (ParseException e) {102e.printStackTrace();103throw new RuntimeException("Parse Error");104}105}106}107108static class DateFormatThread extends Thread {109public void run () {110SimpleDateFormat sdf = getFormatter();111Calendar cal = null;112113int i = 0;114while (runrun) {115i++;116String s = sdf.format(new Date(UTC_LONG));117if (!s.equals(TIME_STRING)) {118throw new RuntimeException("Format Error: " + i + " " +119s + " != " + TIME_STRING);120}121}122}123}124125public static void main (String[] args) {126if (args.length == 1) {127duration = Math.max(10, Integer.parseInt(args[0]));128}129new Bug4407042().test();130}131}132133134