AbstractConsecutiveExecutor.java

net.minecraft.util.thread.AbstractConsecutiveExecutor

信息

  • 全限定名:net.minecraft.util.thread.AbstractConsecutiveExecutor
  • 类型:public abstract class
  • 包:net.minecraft.util.thread
  • 源码路径:src/main/java/net/minecraft/util/thread/AbstractConsecutiveExecutor.java
  • 起始行号:L16
  • 实现:Runnable, TaskScheduler, ProfilerMeasured
  • 职责:

    TODO

字段/常量

  • LOGGER

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

      TODO

  • status

    • 类型: AtomicReference<AbstractConsecutiveExecutor.Status>
    • 修饰符: private final
    • 源码定位: L18
    • 说明:

      TODO

  • queue

    • 类型: StrictQueue<T>
    • 修饰符: private final
    • 源码定位: L19
    • 说明:

      TODO

  • executor

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

      TODO

  • name

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

      TODO

内部类/嵌套类型

  • net.minecraft.util.thread.AbstractConsecutiveExecutor.Status
    • 类型: enum
    • 修饰符: private static
    • 源码定位: L132
    • 说明:

      TODO

构造器

public AbstractConsecutiveExecutor(StrictQueue<T> queue, Executor executor, String name) @ L23

  • 构造器名:AbstractConsecutiveExecutor
  • 源码定位:L23
  • 修饰符:public

参数:

  • queue: StrictQueue
  • executor: Executor
  • name: String

说明:

TODO

方法

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

private boolean canBeScheduled() @ L30

  • 方法名:canBeScheduled
  • 源码定位:L30
  • 返回类型:boolean
  • 修饰符:private

参数:

说明:

TODO

public void close() @ L34

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

参数:

说明:

TODO

private boolean pollTask() @ L39

  • 方法名:pollTask
  • 源码定位:L39
  • 返回类型:boolean
  • 修饰符:private

参数:

说明:

TODO

public void run() @ L53

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

参数:

说明:

TODO

public void runAll() @ L63

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

参数:

说明:

TODO

public void schedule(T task) @ L73

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

参数:

  • task: T

说明:

TODO

private void registerForExecution() @ L79

  • 方法名:registerForExecution
  • 源码定位:L79
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

public int size() @ L93

  • 方法名:size
  • 源码定位:L93
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public boolean hasWork() @ L97

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

参数:

说明:

TODO

public String toString() @ L101

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

参数:

说明:

TODO

public String name() @ L106

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

参数:

说明:

TODO

public List<MetricSampler> profiledMetrics() @ L111

  • 方法名:profiledMetrics
  • 源码定位:L111
  • 返回类型:List
  • 修饰符:public

参数:

说明:

TODO

private boolean setRunning() @ L116

  • 方法名:setRunning
  • 源码定位:L116
  • 返回类型:boolean
  • 修饰符:private

参数:

说明:

TODO

private void setSleeping() @ L120

  • 方法名:setSleeping
  • 源码定位:L120
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

private boolean isRunning() @ L124

  • 方法名:isRunning
  • 源码定位:L124
  • 返回类型:boolean
  • 修饰符:private

参数:

说明:

TODO

private boolean isClosed() @ L128

  • 方法名:isClosed
  • 源码定位:L128
  • 返回类型:boolean
  • 修饰符:private

参数:

说明:

TODO

代码

public abstract class AbstractConsecutiveExecutor<T extends Runnable> implements Runnable, TaskScheduler<T>, ProfilerMeasured {
    private static final Logger LOGGER = LogUtils.getLogger();
    private final AtomicReference<AbstractConsecutiveExecutor.Status> status = new AtomicReference<>(AbstractConsecutiveExecutor.Status.SLEEPING);
    private final StrictQueue<T> queue;
    private final Executor executor;
    private final String name;
 
    public AbstractConsecutiveExecutor(StrictQueue<T> queue, Executor executor, String name) {
        this.executor = executor;
        this.queue = queue;
        this.name = name;
        MetricsRegistry.INSTANCE.add(this);
    }
 
    private boolean canBeScheduled() {
        return !this.isClosed() && !this.queue.isEmpty();
    }
 
    @Override
    public void close() {
        this.status.set(AbstractConsecutiveExecutor.Status.CLOSED);
    }
 
    private boolean pollTask() {
        if (!this.isRunning()) {
            return false;
        } else {
            Runnable runnable = this.queue.pop();
            if (runnable == null) {
                return false;
            } else {
                Util.runNamed(runnable, this.name);
                return true;
            }
        }
    }
 
    @Override
    public void run() {
        try {
            this.pollTask();
        } finally {
            this.setSleeping();
            this.registerForExecution();
        }
    }
 
    public void runAll() {
        try {
            while (this.pollTask()) {
            }
        } finally {
            this.setSleeping();
            this.registerForExecution();
        }
    }
 
    @Override
    public void schedule(T task) {
        this.queue.push(task);
        this.registerForExecution();
    }
 
    private void registerForExecution() {
        if (this.canBeScheduled() && this.setRunning()) {
            try {
                this.executor.execute(this);
            } catch (RejectedExecutionException var4) {
                try {
                    this.executor.execute(this);
                } catch (RejectedExecutionException var3) {
                    LOGGER.error("Could not schedule ConsecutiveExecutor", (Throwable)var3);
                }
            }
        }
    }
 
    public int size() {
        return this.queue.size();
    }
 
    public boolean hasWork() {
        return this.isRunning() && !this.queue.isEmpty();
    }
 
    @Override
    public String toString() {
        return this.name + " " + this.status.get() + " " + this.queue.isEmpty();
    }
 
    @Override
    public String name() {
        return this.name;
    }
 
    @Override
    public List<MetricSampler> profiledMetrics() {
        return ImmutableList.of(MetricSampler.create(this.name + "-queue-size", MetricCategory.CONSECUTIVE_EXECUTORS, this::size));
    }
 
    private boolean setRunning() {
        return this.status.compareAndSet(AbstractConsecutiveExecutor.Status.SLEEPING, AbstractConsecutiveExecutor.Status.RUNNING);
    }
 
    private void setSleeping() {
        this.status.compareAndSet(AbstractConsecutiveExecutor.Status.RUNNING, AbstractConsecutiveExecutor.Status.SLEEPING);
    }
 
    private boolean isRunning() {
        return this.status.get() == AbstractConsecutiveExecutor.Status.RUNNING;
    }
 
    private boolean isClosed() {
        return this.status.get() == AbstractConsecutiveExecutor.Status.CLOSED;
    }
 
    private static enum Status {
        SLEEPING,
        RUNNING,
        CLOSED;
    }
}

引用的其他类