ChunkTaskPriorityQueue.java

net.minecraft.server.level.ChunkTaskPriorityQueue

信息

  • 全限定名:net.minecraft.server.level.ChunkTaskPriorityQueue
  • 类型:public class
  • 包:net.minecraft.server.level
  • 源码路径:src/main/java/net/minecraft/server/level/ChunkTaskPriorityQueue.java
  • 起始行号:L10
  • 职责:

    TODO

字段/常量

  • PRIORITY_LEVEL_COUNT

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

      TODO

  • queuesPerPriority

    • 类型: List<Long2ObjectLinkedOpenHashMap<List<Runnable>>>
    • 修饰符: private final
    • 源码定位: L12
    • 说明:

      TODO

  • topPriorityQueueIndex

    • 类型: int
    • 修饰符: private volatile
    • 源码定位: L15
    • 说明:

      TODO

  • name

    • 类型: String
    • 修饰符: private final
    • 源码定位: L16
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.server.level.ChunkTaskPriorityQueue.TasksForChunk
    • 类型: record
    • 修饰符: public
    • 源码定位: L89
    • 说明:

      TODO

构造器

public ChunkTaskPriorityQueue(String name) @ L18

  • 构造器名:ChunkTaskPriorityQueue
  • 源码定位:L18
  • 修饰符:public

参数:

  • name: String

说明:

TODO

方法

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

protected void resortChunkTasks(int oldPriority, ChunkPos pos, int newPriority) @ L22

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

参数:

  • oldPriority: int
  • pos: ChunkPos
  • newPriority: int

说明:

TODO

protected void submit(Runnable task, long chunkPos, int level) @ L39

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

参数:

  • task: Runnable
  • chunkPos: long
  • level: int

说明:

TODO

protected void release(long pos, boolean unschedule) @ L44

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

参数:

  • pos: long
  • unschedule: boolean

说明:

TODO

public ChunkTaskPriorityQueue.TasksForChunk pop() @ L63

  • 方法名:pop
  • 源码定位:L63
  • 返回类型:ChunkTaskPriorityQueue.TasksForChunk
  • 修饰符:public

参数:

说明:

TODO

public boolean hasWork() @ L80

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

参数:

说明:

TODO

public String toString() @ L84

  • 方法名:toString
  • 源码定位:L84
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

代码

public class ChunkTaskPriorityQueue {
    public static final int PRIORITY_LEVEL_COUNT = ChunkLevel.MAX_LEVEL + 2;
    private final List<Long2ObjectLinkedOpenHashMap<List<Runnable>>> queuesPerPriority = IntStream.range(0, PRIORITY_LEVEL_COUNT)
        .mapToObj(priority -> new Long2ObjectLinkedOpenHashMap<List<Runnable>>())
        .toList();
    private volatile int topPriorityQueueIndex = PRIORITY_LEVEL_COUNT;
    private final String name;
 
    public ChunkTaskPriorityQueue(String name) {
        this.name = name;
    }
 
    protected void resortChunkTasks(int oldPriority, ChunkPos pos, int newPriority) {
        if (oldPriority < PRIORITY_LEVEL_COUNT) {
            Long2ObjectLinkedOpenHashMap<List<Runnable>> oldQueue = this.queuesPerPriority.get(oldPriority);
            List<Runnable> oldTasks = oldQueue.remove(pos.pack());
            if (oldPriority == this.topPriorityQueueIndex) {
                while (this.hasWork() && this.queuesPerPriority.get(this.topPriorityQueueIndex).isEmpty()) {
                    this.topPriorityQueueIndex++;
                }
            }
 
            if (oldTasks != null && !oldTasks.isEmpty()) {
                this.queuesPerPriority.get(newPriority).computeIfAbsent(pos.pack(), k -> Lists.newArrayList()).addAll(oldTasks);
                this.topPriorityQueueIndex = Math.min(this.topPriorityQueueIndex, newPriority);
            }
        }
    }
 
    protected void submit(Runnable task, long chunkPos, int level) {
        this.queuesPerPriority.get(level).computeIfAbsent(chunkPos, p -> Lists.newArrayList()).add(task);
        this.topPriorityQueueIndex = Math.min(this.topPriorityQueueIndex, level);
    }
 
    protected void release(long pos, boolean unschedule) {
        for (Long2ObjectLinkedOpenHashMap<List<Runnable>> queue : this.queuesPerPriority) {
            List<Runnable> tasks = queue.get(pos);
            if (tasks != null) {
                if (unschedule) {
                    tasks.clear();
                }
 
                if (tasks.isEmpty()) {
                    queue.remove(pos);
                }
            }
        }
 
        while (this.hasWork() && this.queuesPerPriority.get(this.topPriorityQueueIndex).isEmpty()) {
            this.topPriorityQueueIndex++;
        }
    }
 
    public ChunkTaskPriorityQueue.@Nullable TasksForChunk pop() {
        if (!this.hasWork()) {
            return null;
        } else {
            int index = this.topPriorityQueueIndex;
            Long2ObjectLinkedOpenHashMap<List<Runnable>> queue = this.queuesPerPriority.get(index);
            long chunkPos = queue.firstLongKey();
            List<Runnable> tasks = queue.removeFirst();
 
            while (this.hasWork() && this.queuesPerPriority.get(this.topPriorityQueueIndex).isEmpty()) {
                this.topPriorityQueueIndex++;
            }
 
            return new ChunkTaskPriorityQueue.TasksForChunk(chunkPos, tasks);
        }
    }
 
    public boolean hasWork() {
        return this.topPriorityQueueIndex < PRIORITY_LEVEL_COUNT;
    }
 
    @Override
    public String toString() {
        return this.name + " " + this.topPriorityQueueIndex + "...";
    }
 
    public record TasksForChunk(long chunkPos, List<Runnable> tasks) {
    }
}

引用的其他类