Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Queues - why "poll" and "offer"?

Tags:

java

naming

Ok, so I've been using Java for a long time now and have recently been preparing for my OCJP exam. I was wondering if anyone might be able to provide any insight into why the method names "poll" (as opposed to the more traditional "pop") and "offer" (as opposed to the more traditional "push") were chosen? I'm looking specifically at the java.util.Queue interface, but would be interested in more general explanations as well :)

This is really more of an academic question than for any specific coding scenario, as I'm just trying to make sense of why Sun (as this was done before Oracle bought them) would choose to the names that they did.

Oh and before anyone decides to crucify me or throw back links to lmgtfy... I've already looked on google, yahoo, wiki, bing, and SO so if I'm overlooking some obvious search criteria or missed some old post here that explains it then I apologize in advance.

like image 229
zpangwin Avatar asked Feb 18 '12 16:02

zpangwin


People also ask

Why do we use polls in Java?

poll() method in Java is used to retrieve or fetch and remove the first element of the Queue or the element present at the head of the Queue. The peek() method only retrieved the element at the head but the poll() also removes the element along with the retrieval. It returns NULL if the queue is empty.

What does poll do in queue in Java?

The poll() method of Queue Interface returns and removes the element at the front end of the container. It deletes the element in the container. The method does not throws an exception when the Queue is empty, it returns null instead.

What does offer () do in Java?

The java. util. PriorityQueue. offer() method is used to insert a particular element into the Priority Queue.

What is polling in queue?

In queueing theory, a discipline within the mathematical theory of probability, a polling system or polling model is a system where a single server visits a set of queues in some order. The model has applications in computer networks and telecommunications, manufacturing and road traffic management.


1 Answers

Because these methods have different semantics explained in the JavaDoc. add/remove are unconditional while offer/poll return special value:

  • offer only offers a new value, but it might not be accepted, e.g. if the queue is full

  • poll only polls for the value, but we accept the fact the value might not be there.

To complicate matters more, BlockingQueue introduces yet another pair of methods for blocking add/remove. Of course they could have used the same named with a bunch of parameters/flags,

smellyGet(boolean blocking, boolean failOnEmpty) 

but don't you think this is a better design?

        | Throws ex. | Special v. | Blocks | Times out --------+------------+------------+--------+--------------------- Insert  | add(e)     | offer(e)   | put(e) | offer(e, time, unit) Remove  | remove()   | poll()     | take() | poll(time, unit) Examine | element()  | peek()     | N/A    | N/A 

* https://meta.stackexchange.com/questions/73566

like image 159
Tomasz Nurkiewicz Avatar answered Sep 23 '22 03:09

Tomasz Nurkiewicz