]> granicus.if.org Git - icu/commitdiff
ICU-11487 Fixed lookup trie initialization problem in TZDBTimeZoneNames.
authorYoshito Umaoka <y.umaoka@gmail.com>
Wed, 21 Jan 2015 19:51:06 +0000 (19:51 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Wed, 21 Jan 2015 19:51:06 +0000 (19:51 +0000)
X-SVN-Rev: 36977

icu4j/main/classes/core/src/com/ibm/icu/impl/TZDBTimeZoneNames.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeZoneFormatTest.java

index 22a8dd47f63db458be2fcded22ea76fc44023f46..4e5e9836db2d20c25d2044b90ea1c81a1bce4d15 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *******************************************************************************
- * Copyright (C) 2014, International Business Machines Corporation and
+ * Copyright (C) 2014-2015, International Business Machines Corporation and
  * others. All Rights Reserved.
  *******************************************************************************
  */
@@ -338,7 +338,7 @@ public class TZDBTimeZoneNames extends TimeZoneNames {
             synchronized(TZDBTimeZoneNames.class) {
                 if (TZDB_NAMES_TRIE == null) {
                     // loading all names into trie
-                    TZDB_NAMES_TRIE = new TextTrieMap<TZDBNameInfo>(true);
+                    TextTrieMap<TZDBNameInfo> trie = new TextTrieMap<TZDBNameInfo>(true);
                     Set<String> mzIDs = TimeZoneNamesImpl._getAvailableMetaZoneIDs();
                     for (String mzID : mzIDs) {
                         TZDBNames names = getMetaZoneNames(mzID);
@@ -363,7 +363,7 @@ public class TZDBTimeZoneNames extends TimeZoneNames {
                             stdInf.type = NameType.SHORT_STANDARD;
                             stdInf.ambiguousType = ambiguousType;
                             stdInf.parseRegions = parseRegions;
-                            TZDB_NAMES_TRIE.put(std, stdInf);
+                            trie.put(std, stdInf);
                         }
                         if (dst != null) {
                             TZDBNameInfo dstInf = new TZDBNameInfo();
@@ -371,9 +371,10 @@ public class TZDBTimeZoneNames extends TimeZoneNames {
                             dstInf.type = NameType.SHORT_DAYLIGHT;
                             dstInf.ambiguousType = ambiguousType;
                             dstInf.parseRegions = parseRegions;
-                            TZDB_NAMES_TRIE.put(dst, dstInf);
+                            trie.put(dst, dstInf);
                         }
                     }
+                    TZDB_NAMES_TRIE = trie;
                 }
             }
         }
index 2c9a5b77053e1187284152941380261868ae28f6..9f5d7aef17148cddc4d8eb4ba07f9230c0936061 100644 (file)
@@ -1,6 +1,6 @@
 /*
  ********************************************************************************
- * Copyright (C) 2007-2014, Google, International Business Machines Corporation *
+ * Copyright (C) 2007-2015, Google, International Business Machines Corporation *
  * and others. All Rights Reserved.                                             *
  ********************************************************************************
  */
@@ -9,6 +9,7 @@ package com.ibm.icu.dev.test.format;
 
 import java.text.ParseException;
 import java.text.ParsePosition;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.EnumSet;
@@ -16,8 +17,10 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.regex.Pattern;
 
+import com.ibm.icu.impl.TZDBTimeZoneNames;
 import com.ibm.icu.impl.ZoneMeta;
 import com.ibm.icu.lang.UCharacter;
 import com.ibm.icu.text.SimpleDateFormat;
@@ -26,6 +29,7 @@ import com.ibm.icu.text.TimeZoneFormat.ParseOption;
 import com.ibm.icu.text.TimeZoneFormat.Style;
 import com.ibm.icu.text.TimeZoneFormat.TimeType;
 import com.ibm.icu.text.TimeZoneNames;
+import com.ibm.icu.text.TimeZoneNames.NameType;
 import com.ibm.icu.util.BasicTimeZone;
 import com.ibm.icu.util.Calendar;
 import com.ibm.icu.util.Output;
@@ -977,4 +981,42 @@ public class TimeZoneFormatTest extends com.ibm.icu.dev.test.TestFmwk {
             }
         }
     }
+
+    // This is a test case of Ticket#11487.
+    // Because the problem is reproduced for the very first time,
+    // the reported problem cannot be reproduced with regular test
+    // execution. Run this test alone reproduced the problem before
+    // the fix was merged.
+    public void TestTZDBNamesThreading() {
+        final TZDBTimeZoneNames names = new TZDBTimeZoneNames(ULocale.ENGLISH);
+        final AtomicInteger found = new AtomicInteger();
+        List<Thread> threads = new ArrayList<Thread>();
+        final int numIteration = 1000;
+
+        try {
+            for (int i = 0; i < numIteration; i++) {
+                Thread thread = new Thread() {
+                    @Override
+                    public void run() {
+                        int resultSize = names.find("GMT", 0, EnumSet.allOf(NameType.class)).size();
+                        if (resultSize > 0) {
+                            found.incrementAndGet();
+                        }
+                    }
+                };
+                thread.start();
+                threads.add(thread);
+            }
+
+            for(Thread thread: threads) {
+                thread.join();
+            }
+        } catch (Throwable t) {
+            errln(t.toString());
+        }
+
+        if (found.intValue() != numIteration) {
+            errln("Incorrect count: " + found.toString() + ", expected: " + numIteration);
+        }
+    }
 }
\ No newline at end of file