retrieve from something external, and also consumers that process what ever retrieved. Since there no knowing in general how quickly either process happen. In between we need a Queue of objects waiting to be processed. Java.util.Concurrent adds blocking queue. Queues are extensions to the collections API which means you can use the generics mechanism to state what type of object you put into them. Here are the basic methods common to any blocking queue. Where a non block queue was has methods that throw exception if the queue is too empty or too full, a block queue has methods that wait until the situation is resolved. Blocking queue operation may be considered atomic from the point of view of threads.
|void||put(O)||Adds a element to the queue, or waits for space.|
|boolean||offer(O)||Adds a element to the queue, if there is space or returns false.|
|boolean||offer(O, long timeout, TimeUnit unit)||Adds a element to the queue, or wait so long for space then returns false.|
|Object||take()||Take an element from the head of the queue, or waits for one to be there|
|boolean||poll(long timeout, TimeUnit unit)||Adds a element to the queue, or waits the given time for space.|
You can also measure fullness of the queue with remainCapacity() or copy all the element to another collection with drainTo().
Standard Queue backed by LinkedLists or Arrays are both available as our.
|ArrayBlockingQueue||Backed by any Array|
|LinkedListBlockingQueueBacked by a linked list|
|DelayQueue||each element .getDelay(), the take method take the element whose .getDelay() method has fallen further below zero, or wait until then|
|PriorityBlockingQueue||The priority, the first element to be returned is order by a Comparator (or Natural ordering, alphabetical for String etc)|
|SychronousQueue||Stores nothing, waits until both a take and a put thread are blocking at the same time.|