Results 1 to 2 of 2
- 03-16-2014, 07:45 PM #1Senior Member
- Join Date
- Jul 2012
- Rep Power
First attempt at concurrent programming. i think I have reached deadlock.
I am writing a program which is a variation of the dining philosophers problem. I am solving it using just wait(), notify() and sleep().
I have managed to get it to work for 2 out of 6 of my philosophers. However the other 4 (apart from 1 who seems to love a particular fork) do nothing and just sit there even when their cutlery is available to use.
I have been over and over my code and, based on my understanding of concurrent programming in Java, I can't see anything wrong. I think there is something wrong with where I think I am instructing a piece of cutlery to be made available when it, in fact, is not being made available or maybe the other philosophers are just not checking.
Below is the code ran on each of the threads (each philosopher is a thread). Are there any obvious mistakes that I have made?
The idea of the code is that if the item of cutlery is available, it will be picked up, then the other piece will be picked up (This will be always be available when needed as I have specified to only ever go for knives if you have your allocated fork). The philosopher will eat (sleep) for a few seconds before setting both pieces to available and then thinking (sleeping). Whilst they are thinking, other philosophers have the occortunity to pick up the items - but none of them ever do!
Last edited by kkid; 03-16-2014 at 10:38 PM.
- 03-16-2014, 09:43 PM #2
- By voipp in forum Threads and SynchronizationReplies: 8Last Post: 12-15-2013, 09:32 PM
- By abdullahansari in forum New To JavaReplies: 1Last Post: 12-02-2010, 07:33 PM
- By tlouvierre in forum New To JavaReplies: 1Last Post: 05-31-2009, 06:48 PM