Separate PerChunkThreadProvider from PerInstanceThreadProvider, support arbitrary ThreadProvider#findThread number

This commit is contained in:
TheMode 2021-04-18 01:55:35 +02:00
parent f0607f776a
commit 7dd6aa2360
5 changed files with 31 additions and 8 deletions

View File

@ -39,8 +39,8 @@ public final class UpdateManager {
{
// DEFAULT THREAD PROVIDER
//threadProvider = new PerGroupChunkProvider();
threadProvider = new PerInstanceThreadProvider(4);
//threadProvider = new PerChunkThreadProvider(4);
}
/**

View File

@ -327,6 +327,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
packet.process(this);
}
//System.out.println(getAcquiredElement().getHandler().getBatchThread());
Collection<Acquirable<Player>> players = new ArrayList<>();
//if (username.equals("TheMode911"))
for (Player p1 : MinecraftServer.getConnectionManager().getOnlinePlayers()) {

View File

@ -0,0 +1,20 @@
package net.minestom.server.thread;
import net.minestom.server.instance.Chunk;
import net.minestom.server.utils.chunk.ChunkUtils;
import org.jetbrains.annotations.NotNull;
/**
* Each {@link Chunk} gets assigned to a random thread.
*/
public class PerChunkThreadProvider extends ThreadProvider {
public PerChunkThreadProvider(int threadCount) {
super(threadCount);
}
@Override
public long findThread(@NotNull Chunk chunk) {
return ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
}
}

View File

@ -1,10 +1,12 @@
package net.minestom.server.thread;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ThreadLocalRandom;
/**
* Each {@link Instance} gets assigned to a random thread.
*/
public class PerInstanceThreadProvider extends ThreadProvider {
public PerInstanceThreadProvider(int threadCount) {
@ -12,7 +14,7 @@ public class PerInstanceThreadProvider extends ThreadProvider {
}
@Override
public int findThread(@NotNull Chunk chunk) {
return ThreadLocalRandom.current().nextInt(getThreads().size());
public long findThread(@NotNull Chunk chunk) {
return System.identityHashCode(chunk.getInstance());
}
}
}

View File

@ -57,10 +57,10 @@ public abstract class ThreadProvider {
*
* @param chunk the chunk
*/
public abstract int findThread(@NotNull Chunk chunk);
public abstract long findThread(@NotNull Chunk chunk);
protected void addChunk(Chunk chunk) {
int threadId = findThread(chunk);
final int threadId = (int) (Math.abs(findThread(chunk)) % threads.size());
BatchThread thread = threads.get(threadId);
var chunks = threadChunkMap.computeIfAbsent(thread, batchThread -> ConcurrentHashMap.newKeySet());