Skip to content

Commit 755a7eb

Browse files
authored
Refactor ticker task to use a thread-safe Set implementation for tickingLocations (#4173)
Fixes #3696 Fixes #4216
1 parent 11cea2d commit 755a7eb

File tree

1 file changed

+10
-2
lines changed
  • src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks

1 file changed

+10
-2
lines changed

Diff for: src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/TickerTask.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class TickerTask implements Runnable {
4444

4545
/**
4646
* This Map holds all currently actively ticking locations.
47+
* The value of this map (Set entries) MUST be thread-safe and mutable.
4748
*/
4849
private final Map<ChunkPosition, Set<Location>> tickingLocations = new ConcurrentHashMap<>();
4950

@@ -329,7 +330,7 @@ public Map<ChunkPosition, Set<Location>> getLocations() {
329330
public Set<Location> getLocations(@Nonnull Chunk chunk) {
330331
Validate.notNull(chunk, "The Chunk cannot be null!");
331332

332-
Set<Location> locations = tickingLocations.getOrDefault(new ChunkPosition(chunk), new HashSet<>());
333+
Set<Location> locations = tickingLocations.getOrDefault(new ChunkPosition(chunk), Collections.emptySet());
333334
return Collections.unmodifiableSet(locations);
334335
}
335336

@@ -343,7 +344,14 @@ public void enableTicker(@Nonnull Location l) {
343344
Validate.notNull(l, "Location cannot be null!");
344345

345346
ChunkPosition chunk = new ChunkPosition(l.getWorld(), l.getBlockX() >> 4, l.getBlockZ() >> 4);
346-
Set<Location> newValue = new HashSet<>();
347+
348+
/*
349+
Note that all the values in #tickingLocations must be thread-safe.
350+
Thus, the choice is between the CHM KeySet or a synchronized set.
351+
The CHM KeySet was chosen since it at least permits multiple concurrent
352+
reads without blocking.
353+
*/
354+
Set<Location> newValue = ConcurrentHashMap.newKeySet();
347355
Set<Location> oldValue = tickingLocations.putIfAbsent(chunk, newValue);
348356

349357
/**

0 commit comments

Comments
 (0)