Fix race conditions in LabelTypes map creation

Change-Id: I9e6d51f73bd664e82d9f54d127bbdd5bc46ab382
This commit is contained in:
Dave Borowitz 2014-01-02 15:16:56 -08:00
parent a72c23cee3
commit 9cad868a80

View File

@ -50,10 +50,15 @@ public class LabelTypes {
private Map<String, LabelType> byLabel() {
if (byLabel == null) {
byLabel = new HashMap<String, LabelType>();
if (labelTypes != null) {
for (LabelType t : labelTypes) {
byLabel.put(t.getName().toLowerCase(), t);
synchronized (this) {
if (byLabel == null) {
Map<String, LabelType> l = new HashMap<String, LabelType>();
if (labelTypes != null) {
for (LabelType t : labelTypes) {
l.put(t.getName().toLowerCase(), t);
}
}
byLabel = l;
}
}
}
@ -88,11 +93,16 @@ public class LabelTypes {
private Map<String, Integer> positions() {
if (positions == null) {
positions = new HashMap<String, Integer>();
if (labelTypes != null) {
int i = 0;
for (LabelType t : labelTypes) {
positions.put(t.getName(), i++);
synchronized (this) {
if (positions == null) {
Map<String, Integer> p = new HashMap<String, Integer>();
if (labelTypes != null) {
int i = 0;
for (LabelType t : labelTypes) {
p.put(t.getName(), i++);
}
}
positions = p;
}
}
}