counter thread provider should never update partitions

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-02-05 06:17:04 +01:00
parent 95fd6e7c81
commit 0934c8ef16

View File

@ -1,7 +1,5 @@
package net.minestom.server.thread; package net.minestom.server.thread;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -12,12 +10,11 @@ import java.util.concurrent.atomic.AtomicInteger;
public interface ThreadProvider<T> { public interface ThreadProvider<T> {
static <T> @NotNull ThreadProvider<T> counter() { static <T> @NotNull ThreadProvider<T> counter() {
return new ThreadProvider<>() { return new ThreadProvider<>() {
private final Cache<T, Integer> cache = Caffeine.newBuilder().weakKeys().build();
private final AtomicInteger counter = new AtomicInteger(); private final AtomicInteger counter = new AtomicInteger();
@Override @Override
public int findThread(@NotNull T partition) { public int findThread(@NotNull T partition) {
return cache.get(partition, i -> counter.getAndIncrement()); return counter.getAndIncrement();
} }
}; };
} }