Paper/src/main/java/org/bukkit/craftbukkit/scheduler/CraftThreadManager.java
Andrew Ardill 30d1a5d3fd returning if a thread is not alive should happen before any changes to
the list of workers, hence the return statement should be in the
synchronized block.
2011-02-21 16:32:41 +11:00

69 lines
1.9 KiB
Java

package org.bukkit.craftbukkit.scheduler;
import java.util.HashSet;
import java.util.Iterator;
import org.bukkit.plugin.Plugin;
public class CraftThreadManager {
final HashSet<CraftWorker> workers = new HashSet<CraftWorker>();
void executeTask(Runnable task, Plugin owner, int taskId) {
CraftWorker craftWorker = new CraftWorker(this, task, owner, taskId);
synchronized (workers) {
workers.add(craftWorker);
}
}
void interruptTask(int taskId) {
synchronized (workers) {
Iterator<CraftWorker> itr = workers.iterator();
while (itr.hasNext()) {
CraftWorker craftWorker = itr.next();
if (craftWorker.getTaskId() == taskId) {
craftWorker.interrupt();
}
}
}
}
void interruptTasks(Plugin owner) {
synchronized (workers) {
Iterator<CraftWorker> itr = workers.iterator();
while (itr.hasNext()) {
CraftWorker craftWorker = itr.next();
if (craftWorker.getOwner().equals(owner)) {
craftWorker.interrupt();
}
}
}
}
void interruptAllTasks() {
synchronized (workers) {
Iterator<CraftWorker> itr = workers.iterator();
while (itr.hasNext()) {
CraftWorker craftWorker = itr.next();
craftWorker.interrupt();
}
}
}
boolean isAlive(int taskId) {
synchronized (workers) {
Iterator<CraftWorker> itr = workers.iterator();
while (itr.hasNext()) {
CraftWorker craftWorker = itr.next();
if (craftWorker.getTaskId() == taskId) {
return craftWorker.isAlive();
}
}
// didn't find it, so it must have been removed
return false;
}
}
}