|
|
|
@ -24,6 +24,8 @@ import java.util.Comparator;
|
|
|
|
|
import java.util.Iterator; |
|
|
|
|
import java.util.PriorityQueue; |
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
import java.util.concurrent.locks.Condition; |
|
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Task instances priority queue implementation |
|
|
|
@ -40,6 +42,16 @@ public class PeerTaskInstancePriorityQueue implements TaskPriorityQueue<TaskInst
|
|
|
|
|
*/ |
|
|
|
|
private PriorityQueue<TaskInstance> queue = new PriorityQueue<>(QUEUE_MAX_SIZE, new TaskInfoComparator()); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Lock used for all public operations |
|
|
|
|
*/ |
|
|
|
|
private final ReentrantLock lock = new ReentrantLock(true); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Condition for blocking when empty |
|
|
|
|
*/ |
|
|
|
|
private final Condition notEmpty = lock.newCondition(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* put task instance to priority queue |
|
|
|
|
* |
|
|
|
@ -64,6 +76,9 @@ public class PeerTaskInstancePriorityQueue implements TaskPriorityQueue<TaskInst
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* poll task info with timeout |
|
|
|
|
* WARN: Please use PriorityBlockingQueue if you want to use poll(timeout, unit) |
|
|
|
|
* because this method of override interface used without considering accuracy of timeout |
|
|
|
|
* |
|
|
|
|
* @param timeout |
|
|
|
|
* @param unit |
|
|
|
|
* @return |
|
|
|
@ -72,10 +87,18 @@ public class PeerTaskInstancePriorityQueue implements TaskPriorityQueue<TaskInst
|
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public TaskInstance poll(long timeout, TimeUnit unit) throws TaskPriorityQueueException, InterruptedException { |
|
|
|
|
if (queue.isEmpty()) { |
|
|
|
|
unit.sleep(timeout); |
|
|
|
|
long nanos = unit.toNanos(timeout); |
|
|
|
|
final ReentrantLock lock = this.lock; |
|
|
|
|
lock.lockInterruptibly(); |
|
|
|
|
TaskInstance result; |
|
|
|
|
try { |
|
|
|
|
while ((result = queue.poll()) == null && nanos > 0) { |
|
|
|
|
nanos--; |
|
|
|
|
} |
|
|
|
|
} finally { |
|
|
|
|
lock.unlock(); |
|
|
|
|
} |
|
|
|
|
return queue.poll(); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|