ChunkTaskDispatcher.java

net.minecraft.server.level.ChunkTaskDispatcher

信息

  • 全限定名:net.minecraft.server.level.ChunkTaskDispatcher
  • 类型:public class
  • 包:net.minecraft.server.level
  • 源码路径:src/main/java/net/minecraft/server/level/ChunkTaskDispatcher.java
  • 起始行号:L17
  • 实现:ChunkHolder.LevelChangeListener, AutoCloseable
  • 职责:

    TODO

字段/常量

  • DISPATCHER_PRIORITY_COUNT

    • 类型: int
    • 修饰符: public static final
    • 源码定位: L18
    • 说明:

      TODO

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L19
    • 说明:

      TODO

  • queue

    • 类型: ChunkTaskPriorityQueue
    • 修饰符: private final
    • 源码定位: L20
    • 说明:

      TODO

  • executor

    • 类型: TaskScheduler<Runnable>
    • 修饰符: private final
    • 源码定位: L21
    • 说明:

      TODO

  • dispatcher

    • 类型: PriorityConsecutiveExecutor
    • 修饰符: private final
    • 源码定位: L22
    • 说明:

      TODO

  • sleeping

    • 类型: boolean
    • 修饰符: protected
    • 源码定位: L23
    • 说明:

      TODO

内部类/嵌套类型

构造器

public ChunkTaskDispatcher(TaskScheduler<Runnable> executor, Executor dispatcherExecutor) @ L25

  • 构造器名:ChunkTaskDispatcher
  • 源码定位:L25
  • 修饰符:public

参数:

  • executor: TaskScheduler
  • dispatcherExecutor: Executor

说明:

TODO

方法

下面的方法块按源码顺序生成。

public boolean hasWork() @ L32

  • 方法名:hasWork
  • 源码定位:L32
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public void onLevelChange(ChunkPos pos, IntSupplier oldLevel, int newLevel, IntConsumer setQueueLevel) @ L36

  • 方法名:onLevelChange
  • 源码定位:L36
  • 返回类型:void
  • 修饰符:public

参数:

  • pos: ChunkPos
  • oldLevel: IntSupplier
  • newLevel: int
  • setQueueLevel: IntConsumer

说明:

TODO

public void release(long pos, Runnable whenReleased, boolean clearQueue) @ L49

  • 方法名:release
  • 源码定位:L49
  • 返回类型:void
  • 修饰符:public

参数:

  • pos: long
  • whenReleased: Runnable
  • clearQueue: boolean

说明:

TODO

public void submit(Runnable task, long pos, IntSupplier level) @ L62

  • 方法名:submit
  • 源码定位:L62
  • 返回类型:void
  • 修饰符:public

参数:

  • task: Runnable
  • pos: long
  • level: IntSupplier

说明:

TODO

protected void pollTask() @ L77

  • 方法名:pollTask
  • 源码定位:L77
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

protected void scheduleForExecution(ChunkTaskPriorityQueue.TasksForChunk tasksForChunk) @ L88

  • 方法名:scheduleForExecution
  • 源码定位:L88
  • 返回类型:void
  • 修饰符:protected

参数:

  • tasksForChunk: ChunkTaskPriorityQueue.TasksForChunk

说明:

TODO

protected void onRelease(long key) @ L95

  • 方法名:onRelease
  • 源码定位:L95
  • 返回类型:void
  • 修饰符:protected

参数:

  • key: long

说明:

TODO

protected ChunkTaskPriorityQueue.TasksForChunk popTasks() @ L98

  • 方法名:popTasks
  • 源码定位:L98
  • 返回类型:ChunkTaskPriorityQueue.TasksForChunk
  • 修饰符:protected

参数:

说明:

TODO

public void close() @ L102

  • 方法名:close
  • 源码定位:L102
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

代码

public class ChunkTaskDispatcher implements ChunkHolder.LevelChangeListener, AutoCloseable {
    public static final int DISPATCHER_PRIORITY_COUNT = 4;
    private static final Logger LOGGER = LogUtils.getLogger();
    private final ChunkTaskPriorityQueue queue;
    private final TaskScheduler<Runnable> executor;
    private final PriorityConsecutiveExecutor dispatcher;
    protected boolean sleeping;
 
    public ChunkTaskDispatcher(TaskScheduler<Runnable> executor, Executor dispatcherExecutor) {
        this.queue = new ChunkTaskPriorityQueue(executor.name() + "_queue");
        this.executor = executor;
        this.dispatcher = new PriorityConsecutiveExecutor(4, dispatcherExecutor, "dispatcher");
        this.sleeping = true;
    }
 
    public boolean hasWork() {
        return this.dispatcher.hasWork() || this.queue.hasWork();
    }
 
    @Override
    public void onLevelChange(ChunkPos pos, IntSupplier oldLevel, int newLevel, IntConsumer setQueueLevel) {
        this.dispatcher.schedule(new StrictQueue.RunnableWithPriority(0, () -> {
            int oldTicketLevel = oldLevel.getAsInt();
            if (SharedConstants.DEBUG_VERBOSE_SERVER_EVENTS) {
                LOGGER.debug("RES {} {} -> {}", pos, oldTicketLevel, newLevel);
            }
 
            this.queue.resortChunkTasks(oldTicketLevel, pos, newLevel);
            setQueueLevel.accept(newLevel);
        }));
    }
 
    public void release(long pos, Runnable whenReleased, boolean clearQueue) {
        this.dispatcher.schedule(new StrictQueue.RunnableWithPriority(1, () -> {
            this.queue.release(pos, clearQueue);
            this.onRelease(pos);
            if (this.sleeping) {
                this.sleeping = false;
                this.pollTask();
            }
 
            whenReleased.run();
        }));
    }
 
    public void submit(Runnable task, long pos, IntSupplier level) {
        this.dispatcher.schedule(new StrictQueue.RunnableWithPriority(2, () -> {
            int ticketLevel = level.getAsInt();
            if (SharedConstants.DEBUG_VERBOSE_SERVER_EVENTS) {
                LOGGER.debug("SUB {} {} {} {}", ChunkPos.unpack(pos), ticketLevel, this.executor, this.queue);
            }
 
            this.queue.submit(task, pos, ticketLevel);
            if (this.sleeping) {
                this.sleeping = false;
                this.pollTask();
            }
        }));
    }
 
    protected void pollTask() {
        this.dispatcher.schedule(new StrictQueue.RunnableWithPriority(3, () -> {
            ChunkTaskPriorityQueue.TasksForChunk tasksForChunk = this.popTasks();
            if (tasksForChunk == null) {
                this.sleeping = true;
            } else {
                this.scheduleForExecution(tasksForChunk);
            }
        }));
    }
 
    protected void scheduleForExecution(ChunkTaskPriorityQueue.TasksForChunk tasksForChunk) {
        CompletableFuture.allOf(tasksForChunk.tasks().stream().map(message -> this.executor.scheduleWithResult(future -> {
            message.run();
            future.complete(Unit.INSTANCE);
        })).toArray(CompletableFuture[]::new)).thenAccept(r -> this.pollTask());
    }
 
    protected void onRelease(long key) {
    }
 
    protected ChunkTaskPriorityQueue.@Nullable TasksForChunk popTasks() {
        return this.queue.pop();
    }
 
    @Override
    public void close() {
        this.executor.close();
    }
}

引用的其他类

  • ChunkHolder

    • 引用位置: 实现
  • ChunkTaskPriorityQueue

    • 引用位置: 参数/字段/构造调用/返回值
    • 关联成员: ChunkTaskPriorityQueue()
  • PriorityConsecutiveExecutor

    • 引用位置: 字段/构造调用
    • 关联成员: PriorityConsecutiveExecutor()
  • StrictQueue

    • 引用位置: 方法调用/构造调用
    • 关联成员: RunnableWithPriority(), StrictQueue.RunnableWithPriority()
  • TaskScheduler

    • 引用位置: 参数/字段
  • ChunkPos

    • 引用位置: 参数/方法调用
    • 关联成员: ChunkPos.unpack()