Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - improve task sleeping #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/main/java/com/playmonumenta/structures/StructuresAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ public boolean equals(Object obj) {
}

private static final HashMap<WorldChunkKey, Integer> CHUNK_TICKET_REFERENCE_COUNT = new HashMap<>();
private static Deque<PendingTask> PENDING_TASKS = new ConcurrentLinkedDeque<>();
private static final LinkedBlockingDeque<PendingTask> PENDING_TASKS = new LinkedBlockingDeque<>();
private static @Nullable BukkitRunnable RUNNING_TASK = null;

private static final EnumSet<EntityType> keptEntities = EnumSet.of(
Expand Down Expand Up @@ -683,24 +683,21 @@ private static void ensureTask(Plugin plugin) {
@Override
public void run() {
while (true) {
PendingTask task = PENDING_TASKS.pollFirst();

if (task != null) {
try {
Bukkit.getScheduler().runTask(plugin, task.mStartTask);
task.mSignal.get(45, TimeUnit.SECONDS);
} catch (Exception ex) {
plugin.getLogger().severe("Structure task took longer than 45s to complete! Continuing to the next task. Exception: " + ex.getMessage());
}
}

PendingTask task;
try {
Thread.sleep(50);
} catch (Exception ex) {
task = PENDING_TASKS.take();
} catch (InterruptedException ignored) {
plugin.getLogger().info("Structure loading task sleep was interrupted");
RUNNING_TASK = null;
break;
}

try {
Bukkit.getScheduler().runTask(plugin, task.mStartTask);
task.mSignal.get(45, TimeUnit.SECONDS);
} catch (Exception ex) {
plugin.getLogger().severe("Structure task took longer than 45s to complete! Continuing to the next task. Exception: " + ex.getMessage());
}
}
}
};
Expand Down