Class HostLimitQueue
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<E>
-
- java.util.concurrent.LinkedBlockingDeque<java.lang.Runnable>
-
- org.openstreetmap.josm.data.cache.HostLimitQueue
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<java.lang.Runnable>
,java.util.Collection<java.lang.Runnable>
,java.util.concurrent.BlockingDeque<java.lang.Runnable>
,java.util.concurrent.BlockingQueue<java.lang.Runnable>
,java.util.Deque<java.lang.Runnable>
,java.util.Queue<java.lang.Runnable>
public class HostLimitQueue extends java.util.concurrent.LinkedBlockingDeque<java.lang.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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private int
hostLimit
private java.util.Map<java.lang.String,java.util.concurrent.Semaphore>
hostSemaphores
private static long
serialVersionUID
-
Constructor Summary
Constructors Constructor Description HostLimitQueue(int hostLimit)
Creates an unbounded queueHostLimitQueue(int hostLimit, int queueLimit)
Creates bounded queue
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
acquireSemaphore(java.lang.Runnable job)
private JCSCachedTileLoaderJob<?,?>
findJob()
private java.util.concurrent.Semaphore
getSemaphore(JCSCachedTileLoaderJob<?,?> job)
java.lang.Runnable
poll(long timeout, java.util.concurrent.TimeUnit unit)
private void
releaseSemaphore(JCSCachedTileLoaderJob<?,?> job)
java.lang.Runnable
take()
private boolean
tryAcquireSemaphore(java.lang.Runnable job, long timeout, java.util.concurrent.TimeUnit unit)
private boolean
tryAcquireSemaphore(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, toString
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
hostSemaphores
private final java.util.Map<java.lang.String,java.util.concurrent.Semaphore> hostSemaphores
-
hostLimit
private final int hostLimit
-
-
Constructor Detail
-
HostLimitQueue
public HostLimitQueue(int hostLimit)
Creates an unbounded queue- Parameters:
hostLimit
- how many parallel calls to host to allow
-
HostLimitQueue
public HostLimitQueue(int hostLimit, int queueLimit)
Creates bounded queue- Parameters:
hostLimit
- how many parallel calls to host to allowqueueLimit
- how deep the queue should be
-
-
Method Detail
-
findJob
private JCSCachedTileLoaderJob<?,?> findJob()
-
poll
public java.lang.Runnable poll(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
- Specified by:
poll
in interfacejava.util.concurrent.BlockingDeque<java.lang.Runnable>
- Specified by:
poll
in interfacejava.util.concurrent.BlockingQueue<java.lang.Runnable>
- Overrides:
poll
in classjava.util.concurrent.LinkedBlockingDeque<java.lang.Runnable>
- Throws:
java.lang.InterruptedException
-
take
public java.lang.Runnable take() throws java.lang.InterruptedException
- Specified by:
take
in interfacejava.util.concurrent.BlockingDeque<java.lang.Runnable>
- Specified by:
take
in interfacejava.util.concurrent.BlockingQueue<java.lang.Runnable>
- Overrides:
take
in classjava.util.concurrent.LinkedBlockingDeque<java.lang.Runnable>
- Throws:
java.lang.InterruptedException
-
getSemaphore
private java.util.concurrent.Semaphore getSemaphore(JCSCachedTileLoaderJob<?,?> job)
-
acquireSemaphore
private void acquireSemaphore(java.lang.Runnable job) throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
tryAcquireSemaphore
private boolean tryAcquireSemaphore(JCSCachedTileLoaderJob<?,?> job)
-
tryAcquireSemaphore
private boolean tryAcquireSemaphore(java.lang.Runnable job, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
releaseSemaphore
private void releaseSemaphore(JCSCachedTileLoaderJob<?,?> job)
-
-