|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectparatask.queues.WorkStealingQueue<E>
E
- The type of elements held in this collectionpublic class WorkStealingQueue<E>
An implementation of a work-stealing queue. Elements are added and removed from the queue using a work-stealing policy.
Elements are added to a thread's local queue, and removed from
a thread's local queue using a last in first out (LIFO) policy. If no elements exist in the thread's local queue,
an element is stolen using a first in first out (FIFO) policy from another thread's local queue.
Consequently, the "head of the queue" in the context of the WorkStealingQueue refers to the
element according to this work-stealing schedule. In other words, "head of the queue" for the
thread when it takes from its own local queue refers to the same end where elements are added (i.e. the LIFO end).
Similarly, the "head of the queue" for a stealing thread refers to the opposite end of the
of the victim's queue (i.e. the FIFO end).
Constructor Summary | |
---|---|
WorkStealingQueue()
Create an empty WorkStealingQueue with maximum capacity and chunksize of 1. |
|
WorkStealingQueue(java.util.Collection<? extends E> c,
int chunksize)
Create a WorkStealingQueue that contains the specified collection of elements and specified chunksize. |
|
WorkStealingQueue(int capacity,
int chunksize)
Create an empty WorkStealingQueue with the specified capacity and chunksize. |
Method Summary | ||
---|---|---|
boolean |
add(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. |
|
boolean |
addAll(java.util.Collection<? extends E> c)
Adds all of the elements in the specified collection to this collection (optional operation). |
|
void |
clear()
Removes all of the elements from this collection (optional operation). |
|
boolean |
contains(java.lang.Object o)
Returns true if this queue contains the specified element. |
|
boolean |
containsAll(java.util.Collection<?> c)
Returns true if this collection contains all of the elements in the specified collection. |
|
int |
drainTo(java.util.Collection<? super E> c)
Removes all available elements from this queue and adds them to the given collection. |
|
int |
drainTo(java.util.Collection<? super E> c,
int maxElements)
Removes at most the given number of available elements from this queue and adds them to the given collection. |
|
E |
element()
Retrieves, but does not remove, the head of this queue. |
|
boolean |
isEmpty()
Returns true if this collection contains no elements. |
|
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in this collection. |
|
boolean |
offer(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. |
|
boolean |
offer(E e,
long timeout,
java.util.concurrent.TimeUnit unit)
Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available. |
|
E |
peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. |
|
E |
poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty. |
|
E |
poll(long timeout,
java.util.concurrent.TimeUnit unit)
Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available. |
|
void |
put(E e)
Inserts the specified element into this queue, waiting if necessary for space to become available. |
|
int |
remainingCapacity()
Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, or Integer.MAX_VALUE if there is no intrinsic limit. |
|
E |
remove()
Retrieves and removes the head of this queue. |
|
boolean |
remove(java.lang.Object o)
Removes a single instance of the specified element from this queue, if it is present. |
|
boolean |
removeAll(java.util.Collection<?> c)
Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
|
boolean |
retainAll(java.util.Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection (optional operation). |
|
int |
size()
Returns the number of elements in this collection. |
|
E |
take()
Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. |
|
java.lang.Object[] |
toArray()
Returns an array containing all of the elements in this collection. |
|
|
toArray(T[] a)
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface java.util.Collection |
---|
equals, hashCode |
Constructor Detail |
---|
public WorkStealingQueue()
WorkStealingQueue(int, int)
,
WorkStealingQueue(Collection, int)
public WorkStealingQueue(int capacity, int chunksize)
capacity
- The WorkStealingQueue's capacitychunksize
- The chunksize in case of stealsWorkStealingQueue()
,
WorkStealingQueue(Collection, int)
public WorkStealingQueue(java.util.Collection<? extends E> c, int chunksize)
c
- The collection of elements to place inside the WorkStealingQueuechunksize
- The chunksize in case of stealsMethod Detail |
---|
public boolean add(E e)
java.util.concurrent.BlockingQueue
offer
.
add
in interface java.util.Collection<E>
add
in interface java.util.concurrent.BlockingQueue<E>
add
in interface java.util.Queue<E>
e
- the element to add
Collection.add(E)
)public boolean contains(java.lang.Object o)
java.util.concurrent.BlockingQueue
contains
in interface java.util.Collection<E>
contains
in interface java.util.concurrent.BlockingQueue<E>
o
- object to be checked for containment in this queue
public int drainTo(java.util.Collection<? super E> c)
java.util.concurrent.BlockingQueue
drainTo
in interface java.util.concurrent.BlockingQueue<E>
c
- the collection to transfer elements into
public int drainTo(java.util.Collection<? super E> c, int maxElements)
java.util.concurrent.BlockingQueue
drainTo
in interface java.util.concurrent.BlockingQueue<E>
c
- the collection to transfer elements intomaxElements
- the maximum number of elements to transfer
public boolean offer(E e)
java.util.concurrent.BlockingQueue
BlockingQueue.add(E)
, which can fail to insert an
element only by throwing an exception.
offer
in interface java.util.concurrent.BlockingQueue<E>
offer
in interface java.util.Queue<E>
e
- the element to add
public boolean offer(E e, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
java.util.concurrent.BlockingQueue
offer
in interface java.util.concurrent.BlockingQueue<E>
e
- the element to addtimeout
- how long to wait before giving up, in units of
unitunit
- a TimeUnit determining how to interpret the
timeout parameter
java.lang.InterruptedException
- if interrupted while waitingpublic E poll(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
java.util.concurrent.BlockingQueue
poll
in interface java.util.concurrent.BlockingQueue<E>
timeout
- how long to wait before giving up, in units of
unitunit
- a TimeUnit determining how to interpret the
timeout parameter
java.lang.InterruptedException
- if interrupted while waitingpublic void put(E e) throws java.lang.InterruptedException
java.util.concurrent.BlockingQueue
put
in interface java.util.concurrent.BlockingQueue<E>
e
- the element to add
java.lang.InterruptedException
- if interrupted while waitingpublic int remainingCapacity()
java.util.concurrent.BlockingQueue
Note that you cannot always tell if an attempt to insert an element will succeed by inspecting remainingCapacity because it may be the case that another thread is about to insert or remove an element.
remainingCapacity
in interface java.util.concurrent.BlockingQueue<E>
public boolean remove(java.lang.Object o)
java.util.concurrent.BlockingQueue
remove
in interface java.util.Collection<E>
remove
in interface java.util.concurrent.BlockingQueue<E>
o
- element to be removed from this queue, if present
public E take() throws java.lang.InterruptedException
java.util.concurrent.BlockingQueue
take
in interface java.util.concurrent.BlockingQueue<E>
java.lang.InterruptedException
- if interrupted while waitingpublic E element()
java.util.Queue
peek
only in that it throws an exception
if this queue is empty.
element
in interface java.util.Queue<E>
public E peek()
java.util.Queue
peek
in interface java.util.Queue<E>
public E poll()
java.util.Queue
poll
in interface java.util.Queue<E>
public E remove()
java.util.Queue
poll
only in that it throws an exception if this
queue is empty.
remove
in interface java.util.Queue<E>
public boolean addAll(java.util.Collection<? extends E> c)
java.util.Collection
addAll
in interface java.util.Collection<E>
c
- collection containing elements to be added to this collection
Collection.add(Object)
public void clear()
java.util.Collection
clear
in interface java.util.Collection<E>
public boolean containsAll(java.util.Collection<?> c)
java.util.Collection
containsAll
in interface java.util.Collection<E>
c
- collection to be checked for containment in this collection
Collection.contains(Object)
public boolean isEmpty()
java.util.Collection
isEmpty
in interface java.util.Collection<E>
public java.util.Iterator<E> iterator()
java.util.Collection
iterator
in interface java.lang.Iterable<E>
iterator
in interface java.util.Collection<E>
public boolean removeAll(java.util.Collection<?> c)
java.util.Collection
removeAll
in interface java.util.Collection<E>
c
- collection containing elements to be removed from this collection
Collection.remove(Object)
,
Collection.contains(Object)
public boolean retainAll(java.util.Collection<?> c)
java.util.Collection
retainAll
in interface java.util.Collection<E>
c
- collection containing elements to be retained in this collection
Collection.remove(Object)
,
Collection.contains(Object)
public int size()
java.util.Collection
size
in interface java.util.Collection<E>
public java.lang.Object[] toArray()
java.util.Collection
The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
toArray
in interface java.util.Collection<E>
public <T> T[] toArray(T[] a)
java.util.Collection
If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)
If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
Like the Collection.toArray()
method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
Suppose x is a collection known to contain only strings. The following code can be used to dump the collection into a newly allocated array of String:
String[] y = x.toArray(new String[0]);Note that toArray(new Object[0]) is identical in function to toArray().
toArray
in interface java.util.Collection<E>
a
- the array into which the elements of this collection are to be
stored, if it is big enough; otherwise, a new array of the same
runtime type is allocated for this purpose.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |