Package uk.ac.ebi.utils.threading
Class HackedBlockingQueue<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.LinkedBlockingQueue<E>
uk.ac.ebi.utils.threading.HackedBlockingQueue<E>
- All Implemented Interfaces:
Serializable,Iterable<E>,Collection<E>,BlockingQueue<E>,Queue<E>
Tricks the behaviour of LinkedBlockingQueue so that methods like offer(Object)
and poll() actually invoke LinkedBlockingQueue.put(Object) and LinkedBlockingQueue.take(), i.e., the queue always
waits for itself to be free-of/filled-with-some value/s.
This can be used in ThreadPoolExecutor, which of AbstractExecutorService.submit(Runnable)
methods can raise RejectedExecutionException by calling offer(). By passing this class to its
constructor, the executor will always wait for a free thread, either in the executor or in the task queue.
- Author:
- brandizi
- Date:
- 22 Dec 2017
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionHackedBlockingQueue(int capacity) HackedBlockingQueue(Collection<? extends E> c) -
Method Summary
Modifier and TypeMethodDescriptionstatic ThreadPoolExecutorDefaults toRuntime.availableProcessors()and a submission queue that is twice this number.static ThreadPoolExecutorcreateExecutor(int poolSize, int queueSize) Using an instance of this class, returns a fixed sizeThreadPoolExecutorthat blocksExecutorService.submit(Runnable)when both the pool and the submission queue are full.booleanbooleanpoll()Methods inherited from class java.util.concurrent.LinkedBlockingQueue
clear, contains, drainTo, drainTo, forEach, iterator, peek, put, remainingCapacity, remove, removeAll, removeIf, retainAll, size, spliterator, take, toArray, toArray, toStringMethods inherited from class java.util.AbstractQueue
add, addAll, element, removeMethods 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.concurrent.BlockingQueue
addMethods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, stream, toArray
-
Constructor Details
-
HackedBlockingQueue
public HackedBlockingQueue() -
HackedBlockingQueue
-
HackedBlockingQueue
public HackedBlockingQueue(int capacity)
-
-
Method Details
-
offer
- Specified by:
offerin interfaceBlockingQueue<E>- Specified by:
offerin interfaceQueue<E>- Overrides:
offerin classLinkedBlockingQueue<E>
-
offer
- Specified by:
offerin interfaceBlockingQueue<E>- Overrides:
offerin classLinkedBlockingQueue<E>- Throws:
InterruptedException
-
poll
- Specified by:
pollin interfaceBlockingQueue<E>- Overrides:
pollin classLinkedBlockingQueue<E>- Throws:
InterruptedException
-
poll
-
createExecutor
Using an instance of this class, returns a fixed sizeThreadPoolExecutorthat blocksExecutorService.submit(Runnable)when both the pool and the submission queue are full. -
createExecutor
Defaults toRuntime.availableProcessors()and a submission queue that is twice this number.
-