aspose file tools*
The moose likes Beginning Java and the fly likes LinkedBlockingQueue vs ArrayBlockingQueue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "LinkedBlockingQueue vs ArrayBlockingQueue" Watch "LinkedBlockingQueue vs ArrayBlockingQueue" New topic
Author

LinkedBlockingQueue vs ArrayBlockingQueue

Uttam Kini
Greenhorn

Joined: Aug 07, 2008
Posts: 17
I need to implement a FIFO queue for my application which is a static Object into which many threads put and poll .... I have decided to choose implementations of BlockingQueue because I can restrict the size of the queue and my thread access is synchronized ... which means threads will block if the queue remains full due to some other bottleneck in the application..... I am confused between using LinkedBlockingQueue and ArrayBlockingQueue ... From what I understand LinkedBlockingQueue expands dynamically and ArrayBlockingQueue allocates an array of a the specified size.... In the API doc of LinkedBlockingQueue, it says "Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications"...... This seems a little ambiguous to me .... Could some one please elaborate this for me ..... so that I can be clear on the choice of Queue for my application.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Uttam,
I will attempt to explain how I understand it..

i.e. The ArrayBlockingQueue is bounded with the capacity argument supplied when constructing it. If the Queue turns out to be short when rate of object adding gets high it will block and this may reduce the objects "passing through" the queue in a unit time.

Whereas in the LinkedBlockingQueue, bounding is optional and the highest capacity is Integer.MAX_VALUE and it can grow up to that value if no lower bound is specified. This should allow more objects to be added to the queue without blocking thereby increasing the throughput. But according to the API this seems to slow down when multiple threads access the LinkedBlockingQueue.

If you can estimate with reasonable accuracy the size of your queue then may be ArrayBlockingQueue is what you need. Othewise LinkedBlockingQueue may have to be it.

Can anybody else confirm?
Uttam Kini
Greenhorn

Joined: Aug 07, 2008
Posts: 17
Thanks for the reply Gamini.... In my application I want the bound of the queue to act as a throttle, so that the heap does not grow beyond it's limits.... So therefore if the queue is full... then the producer threads which want to put into it should wait for space to become available... Thats the reason I choose a BlockingQueue implementation..... If some other operations are taking time to complete,then the consumer threads will not consume from the queue and the producer threads must block until space becomes available.... I will decide on the bound for the queue based on the performance of my consumer threads .. I Hope that gives a bigger picture...
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
So the producer is in a different JVM in a remote system?
Uttam Kini
Greenhorn

Joined: Aug 07, 2008
Posts: 17
The producer lives in the same JVM
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
I see. So when the Queue is blocking the producer will block without creating any more new objects threby keeping a tab on memory usage?
Uttam Kini
Greenhorn

Joined: Aug 07, 2008
Posts: 17
Exactly.... And since I have control on the bound ... I can tune my application for optimum performance by varying the bound (trial and error).
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Uttam Kini:
In the API doc of LinkedBlockingQueue, it says "Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications"...... This seems a little ambiguous to me.


In which way does it seem to be ambiguous?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Uttam Kini
Greenhorn

Joined: Aug 07, 2008
Posts: 17
Less predictable performance is the ambiguous part..... predictable can mean 2 things to me :

1. which thread gets the first priority

2. the speed of insertions and removals is not the same and hence not predictable (since throughput is mentioned on top, this gives it more credibility)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
I'm pretty sure they mean #2. The first isn't about performance; it's about fairness.
Uttam Kini
Greenhorn

Joined: Aug 07, 2008
Posts: 17
I have decided that I will go ahead with the ArrayBlockingQueue since I can specify the bound initially...... If I get problems with it ... I might as well try the LinkedBlockingQueue...Since the APIs for both are the same, there would be minimal code change.... Thanks for the posts everybody
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Mike Simmons:
I'm pretty sure they mean #2. The first isn't about performance; it's about fairness.


+1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LinkedBlockingQueue vs ArrayBlockingQueue