aspose file tools*
The moose likes Threads and Synchronization and the fly likes wait/notifyAll not behaving as I expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait/notifyAll not behaving as I expected" Watch "wait/notifyAll not behaving as I expected" New topic
Author

wait/notifyAll not behaving as I expected

Toby Eggitt
Ranch Hand

Joined: Dec 08, 2004
Posts: 41
Can anyone tell me what might be wrong with this code? It's supposed to be a single-element queue, with blocking put and get methods. It seems to work just fine "most of the time", but on Windows, with two consumers (and one producer) I find that one of the consumer threads, just sometimes, sleeps forever in the wait, apparently never waking up (even if the other consumer thread is off busy, and not waiting in the get() method).

Yes, I know that I should be using a BlockingQueue for this, but this is a learning exercise, and I'm puzzled how it can fail like this. What (probably obvious!) did I miss?

TIA,
Toby

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The answer MAY be in the code you posted, but I'm too lazy to try to figure out multi-threading code without a complete SSCCE. In particular, are all your producers and consumers using the SAME RunnableQueue?

Also, when you say, "one of the consumer threads, just sometimes, sleeps forever in the wait, apparently never waking up," how are you determining this, and why do you expect it to wake up?
Toby Eggitt
Ranch Hand

Joined: Dec 08, 2004
Posts: 41
Indeed, I was (kinda) hoping that I had done something clearly idiotic, but now it's looking like the problem is actually in other parts of the system. Most likely, it looks like the browsers (Chrome, FF, IE, etc.) that are creating the requests that are being packaged as Runnables and passed into a thread pool .. those browsers are doing some strange things opening sockets that then don't do anything (I'm certain that Chrome is doing that at least) and I think that those oddities are creating unexpected behavior elsewhere.

So, sorry to have bothered y'all,
Cheers,
Toby
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18904
    
    8

It did occur to me that calling "put(null)" would put a stick in the system's spokes; the code doesn't reject that if it's tried.
Toby Eggitt
Ranch Hand

Joined: Dec 08, 2004
Posts: 41
That's a fair observation! Not the problem I've been witnessing, but a very fair comment all the same. Thanks!
 
 
subject: wait/notifyAll not behaving as I expected