aspose file tools*
The moose likes Threads and Synchronization and the fly likes LinkedBlockingQueue.take() ever timeout? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "LinkedBlockingQueue.take() ever timeout?" Watch "LinkedBlockingQueue.take() ever timeout?" New topic
Author

LinkedBlockingQueue.take() ever timeout?

Sam Yim
Greenhorn

Joined: Jul 25, 2009
Posts: 17
I'm interested in using LinkedBlockingQueue in a web application.
Part of the web flow, it retrieves data to be return to the client (browser) but before it does there is some computations that is done.

I want to return the data to the client without having to wait for the computation task to complete.
Since performance of the computation task is not important, I wanted to offload it to a Queue to get processed.

Wasn't sure how the LinkedBlockingQueue would behave in a web application environment.

If there is no traffic coming to the servlet, the Queue will be empty. Was curious to know if the take() method ever timeout if the Queue remains empty for a long period of time.


I was looking at the
source code and it's a bit difficult for me to follow.


Since the "consumer" code (that takes items out of the queue) will be running in a separate thread, I wasn't sure if the thread itself will die when idle and/or the take() method will timeout.

Any thoughts on this would be greatly appreciate?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Sam Yim wrote:I'm interested in using LinkedBlockingQueue in a web application.
Part of the web flow, it retrieves data to be return to the client (browser) but before it does there is some computations that is done.

I want to return the data to the client without having to wait for the computation task to complete.
Since performance of the computation task is not important, I wanted to offload it to a Queue to get processed.

Wasn't sure how the LinkedBlockingQueue would behave in a web application environment.

If there is no traffic coming to the servlet, the Queue will be empty. Was curious to know if the take() method ever timeout if the Queue remains empty for a long period of time.


I was looking at the
source code and it's a bit difficult for me to follow.


Since the "consumer" code (that takes items out of the queue) will be running in a separate thread, I wasn't sure if the thread itself will die when idle and/or the take() method will timeout.

Any thoughts on this would be greatly appreciate?



Won't the poll() method work here? It is just like the take method, except that one version will return if the queue is empty, and another version will wait but only for a specified amount of time.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sam Yim
Greenhorn

Joined: Jul 25, 2009
Posts: 17
That's the thing, I want the "consumer" part to keep waiting for something in the queue.

That's why I was interested in using take() based on the description. But I'm not sure what the behavior is when there is nothing in the queue for a long period of time (i.e. 8hours...)

I guess I'll just have to experiment with it.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Sam Yim wrote:That's the thing, I want the "consumer" part to keep waiting for something in the queue.

That's why I was interested in using take() based on the description. But I'm not sure what the behavior is when there is nothing in the queue for a long period of time (i.e. 8hours...)

I guess I'll just have to experiment with it.


There is no time out - not even 8 hours. There are three variations on getting things from the Queue:

1) poll(): Return immediately
2) poll(time, unit): Return after timeout
3) take(): Block continuously


Steve
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: LinkedBlockingQueue.take() ever timeout?