• Post Reply Bookmark Topic Watch Topic
  • New Topic

ConcurrentLinkedQueue pop question

 
Johan Haleby
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using a ConcurrentLinkedQueue in a multi-threaded application. If two or more threads invoke the poll method of my queue "at the same time", is it possible that they can retrieve the same object?

The reason I'm asking is because I've created a junit test which spawns lots of threads and it seems that sometimes this does happen. According to the java doc, poll() should remove the object from the queue upon invocation and return null if there's no object in the queue. So I'm not sure if it's a bug in my test class or if this is the behavior of the ConcurrentLinkedQueue. Does anyone know?

Thanks,
Johan
[ June 13, 2007: Message edited by: Johan Haleby ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say pop(), do you mean poll()? I'm pretty sure the behavior you describe should not be occurring - unless for some reason, the same object has been placed in the queue multiple times. A queue does not enforce uniqueness (unlike Set), so this is theorectically possible. It seems unlikely though unless you are doing it intentionally for some reason. Can you show the test code you're using?
 
Johan Haleby
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

Sorry, of course I mean poll() and not pop().

The same object should never be placed in the queue more than once, however I have not tested this so I'm not 100% sure if that is what's causing this behavior. I don't have code in front of me right now but I can post it later this evening.

Thanks for your reply.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ConcurrentLinkedQueue works just fine. The problem is with your code. (sorry about that)

Interestingly, when you remove() an object, it is not removed immediately; there is a delay in the physical removal.
 
Johan Haleby
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it wasn't anything wrong with my code. The problem was that my application went into a state that I did not thought was possible or legal, but when I gave it a bit more thought it was indeed a valid state.

Thanks for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!