AbstractConsecutiveExecutor .java

net.minecraft.util.thread.AbstractConsecutiveExecutor (class): members: runAll, profiledMetrics, hasWork, queue, Status, executor, schedule, status, size.

net.minecraft.util.threadclass138 linescommonextends Runnable>3.4 KB

Implements

API Surface (9)

void close()
void run()
void runAll()
void schedule(final T task)
int size()
boolean hasWork()
String toString()
String name()
List<MetricSampler> profiledMetrics()

Fields & Constants (5)

LOGGER
status
queue
executor
name

String literals (2)

"Could not schedule ConsecutiveExecutor"
"-queue-size"

Code Graph: Dependencies (7)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

AbstractConsecutiveExecutor.java138 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.thread;
2
3
import com.google.common.collect.ImmutableList;
4
import com.mojang.logging.LogUtils;
5
import java.util.List;
6
import java.util.concurrent.Executor;
7
import java.util.concurrent.RejectedExecutionException;
8
import java.util.concurrent.atomic.AtomicReference;
9
import net.minecraft.util.Util;
10
import net.minecraft.util.profiling.metrics.MetricCategory;
11
import net.minecraft.util.profiling.metrics.MetricSampler;
12
import net.minecraft.util.profiling.metrics.MetricsRegistry;
13
import net.minecraft.util.profiling.metrics.ProfilerMeasured;
14
import org.slf4j.Logger;
15
16
public abstract class AbstractConsecutiveExecutor<T extends Runnable> implements Runnable, TaskScheduler<T>, ProfilerMeasured {
17
	private static final Logger LOGGER = LogUtils.getLogger();
18
	private final AtomicReference<AbstractConsecutiveExecutor.Status> status = new AtomicReference<>(AbstractConsecutiveExecutor.Status.SLEEPING);
19
	private final StrictQueue<T> queue;
20
	private final Executor executor;
21
	private final String name;
22
23
	public AbstractConsecutiveExecutor(final StrictQueue<T> queue, final Executor executor, final String name) {
24
		this.executor = executor;
25
		this.queue = queue;
26
		this.name = name;
27
		MetricsRegistry.INSTANCE.add(this);
28
	}
29
30
	private boolean canBeScheduled() {
31
		return !this.isClosed() && !this.queue.isEmpty();
32
	}
33
34
	@Override
35
	public void close() {
36
		this.status.set(AbstractConsecutiveExecutor.Status.CLOSED);
37
	}
38
39
	private boolean pollTask() {
40
		if (!this.isRunning()) {
41
			return false;
42
		}
43
44
		Runnable runnable = this.queue.pop();
45
		if (runnable == null) {
46
			return false;
47
		}
48
49
		Util.runNamed(runnable, this.name);
50
		return true;
51
	}
52
53
	@Override
54
	public void run() {
55
		try {
56
			this.pollTask();
57
		} finally {
58
			this.setSleeping();
59
			this.registerForExecution();
60
		}
61
	}
62
63
	public void runAll() {
64
		try {
65
			while (this.pollTask()) {
66
			}
67
		} finally {
68
			this.setSleeping();
69
			this.registerForExecution();
70
		}
71
	}
72
73
	@Override
74
	public void schedule(final T task) {
75
		this.queue.push(task);
76
		this.registerForExecution();
77
	}
78
79
	private void registerForExecution() {
80
		if (this.canBeScheduled() && this.setRunning()) {
81
			try {
82
				this.executor.execute(this);
83
			} catch (RejectedExecutionException e) {
84
				try {
85
					this.executor.execute(this);
86
				} catch (RejectedExecutionException e2) {
87
					LOGGER.error("Could not schedule ConsecutiveExecutor", e2);
88
				}
89
			}
90
		}
91
	}
92
93
	public int size() {
94
		return this.queue.size();
95
	}
96
97
	public boolean hasWork() {
98
		return this.isRunning() && !this.queue.isEmpty();
99
	}
100
101
	@Override
102
	public String toString() {
103
		return this.name + " " + this.status.get() + " " + this.queue.isEmpty();
104
	}
105
106
	@Override
107
	public String name() {
108
		return this.name;
109
	}
110
111
	@Override
112
	public List<MetricSampler> profiledMetrics() {
113
		return ImmutableList.of(MetricSampler.create(this.name + "-queue-size", MetricCategory.CONSECUTIVE_EXECUTORS, this::size));
114
	}
115
116
	private boolean setRunning() {
117
		return this.status.compareAndSet(AbstractConsecutiveExecutor.Status.SLEEPING, AbstractConsecutiveExecutor.Status.RUNNING);
118
	}
119
120
	private void setSleeping() {
121
		this.status.compareAndSet(AbstractConsecutiveExecutor.Status.RUNNING, AbstractConsecutiveExecutor.Status.SLEEPING);
122
	}
123
124
	private boolean isRunning() {
125
		return this.status.get() == AbstractConsecutiveExecutor.Status.RUNNING;
126
	}
127
128
	private boolean isClosed() {
129
		return this.status.get() == AbstractConsecutiveExecutor.Status.CLOSED;
130
	}
131
132
	private enum Status {
133
		SLEEPING,
134
		RUNNING,
135
		CLOSED;
136
	}
137
}
138

Decompiled Minecraft 26.3 source of AbstractConsecutiveExecutor (net/minecraft/util/thread/AbstractConsecutiveExecutor.java) with Mojang official mappings, stored verbatim on the site.