|
|
@ -24,6 +24,8 @@ import java.util.Comparator; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.PriorityQueue; |
|
|
|
import java.util.PriorityQueue; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
import java.util.concurrent.locks.Condition; |
|
|
|
|
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Task instances priority queue implementation |
|
|
|
* 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()); |
|
|
|
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 |
|
|
|
* put task instance to priority queue |
|
|
|
* |
|
|
|
* |
|
|
@ -64,6 +76,9 @@ public class PeerTaskInstancePriorityQueue implements TaskPriorityQueue<TaskInst |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* poll task info with timeout |
|
|
|
* 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 timeout |
|
|
|
* @param unit |
|
|
|
* @param unit |
|
|
|
* @return |
|
|
|
* @return |
|
|
@ -72,10 +87,18 @@ public class PeerTaskInstancePriorityQueue implements TaskPriorityQueue<TaskInst |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public TaskInstance poll(long timeout, TimeUnit unit) throws TaskPriorityQueueException, InterruptedException { |
|
|
|
public TaskInstance poll(long timeout, TimeUnit unit) throws TaskPriorityQueueException, InterruptedException { |
|
|
|
if (queue.isEmpty()) { |
|
|
|
long nanos = unit.toNanos(timeout); |
|
|
|
unit.sleep(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; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|