aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread coma??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread coma???" Watch "Thread coma???" New topic
Author

Thread coma???

James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Hello Ladies/Gents,

I am working on a solution to the consumer/producer problem using a bounded buffer of random integers. I've got what looks to be a fairly solid solution with one exception: in main, I'm supposed to display the buffer contents to the screen periodically. What I decided to do was use an infinite loop with a 1000-ms sleep() each time after displaying the contents. The problem is that it sleeps and never wakes up again. Maybe someone sees something I don't and can point me in the right direction? All of the classes have been combined below for your convenience. As always, thanks in advance!



Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40



You have to be careful here. Just because a method doesn't modify the list, doesn't mean that it won't modify the internal structure, or is dependent on the fact that the structure won't change. You also need to synchronize the getValue() and getBufSize() methods too.

Henry


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

Joined: Sep 28, 2004
Posts: 18896
    
  40

BTW, since you can have calls from many threads that effect the count variable, you may need to protect that variable as well.

Henry
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Henry Wong wrote:

You have to be careful here. Just because a method doesn't modify the list, doesn't mean that it won't modify the internal structure, or is dependent on the fact that the structure won't change. You also need to synchronize the getValue() and getBufSize() methods too.

Henry



Hi Henry.

Thanks for the response. Yes, that seems to work. I realize now that after the getBufSize() and getValue() return their values, these values could change (or the consumer could remove the value at location i, crashing the program upon a getValue(i) call), or return the wrong value altogether. I knew, better too; I have your book here with me (most of my mistakes are just due to silliness).
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Well, you know what, it was actually another minor change in code that took away the permanent sleep condition, but thanks for pointing out the dangerous code.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Thread coma???