Adverts

When developing a concurrent system it is almost impossible to avoid using a queue of one sort or another. If two threads need to pass tasks to one another then the system is well modelled as a producer consumer problem and a queue is the easiest way to manage producers and consumers. The java.util.Queue interface was added to the core Java libraries with the release of Java 5 and is a specifically designed to be a collection that holds tasks for processing.

Being a collection means that a Queue has a number of operations in common with other collections such as contains and clear that perform the expected actions. Queues also have six additional methods add, offer, remove, poll, element and peek that are specific to submitting work to a queue for processing. These six operations can be split into three groups: offering work, collecting work and inspecting the queue. Each task has two methods, one which throws an exception and one that returns a special value such as null or false. This allows Queues to impose extra conditions that wouldn't otherwise be inforceable. The table below shows which methods do what:

Throws exception Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()

Adverts

Donate and Help

Please support this site and
Bandwidth doesn't grow on trees y' know :o)

Adverts

Get Adsense