
March 17th, 2008, 11:40 AM
|
|
Contributing User
|
|
Join Date: Jun 2007
Posts: 128
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 2
|
|
|
Dining Philosopher.....FUN!
Hello all. So I'm working on this program and I'm stuck. The user enters how many philosophers there should be (1-10) and there are only 50 meals available to be eaten. It currently runs over the number of meals eaten. I tried synchronizing a few different things without any luck.
Any help would be much appreciated.
Code:
import java.io.*;
import java.util.*;
class philosopher extends Thread {
private static int TIMES_2_EAT = 50;
private static int times_eaten = 0;
private static int num_philosophers = 0;
private static int sleep_multiplier = 5;
private static boolean DEBUG = false;
// supports a maximum of 10 philosophers
private static int [] chopsticks = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
private int phil_number;
Random generator = new Random();
public philosopher( int n ) {
phil_number = n;
num_philosophers++;
}
private void eat() {
int time_2_eat = 1 + generator.nextInt(10);
try {
sleep(time_2_eat*sleep_multiplier);
} catch ( InterruptedException e) {};
}
private void think() {
int time_2_think = 2 + generator.nextInt(20);
try {
sleep(time_2_think*sleep_multiplier);
} catch ( InterruptedException e) {};
}
private void get_Chopsticks ( int p_num ) {
synchronized(chopsticks) {
while (chopsticks[p_num] >= 0) {
try {
if (DEBUG) System.out.println(" Phil number " + p_num + " waiting for chopstick");
chopsticks.wait();
} catch (InterruptedException e) {};
}
chopsticks[p_num] = p_num;
if (DEBUG) System.out.println("Philosopher " + p_num + " picked up " +
p_num + " chopstick.");
while (chopsticks[(p_num+1) % num_philosophers] >= 0) {
try {
if (DEBUG) System.out.println(" Phil number " + p_num + " waiting for chopstick");
chopsticks.wait();
} catch (InterruptedException e) {};
}
chopsticks[(p_num+1) % num_philosophers] = p_num;
if (DEBUG) System.out.println("Philosopher " + p_num + " picked up " +
(p_num+1)%num_philosophers + " chopstick.");
times_eaten++;
} // end sync section
}
private void replace_chopsticks (int p_num) {
synchronized(chopsticks){
chopsticks[p_num] = -1;
chopsticks[(p_num+1) % num_philosophers] = -1;
if (DEBUG) System.out.println("Philosopher " + p_num + " put down chopsticks.");
chopsticks.notifyAll();
} // end synch section
}
public void run() {
int times_I_have_eaten = 0;
System.out.println("Philosopher " + phil_number + " ready to eat.");
while ( times_eaten < TIMES_2_EAT ) {
think();
// now get ready to eat
get_Chopsticks(phil_number);
eat();
System.out.println("Philosopher " + phil_number + " just ate " +
times_I_have_eaten + "th meal.");
// put down chopsticks
replace_chopsticks(phil_number);
times_I_have_eaten++;
}
// nothing left to eat, so leave the table
num_philosophers--;
System.out.println("Philosopher " + phil_number + " has eaten: " +
times_I_have_eaten + " times.");
System.out.println(" Number phils left: " + num_philosophers);
System.out.println(" Total meals eaten: " + times_eaten);
synchronized (chopsticks) {
chopsticks.notifyAll(); // one last notify before you leave
}
}
} // end class philosopher
public class DiningPhils_fixsync {
public static void main( String[] args ) throws IOException {
String input_string;
BufferedReader in_from_keyboard = new BufferedReader (
new InputStreamReader (System.in) );
System.out.println("Enter the number of philosophers: ");
input_string = in_from_keyboard.readLine();
int num_philosophers = Integer.parseInt(input_string);
num_philosophers = (num_philosophers < 0 || num_philosophers > 10)
? 4 : num_philosophers;
System.out.println("Running with " + num_philosophers + " philosophers");
int i;
for (i=0; i<num_philosophers; i++) {
philosopher p = new philosopher( i );
new Thread( p ).start();
}
}//end main
}//end public class ThreadTester
Thanks,
J
|