Run thread directly

This commit is contained in:
TheMode 2021-04-14 22:48:13 +02:00
parent 73cdfed5ce
commit fec36d4706
3 changed files with 24 additions and 23 deletions

View File

@ -120,12 +120,11 @@ public final class UpdateManager {
// Server tick (instance/chunk/entity)
// Synchronize with the update manager instance, like the signal for chunk load/unload
final CountDownLatch countDownLatch;
synchronized (this) {
this.threadProvider.prepareUpdate(tickStart);
countDownLatch = threadProvider.update(tickStart);
}
CountDownLatch countDownLatch = threadProvider.notifyThreads();
// Wait tick end
try {
countDownLatch.await();
@ -199,7 +198,7 @@ public final class UpdateManager {
* <p>
* WARNING: should be automatically done by the {@link Instance} implementation.
*
* @param chunk the loaded chunk
* @param chunk the loaded chunk
*/
public synchronized void signalChunkLoad(@NotNull Chunk chunk) {
if (this.threadProvider == null)
@ -212,7 +211,7 @@ public final class UpdateManager {
* <p>
* WARNING: should be automatically done by the {@link Instance} implementation.
*
* @param chunk the unloaded chunk
* @param chunk the unloaded chunk
*/
public synchronized void signalChunkUnload(@NotNull Chunk chunk) {
if (this.threadProvider == null)

View File

@ -33,10 +33,6 @@ public class BatchThread extends Thread {
return queue;
}
public void addRunnable(@NotNull Runnable runnable) {
this.runnable.queue.add(runnable);
}
public void shutdown() {
this.runnable.stop = true;
}
@ -91,8 +87,9 @@ public class BatchThread extends Thread {
}
}
public synchronized void startTick(@NotNull CountDownLatch countDownLatch) {
public synchronized void startTick(@NotNull CountDownLatch countDownLatch, Runnable runnable) {
synchronized (tickLock) {
this.queue.add(runnable);
this.countDownLatch = countDownLatch;
this.tickLock.notifyAll();
}

View File

@ -86,10 +86,22 @@ public abstract class ThreadProvider {
*
* @param time the tick time in milliseconds
*/
public void prepareUpdate(long time) {
this.threadChunkMap.forEach((threadId, chunkEntries) -> {
BatchThread thread = threads.get(threadId);
public @NotNull CountDownLatch update(long time) {
CountDownLatch countDownLatch = new CountDownLatch(threads.size());
for (BatchThread thread : threads) {
final int id = threads.indexOf(thread);
if (id == -1) {
countDownLatch.countDown();
continue;
}
final var chunkEntries = threadChunkMap.get(id);
if (chunkEntries == null) {
countDownLatch.countDown();
continue;
}
// Cache chunk entities
Map<Chunk, List<Entity>> chunkListMap = new HashMap<>(chunkEntries.size());
for (ChunkEntry chunkEntry : chunkEntries) {
var chunk = chunkEntry.chunk;
@ -97,7 +109,8 @@ public abstract class ThreadProvider {
chunkListMap.put(chunk, entities);
}
thread.addRunnable(() -> {
// Execute tick
thread.getMainRunnable().startTick(countDownLatch, () -> {
chunkListMap.forEach((chunk, entities) -> {
chunk.tick(time);
entities.forEach(entity -> {
@ -105,19 +118,11 @@ public abstract class ThreadProvider {
});
});
});
});
}
@NotNull
public CountDownLatch notifyThreads() {
CountDownLatch countDownLatch = new CountDownLatch(threads.size());
for (BatchThread thread : threads) {
final BatchThread.BatchRunnable runnable = thread.getMainRunnable();
runnable.startTick(countDownLatch);
}
return countDownLatch;
}
public void shutdown() {
this.threads.forEach(BatchThread::shutdown);
}