• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

bounded vs unbounded

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I'm working on a project that is going to require a queue. When searching the java docs there are several to choose from:

java.util.concurrent.ArrayBlockingQueue<E>
java.util.concurrent.ConcurrentLinkedQueue<E>
java.util.concurrent.DelayQueue<E>
java.util.concurrent.LinkedBlockingQueue<E>
java.util.concurrent.PriorityBlockingQueue<E>
java.util.concurrent.SynchronousQueue<E>

Some of these are referred to as being "bounded" while others are "unbounded". I don't understand the meanings behind these terms, which makes it hard to make a choice as to which one is most suitable for my project. Could someone please explain, in laymans terms, what the difference is between a bounded queue and an unbounded queue?

PROJECT DESCRIPTION:

My project will involve several threads running, each of which will access and insert into the queue an EMessage object, which is essentially an email message with subject line (two Strings in other words). The queue will then release each EMessage, one at a time, to another class that will perform the actual processing of the email. Hope this helps. Perhaps with this description you can recommend the appropriate type queue class to use?

Alan
[ December 12, 2006: Message edited by: Alan Shiers ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Blocking Queue doc says it's optionally "capacity bounded" meaning that you can only put a specified number of items in the queue. If you try to put more, the put operation blocks until another thread takes something out and makes room. Exercise for the original poster: Which other ones said they were bounded? Does that definition fit them, too?
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following are those queue implementations that state they are bounded:

ArrayBlockingQueue<E>
LinkedBlockingQueue<E> (Optionally bounded)

So, I believe I understand now. Bounded simply means that the queue has a specific capacity that cannot be exceeded. Unbounded is one who capacity can expand, at least as far as memory consumption will permit.

So, I believe I would be looking for an unbounded queue. Since I'm not too concerned about the growing capacity of the queue, and since the queue will be accessed by multiple threads, my best choice is probably ConcurrentLinkedQueue.

Thanks,

Alan
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds cool. Have to admit I've never read up on ConcurrentLinkedQueue. Guess I oughtta go do that. Let us know how it works out!
 
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can an unbounded queue be a blocking queue ?

If so, how ?

As per my understanding, blocking queue means the offer/poll operations can be blocked if the queue is full/empty respectively.

which might mean, that poll operation will still hold as blocking operation for unbounded blocking queue.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhineet Kapil wrote:As per my understanding, blocking queue means the offer/poll operations can be blocked if the queue is full/empty respectively.

which might mean, that poll operation will still hold as blocking operation for unbounded blocking queue.


Not quite. The poll operation on a BlockingQueue returns a special value if the queue is empty. The take operation does block however. See the Javadoc for full details.
 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joanne,

Yes you are right. It is take() operation.

I used word 'poll' in the sense of data removal operation.

So, unbounded queues will have one way blocking ie removal operations (and not addition operations)

 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhineet Kapil wrote:which might mean, that poll operation will still hold as blocking operation for unbounded blocking queue.


And what would you have it do otherwise?

Joanne's basically explained it for you, but think about what "unbounded" means. The Queue can't return a value it doesn't have, so it needs some mechanism to say "I don't have a value for you".

Personally, I prefer blocking, because otherwise you usually need to write code which simply "buzzes" until it can provide a value - which is, logically, the same thing. And blocking is generally preferable (and less CPU-intensive) unless you have a specific reason to "buzz".

Or, of course, do something else.

Winston
 
On top of spaghetti all covered in cheese, there was this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic