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
ConstructorDescriptionHackedBlockingQueue
(int capacity) HackedBlockingQueue
(Collection<? extends E> c) -
Method Summary
Modifier and TypeMethodDescriptionstatic ThreadPoolExecutor
Defaults toRuntime.availableProcessors()
and a submission queue that is twice this number.static ThreadPoolExecutor
createExecutor
(int poolSize, int queueSize) Using an instance of this class, returns a fixed sizeThreadPoolExecutor
that blocksExecutorService.submit(Runnable)
when both the pool and the submission queue are full.boolean
boolean
poll()
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, toString
Methods inherited from class java.util.AbstractQueue
add, addAll, element, remove
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.concurrent.BlockingQueue
add
Methods 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:
offer
in interfaceBlockingQueue<E>
- Specified by:
offer
in interfaceQueue<E>
- Overrides:
offer
in classLinkedBlockingQueue<E>
-
offer
- Specified by:
offer
in interfaceBlockingQueue<E>
- Overrides:
offer
in classLinkedBlockingQueue<E>
- Throws:
InterruptedException
-
poll
- Specified by:
poll
in interfaceBlockingQueue<E>
- Overrides:
poll
in classLinkedBlockingQueue<E>
- Throws:
InterruptedException
-
poll
-
createExecutor
Using an instance of this class, returns a fixed sizeThreadPoolExecutor
that 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.
-