From cfff855cdf5019a8a45cf950edde4e0de375a18e Mon Sep 17 00:00:00 2001 From: "jia.shao.peng" Date: Tue, 27 Sep 2011 07:56:21 +0000 Subject: [PATCH] JAVA: Fix a small threading issue with PhoneNumberUtil.java Review URL: http://codereview.appspot.com/5129044 git-svn-id: http://libphonenumber.googlecode.com/svn/trunk@359 ee073f10-1060-11df-b6a4-87a95322a99c --- java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index acca382..2778231 100644 --- a/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -318,7 +318,8 @@ public class PhoneNumberUtil { private static PhoneNumberUtil instance = null; // A mapping from a region code to the PhoneMetadata for that region. - private Map regionToMetadataMap = new HashMap(); + private final Map regionToMetadataMap = + Collections.synchronizedMap(new HashMap()); // A cache for frequently used region-specific regular expressions. // As most people use phone numbers primarily from one to two countries, and there are roughly 60 @@ -1629,8 +1630,10 @@ public class PhoneNumberUtil { if (!isValidRegionCode(regionCode)) { return null; } - if (!regionToMetadataMap.containsKey(regionCode)) { - loadMetadataForRegionFromFile(currentFilePrefix, regionCode); + synchronized (regionToMetadataMap) { + if (!regionToMetadataMap.containsKey(regionCode)) { + loadMetadataForRegionFromFile(currentFilePrefix, regionCode); + } } return regionToMetadataMap.get(regionCode); } -- 2.7.4