- Queue
- It
is an interface and extends Collection interface
- Most
used implementations are Blocking Queue, Priority Queue & Linked
List
- Blocking Queue
- Are
thread safe
- Blocks
the thread which tries to dequeue elements from an empty queue OR a
thread which tries to insert elements in queue which is already full
- Threads
are blocked until an element is either inserted or removed
respectively
- Do
not accept NULL values
- Used
to implement producer/consumer based application
- If
operation is not possible immediately then (refer below table)
|
Throws
Exception |
Returns Special
Value usually (true/false) |
Blocks method call |
Method
call blocks & waits till Time Out |
Insert |
add(o) |
offer(o) |
put(o) |
offer(o, timeout, timeunit) |
Remove |
remove(o) |
poll() |
take() |
poll(timeout, timeunit) |
Examine |
element() |
peek() |
|
|
- Priority Queue
- Elements
are inserted according to some priority order (not FIFO)
- Either
default natural sorting order or customized sorting order can be
implemented
- Insertion
order not preserved
- Duplicates
are not allowed
- Heterogeneous
objects allowed ONLY for customized order, ONLY homogeneous for natural
sorting order
- NULL
elements NOT allowed
- Constructors
- Default:
With initial capacity 11 and with natural sorting order
- int
initialCapacity = Creates with predefined size = initialCapacity
- int
initialCapacity, Comparator c: Creates with predefined size =
initialCapacity and comparator for customized sorting
- SortedSet
s: convert SortedSet to PriorityQueue
- Collection
c: convert Collection to PriorityQueue
- Some
Operating Systems won’t provide proper support for thread priorities and
PriorityQueue
- Linked List
- Implements
List -> Collection
- Insertion
order preserved, Allows: Duplicates, Heterogeneous objects & null
- By
default LinkedList creates Empty list, memory allocation is done at
runtime
- Constructors
- Default
- Collections
C = convert any collection to LinkedList
- Highly
recommended when there are frequent random insert or deletes
- Not
recommended when there are frequent retrieval operations as it needs
rearranging the indexes
- LinkedList
provides a few methods like addFirst, addLast, RemoveFirst, removeLast,
getFirst & getLast to support Stack & Queue based implementation
using LinkedList
- Version
1.5 onwards LinkedList also implements Queue interface, such LinkedList
implementation will always follow FIFO order
No comments:
Post a Comment