public class HostLimitQueue extends java.util.concurrent.LinkedBlockingDeque<java.lang.Runnable>
This implementation doesn't guarantee to have at most hostLimit connections per host[1], and it doesn't
guarantee that all threads will be busy, when there is work for them[2].
[1] More connection per host may happen, when ThreadPoolExecutor is growing its pool, and thus
tasks do not go through the Queue
[2] If we have a queue, and for all hosts in queue we will fail to acquire semaphore, the thread
take the first available job and wait for semaphore. It might be the case, that semaphore was released
for some task further in queue, but this implementation doesn't try to detect such situation
Modifier and Type | Field and Description |
---|---|
private int |
corePoolSize |
private java.util.concurrent.ThreadPoolExecutor |
executor |
private int |
hostLimit |
private java.util.Map<java.lang.String,java.util.concurrent.Semaphore> |
hostSemaphores |
private int |
maximumPoolSize |
private static long |
serialVersionUID |
Constructor and Description |
---|
HostLimitQueue(int hostLimit)
Creates an unbounded queue
|
Modifier and Type | Method and Description |
---|---|
private void |
acquireSemaphore(java.lang.Runnable job) |
private JCSCachedTileLoaderJob<?,?> |
findJob() |
private java.util.concurrent.Semaphore |
getSemaphore(JCSCachedTileLoaderJob<?,?> job) |
boolean |
offer(java.lang.Runnable e) |
java.lang.Runnable |
poll(long timeout,
java.util.concurrent.TimeUnit unit) |
private void |
releaseSemaphore(JCSCachedTileLoaderJob<?,?> job) |
void |
setExecutor(java.util.concurrent.ThreadPoolExecutor executor)
Set the executor for which this queue works.
|
java.lang.Runnable |
take() |
private boolean |
tryAcquireSemaphore(JCSCachedTileLoaderJob<?,?> job) |
private boolean |
tryAcquireSemaphore(java.lang.Runnable job,
long timeout,
java.util.concurrent.TimeUnit unit) |
add, addFirst, addLast, clear, contains, descendingIterator, drainTo, drainTo, element, getFirst, getLast, iterator, offer, offerFirst, offerFirst, offerLast, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollFirst, pollLast, pollLast, pop, push, put, putFirst, putLast, remainingCapacity, remove, remove, removeFirst, removeFirstOccurrence, removeLast, removeLastOccurrence, size, spliterator, takeFirst, takeLast, toArray, toArray, toString
containsAll, isEmpty, removeAll, retainAll
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
private static final long serialVersionUID
private final java.util.Map<java.lang.String,java.util.concurrent.Semaphore> hostSemaphores
private final int hostLimit
private java.util.concurrent.ThreadPoolExecutor executor
private int corePoolSize
private int maximumPoolSize
public HostLimitQueue(int hostLimit)
hostLimit
- how many parallel calls to host to allowprivate JCSCachedTileLoaderJob<?,?> findJob()
public java.lang.Runnable poll(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
poll
in interface java.util.concurrent.BlockingDeque<java.lang.Runnable>
poll
in interface java.util.concurrent.BlockingQueue<java.lang.Runnable>
poll
in class java.util.concurrent.LinkedBlockingDeque<java.lang.Runnable>
java.lang.InterruptedException
public java.lang.Runnable take() throws java.lang.InterruptedException
take
in interface java.util.concurrent.BlockingDeque<java.lang.Runnable>
take
in interface java.util.concurrent.BlockingQueue<java.lang.Runnable>
take
in class java.util.concurrent.LinkedBlockingDeque<java.lang.Runnable>
java.lang.InterruptedException
public void setExecutor(java.util.concurrent.ThreadPoolExecutor executor)
executor
- executor for which this queue workspublic boolean offer(java.lang.Runnable e)
offer
in interface java.util.concurrent.BlockingDeque<java.lang.Runnable>
offer
in interface java.util.concurrent.BlockingQueue<java.lang.Runnable>
offer
in interface java.util.Deque<java.lang.Runnable>
offer
in interface java.util.Queue<java.lang.Runnable>
offer
in class java.util.concurrent.LinkedBlockingDeque<java.lang.Runnable>
private java.util.concurrent.Semaphore getSemaphore(JCSCachedTileLoaderJob<?,?> job)
private void acquireSemaphore(java.lang.Runnable job) throws java.lang.InterruptedException
java.lang.InterruptedException
private boolean tryAcquireSemaphore(JCSCachedTileLoaderJob<?,?> job)
private boolean tryAcquireSemaphore(java.lang.Runnable job, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
java.lang.InterruptedException
private void releaseSemaphore(JCSCachedTileLoaderJob<?,?> job)