From 09d0cf1441653dc95d7c84938c997484cce97a9a Mon Sep 17 00:00:00 2001 From: emilyy-dev <35617540+emilyy-dev@users.noreply.github.com> Date: Sun, 28 Nov 2021 20:06:50 -0300 Subject: [PATCH] synchronized entrySet/values creation --- .../adventure/text/format/DecorationMap.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/net/kyori/adventure/text/format/DecorationMap.java b/api/src/main/java/net/kyori/adventure/text/format/DecorationMap.java index ed0b9de6d6..81fa8ae2c7 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/DecorationMap.java +++ b/api/src/main/java/net/kyori/adventure/text/format/DecorationMap.java @@ -89,8 +89,8 @@ private static int offset(final TextDecoration decoration) { private final int bitSet; // lazy - private EntrySet entrySet = null; - private Values values = null; + private volatile EntrySet entrySet = null; + private volatile Values values = null; private DecorationMap(final int bitSet) { this.bitSet = bitSet; @@ -139,7 +139,12 @@ public boolean isEmpty() { @Override public @NotNull Set> entrySet() { if (this.entrySet == null) { - this.entrySet = new EntrySet(); + synchronized (this) { + // re-check for lost race condition + if (this.entrySet == null) { + this.entrySet = new EntrySet(); + } + } } return this.entrySet; } @@ -152,7 +157,12 @@ public boolean isEmpty() { @Override public @NotNull Collection values() { if (this.values == null) { - this.values = new Values(); + synchronized (this) { + // re-check for lost race condition + if (this.values == null) { + this.values = new Values(); + } + } } return this.values; }