This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread coma???

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic