• Post Reply Bookmark Topic Watch Topic
  • New Topic

solution to the dining philosopher problem  RSS feed

 
Yan Zhou
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I have code a program that solves the dinning philosopher problem. But there is still room to improve.

My solution is to acquire both forks at any given time. If a philosopher first gets the left fork, and then tries to get the right fork, that may risk starvation.

The current solution locks on the list object (which contains all fork objects). Thus, only one philosopher can acquire two forks at any given time, and multiple philosophers can eat at the same time. Once a philosopher acquires the forks, they are removed from the list; and philosopher returns the forks to the list after he finished eating.

I would like to enhance the program so that multiple philosophers can get the forks at the same time. And I still want to ensure that a philosopher always get two forks at once. This is more difficult because I can no longer lock on the list object which contains forks since multiple phiolosophers must be allowed to remove/add forks to the list. I do not have an elegant solution for this.

Any suggestion?
Yan
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to take a look at http://www.xprogramming.com/xpmag/acsDiningPhilosophers.htm
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!