Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 17th, 2008, 11:40 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Dining Philosopher.....FUN!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT