This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Chopstick Program - Suggestions?  RSS feed

 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this program based off of the Sun tutorial on Threads. I didn't look at the code that their applet uses, so this is all from my own head, which leads me to this post
Since I'm starting to actually understand threads, I was wondering if anyone would mind commenting on the code - perhaps give a different/better way to approach it. I'm just extremely happy it works, but would love to hear feedback from people who've done a lot of multithreaded coding.
The println()'s are there to follow along with what is happening. Much like the tutorials applet, I find that if I increase the time that the thread sleeps, it becomes very sequential. If I make the time 0, it's all chaotic.

Thanks!
Jason
[This message has been edited by jason adam (edited December 18, 2001).]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some notes:
1) I don't think I understand the tail end of your isAvailable() method:

Why are you setting available to false but returing true?
2) Implement Runnable and instantiate a Thread with it, instead of extending Thread. It's more flexible (you can subclass something else), and it's closer to your code's intent.
3) notify() should be your default choice. notifyAll() does have its applications, but you're essentially saying "everybody wake up and let's see who can get the lock next!" That's a big potential difference in overhead versus letting the system's thread scheduler pick one for you.
Hope these help,
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is more a general programming issue than a thread issue, but isAvailable() seems to be an awkward method (as Michael noticed). Since it apparently always returns true, there isn't any need for a return value. Perhaps 'public void waitUntilAvailable()' would be a better declaration. The use of a temporary variable at the end is unnecessary since 'available' will always be true at that point.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The available thing is because two different threads both require the same chopstick object. If available is currently set to true, I have to have a way to tell that object that yes, you can have the stick, but then set available to false so that if the other thread tries to get the stick, it will have to wait. Since I can't set available to false after I return true (telling the first calling thread that the stick is available), I use a temporary boolean to hold the true value, set available to false, and then return true.
Yeah, it's kind of funky. I needed to figure a way so that the thread could test if both objects were available. If they were, it would print out whatever message, and then release the object. If a stick wasn't available, the thread would wait until it was.
Thanks for the pointers, I'm gonna play around with it more and see what I can come up with.
Jason
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, think this should make more sense. Thing I was forgetting is that if I call getStick(), and call wait in that method if the stick isn't available, the rest of the code in the threads run method isn't going to complete until the stick is available.

That look a little better?
Thanks!
Jason
[This message has been edited by jason adam (edited December 31, 2001).]
 
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
I am wondering what would happen if you change the order of chopstick while creating a Grabber object. Like instead of Chopstick(0 and 4) try making it Chopstick(4 and 0). Its prone to Deadlock situations.
Regards,
Vijay
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it's supposed to be susceptible to deadlocks. The more you increase the time that the thread sleeps, the less chance you have of a deadlock. It was the whole point of the tutorial that I started this program from
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!