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.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop