It's not a secret anymore!*
The moose likes Threads and Synchronization and the fly likes LinkedBlockingDeque offerFirst Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "LinkedBlockingDeque offerFirst" Watch "LinkedBlockingDeque offerFirst" New topic
Author

LinkedBlockingDeque offerFirst

Gary Blomquist
Greenhorn

Joined: Dec 07, 2001
Posts: 14
Is it possible for offerFirst(E e) of LinkedBlockingDeque to block? The javadoc says:

"Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available."

The verbiage "if it is possible to do so immediately" seems to indicate that it can NOT block but I have a Thread that is calling this method that either dies or blocks indefinitely. The run loop is inside a try/catch block so the thread should not die. I am wondering if the thread might be seizing up on the call to offerFirst.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Look into source code:
http://www.docjar.com/html/api/java/util/concurrent/LinkedBlockingDeque.java.html

This is an implementation of offerFirst:


This is a declaration of 'lock' variable:


It seems that offerFirst blocks.
Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
Hey.

Thread that is calling this method that either dies or blocks indefinitely.


It is rather a strange situation. It could have been locked only during some longer traversal, for example:



Could you show more your LinkedBlockingDeque's client code?

Adam


SCJP, SCWCD, SCBCD, SCDJWS
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

Ireneusz Kordal wrote:
It seems that offerFirst blocks.


When the JavaDoc implies that it doesn't block, it is saying that there is no situation that can cause it to block indefinitely -- which is generally caused by a call to the condition variable await() method, because the queue is full.

The locks are just held long enough to do the task, and maintain thread safety.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

Adam Smolnik wrote:
It is rather a strange situation.

Could show more your LinkedBlockingDeque's client code?


Agreed. We need to see the client code -- as it is likely something else, and not being blocked while calling the offerFirst() method.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LinkedBlockingDeque offerFirst
 
Similar Threads
question on threads
Thread doubt
Thread's Beta Version (Dan)
Calling a java class from PLSQL!!
Q on thread at Jxam