Class HostLimitQueue
- All Implemented Interfaces:
Serializable,Iterable<Runnable>,Collection<Runnable>,BlockingDeque<Runnable>,BlockingQueue<Runnable>,Deque<Runnable>,Queue<Runnable>
Queue for ThreadPoolExecutor that implements per-host limit. It will acquire a semaphore for each task
and it will set a runnable task with semaphore release, when job has finished.
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
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intprivate static final long -
Constructor Summary
ConstructorsConstructorDescriptionHostLimitQueue(int hostLimit) Creates an unbounded queueHostLimitQueue(int hostLimit, int queueLimit) Creates bounded queue -
Method Summary
Modifier and TypeMethodDescriptionprivate voidacquireSemaphore(Runnable job) private JCSCachedTileLoaderJob<?,?> findJob()private SemaphoregetSemaphore(JCSCachedTileLoaderJob<?, ?> job) private voidreleaseSemaphore(JCSCachedTileLoaderJob<?, ?> job) take()private booleantryAcquireSemaphore(Runnable job, long timeout, TimeUnit unit) private booleantryAcquireSemaphore(JCSCachedTileLoaderJob<?, ?> job) Methods inherited from class java.util.concurrent.LinkedBlockingDeque
add, addAll, addFirst, addLast, clear, contains, descendingIterator, drainTo, drainTo, element, forEach, getFirst, getLast, iterator, offer, offer, offerFirst, offerFirst, offerLast, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollFirst, pollLast, pollLast, pop, push, put, putFirst, putLast, remainingCapacity, remove, remove, removeAll, removeFirst, removeFirstOccurrence, removeIf, removeLast, removeLastOccurrence, retainAll, size, spliterator, takeFirst, takeLast, toArray, toArray, toStringMethods inherited from class java.util.AbstractCollection
containsAll, isEmptyMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
containsAll, equals, hashCode, isEmpty, parallelStream, stream, toArray
-
Field Details
-
serialVersionUID
- See Also:
-
hostSemaphores
-
hostLimit
-
-
Constructor Details
-
HostLimitQueue
Creates an unbounded queue- Parameters:
hostLimit- how many parallel calls to host to allow
-
HostLimitQueue
Creates bounded queue- Parameters:
hostLimit- how many parallel calls to host to allowqueueLimit- how deep the queue should be
-
-
Method Details
-
findJob
-
poll
- Specified by:
pollin interfaceBlockingDeque<Runnable>- Specified by:
pollin interfaceBlockingQueue<Runnable>- Overrides:
pollin classLinkedBlockingDeque<Runnable>- Throws:
InterruptedException
-
take
- Specified by:
takein interfaceBlockingDeque<Runnable>- Specified by:
takein interfaceBlockingQueue<Runnable>- Overrides:
takein classLinkedBlockingDeque<Runnable>- Throws:
InterruptedException
-
getSemaphore
-
acquireSemaphore
- Throws:
InterruptedException
-
tryAcquireSemaphore
-
tryAcquireSemaphore
private boolean tryAcquireSemaphore(Runnable job, long timeout, TimeUnit unit) throws InterruptedException - Throws:
InterruptedException
-
releaseSemaphore
-