diff --git a/core/src/main/java/com/boydti/fawe/example/MappedFaweQueue.java b/core/src/main/java/com/boydti/fawe/example/MappedFaweQueue.java index 356b6761..3a9e863d 100644 --- a/core/src/main/java/com/boydti/fawe/example/MappedFaweQueue.java +++ b/core/src/main/java/com/boydti/fawe/example/MappedFaweQueue.java @@ -31,7 +31,6 @@ public abstract class MappedFaweQueue extends FaweQueue { private WORLD impWorld; private IFaweQueueMap map; - public ArrayDeque tasks = new ArrayDeque<>(); public MappedFaweQueue(final World world) { this(world, null); @@ -93,12 +92,6 @@ public abstract class MappedFaweQueue extends FaweQueue { } - @Override - public void addNotifyTask(Runnable runnable) { - this.tasks.add(runnable); - size(); - } - public abstract WORLD getImpWorld(); public abstract boolean isChunkLoaded(WORLD world, int x, int z); @@ -210,6 +203,7 @@ public abstract class MappedFaweQueue extends FaweQueue { @Override public void runTasks() { + super.runTasks(); if (getProgressTask() != null) { getProgressTask().run(ProgressType.DONE, 1); } diff --git a/core/src/main/java/com/boydti/fawe/example/NMSMappedFaweQueue.java b/core/src/main/java/com/boydti/fawe/example/NMSMappedFaweQueue.java index 98e88b2a..73bed151 100644 --- a/core/src/main/java/com/boydti/fawe/example/NMSMappedFaweQueue.java +++ b/core/src/main/java/com/boydti/fawe/example/NMSMappedFaweQueue.java @@ -35,7 +35,7 @@ public abstract class NMSMappedFaweQueue ex } private void addRelightTask() { - tasks.add(new Runnable() { + addNotifyTask(new Runnable() { @Override public void run() { if (relighter != null) { diff --git a/core/src/main/java/com/boydti/fawe/object/FaweQueue.java b/core/src/main/java/com/boydti/fawe/object/FaweQueue.java index fc79512c..9d17e78a 100644 --- a/core/src/main/java/com/boydti/fawe/object/FaweQueue.java +++ b/core/src/main/java/com/boydti/fawe/object/FaweQueue.java @@ -10,7 +10,6 @@ import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.MemUtil; import com.boydti.fawe.util.SetQueue; -import com.boydti.fawe.util.TaskManager; import com.sk89q.jnbt.CompoundTag; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.Vector; @@ -26,7 +25,6 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ExecutorCompletionService; -import java.util.concurrent.atomic.AtomicBoolean; public abstract class FaweQueue { @@ -269,8 +267,6 @@ public abstract class FaweQueue { public abstract void addNotifyTask(int x, int z, Runnable runnable); - public abstract void addNotifyTask(Runnable runnable); - public boolean hasBlock(int x, int y, int z) throws FaweException.FaweChunkLoadException { return getCombinedId4Data(x, y, z) != 0; } @@ -355,6 +351,10 @@ public abstract class FaweQueue { public abstract int size(); + public boolean isEmpty() { + return size() == 0; + } + /** * Lock the thread until the queue is empty */ @@ -371,20 +371,47 @@ public abstract class FaweQueue { SetQueue.IMP.flush(this); } else { if (enqueue()) { - final AtomicBoolean running = new AtomicBoolean(true); - addNotifyTask(new Runnable() { - @Override - public void run() { - TaskManager.IMP.notify(running); + while (!isEmpty() && SetQueue.IMP.isStage(this, SetQueue.QueueStage.ACTIVE)) { + synchronized (this) { + try { + this.wait(time); + } catch (InterruptedException e) { + e.printStackTrace(); + } } - }); - TaskManager.IMP.wait(running, time); + } } } } } - public abstract void runTasks(); + public ConcurrentLinkedDeque tasks = new ConcurrentLinkedDeque<>(); + + public void addNotifyTask(Runnable runnable) { + this.tasks.add(runnable); + } + + + public void runTasks() { + synchronized (this) { + this.notifyAll(); + } + if (getProgressTask() != null) { + getProgressTask().run(ProgressType.DONE, 1); + } + while (!tasks.isEmpty()) { + Runnable task = tasks.poll(); + try { + task.run(); + } catch (Throwable e) { + MainUtil.handleError(e); + } + } + } + + public void addTask(Runnable whenFree) { + tasks.add(whenFree); + } public boolean enqueue() { return SetQueue.IMP.enqueue(this);