mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-29 19:41:47 +01:00
Improve thread provider for chunk & instance
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
767c743e01
commit
bf5c5aa479
@ -1,14 +1,34 @@
|
||||
package net.minestom.server.thread;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import net.minestom.server.instance.Chunk;
|
||||
import net.minestom.server.instance.Instance;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@FunctionalInterface
|
||||
@ApiStatus.Experimental
|
||||
public interface ThreadProvider {
|
||||
ThreadProvider PER_CHUNk = Object::hashCode;
|
||||
ThreadProvider PER_INSTANCE = chunk -> chunk.getInstance().hashCode();
|
||||
ThreadProvider PER_CHUNk = new ThreadProvider() {
|
||||
private final AtomicInteger counter = new AtomicInteger();
|
||||
|
||||
@Override
|
||||
public int findThread(@NotNull Chunk chunk) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
};
|
||||
ThreadProvider PER_INSTANCE = new ThreadProvider() {
|
||||
private final Cache<Instance, Integer> cache = Caffeine.newBuilder().weakKeys().build();
|
||||
private final AtomicInteger counter = new AtomicInteger();
|
||||
|
||||
@Override
|
||||
public int findThread(@NotNull Chunk chunk) {
|
||||
return cache.get(chunk.getInstance(), i -> counter.getAndIncrement());
|
||||
}
|
||||
};
|
||||
ThreadProvider SINGLE = chunk -> 0;
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user