8287860: Revise usage of volatile in j.u.Locale

Reviewed-by: naoto
This commit is contained in:
Sergey Tsypanov 2022-06-07 11:46:06 +00:00 committed by Naoto Sato
parent bde7a7ae03
commit 4fe0ca9ec3
2 changed files with 24 additions and 19 deletions

View file

@ -50,6 +50,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.spi.LocaleNameProvider; import java.util.spi.LocaleNameProvider;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import jdk.internal.vm.annotation.Stable;
import sun.security.action.GetPropertyAction; import sun.security.action.GetPropertyAction;
import sun.util.locale.BaseLocale; import sun.util.locale.BaseLocale;
import sun.util.locale.InternalLocaleBuilder; import sun.util.locale.InternalLocaleBuilder;
@ -681,7 +683,7 @@ public final class Locale implements Cloneable, Serializable {
/** /**
* Map to hold country codes for each ISO3166 part. * Map to hold country codes for each ISO3166 part.
*/ */
private static Map<IsoCountryCode, Set<String>> iso3166CodesMap = new ConcurrentHashMap<>(); private static final Map<IsoCountryCode, Set<String>> iso3166CodesMap = new ConcurrentHashMap<>();
/** /**
* This method is called from Locale class to retrieve country code set * This method is called from Locale class to retrieve country code set
@ -1079,17 +1081,18 @@ public final class Locale implements Cloneable, Serializable {
private static Locale initDefault(Locale.Category category) { private static Locale initDefault(Locale.Category category) {
Properties props = GetPropertyAction.privilegedGetProperties(); Properties props = GetPropertyAction.privilegedGetProperties();
Locale locale = Locale.defaultLocale;
return getInstance( return getInstance(
props.getProperty(category.languageKey, props.getProperty(category.languageKey,
defaultLocale.getLanguage()), locale.getLanguage()),
props.getProperty(category.scriptKey, props.getProperty(category.scriptKey,
defaultLocale.getScript()), locale.getScript()),
props.getProperty(category.countryKey, props.getProperty(category.countryKey,
defaultLocale.getCountry()), locale.getCountry()),
props.getProperty(category.variantKey, props.getProperty(category.variantKey,
defaultLocale.getVariant()), locale.getVariant()),
getDefaultExtensions(props.getProperty(category.extensionsKey, "")) getDefaultExtensions(props.getProperty(category.extensionsKey, ""))
.orElse(defaultLocale.getLocaleExtensions())); .orElse(locale.getLocaleExtensions()));
} }
private static Optional<LocaleExtensions> getDefaultExtensions(String extensionsProp) { private static Optional<LocaleExtensions> getDefaultExtensions(String extensionsProp) {
@ -1265,11 +1268,12 @@ public final class Locale implements Cloneable, Serializable {
* @return An array of ISO 639 two-letter language codes. * @return An array of ISO 639 two-letter language codes.
*/ */
public static String[] getISOLanguages() { public static String[] getISOLanguages() {
if (isoLanguages == null) { String[] languages = Locale.isoLanguages;
isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable); if (languages == null) {
Locale.isoLanguages = languages = getISO2Table(LocaleISOData.isoLanguageTable);
} }
String[] result = new String[isoLanguages.length]; String[] result = new String[languages.length];
System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length); System.arraycopy(languages, 0, result, 0, languages.length);
return result; return result;
} }
@ -1608,8 +1612,9 @@ public final class Locale implements Cloneable, Serializable {
* @since 1.7 * @since 1.7
*/ */
public String toLanguageTag() { public String toLanguageTag() {
if (languageTag != null) { String lTag = this.languageTag;
return languageTag; if (lTag != null) {
return lTag;
} }
LanguageTag tag = LanguageTag.parseLocale(baseLocale, localeExtensions); LanguageTag tag = LanguageTag.parseLocale(baseLocale, localeExtensions);
@ -1657,11 +1662,11 @@ public final class Locale implements Cloneable, Serializable {
String langTag = buf.toString(); String langTag = buf.toString();
synchronized (this) { synchronized (this) {
if (languageTag == null) { if (this.languageTag == null) {
languageTag = langTag; this.languageTag = langTag;
} }
} }
return languageTag; return langTag;
} }
/** /**
@ -2257,7 +2262,7 @@ public final class Locale implements Cloneable, Serializable {
/** /**
* Calculated hashcode * Calculated hashcode
*/ */
private transient volatile int hashCodeValue; private transient @Stable int hashCodeValue;
private static volatile Locale defaultLocale = initDefault(); private static volatile Locale defaultLocale = initDefault();
private static volatile Locale defaultDisplayLocale; private static volatile Locale defaultDisplayLocale;
@ -3110,7 +3115,7 @@ public final class Locale implements Cloneable, Serializable {
private final String range; private final String range;
private final double weight; private final double weight;
private volatile int hash; private @Stable int hash;
/** /**
* Constructs a {@code LanguageRange} using the given {@code range}. * Constructs a {@code LanguageRange} using the given {@code range}.

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -97,7 +97,7 @@ public final class BaseLocale {
private final String region; private final String region;
private final String variant; private final String variant;
private volatile int hash; private @Stable int hash;
/** /**
* Boolean for the old ISO language code compatibility. * Boolean for the old ISO language code compatibility.