Thursday, October 30, 2008

implementing Dining philosophers problem

it s just an example algorithm that can be use to avoid statvation as well as deadlock....
 

----------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package diningphilosopy;

/**
 *
 * @author FARHATH
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        Chopstick chpk[] = new Chopstick[5];
        Philosophers phil[] = new Philosophers[5];
        Thread[] threads = new Thread[5];
        for(int i =0 ; i<5;>
            chpk[i]= new Chopstick(i);
        }
        for(int i=0; i<5;>
            if(i== 4){
                phil[i] = new Philosophers(i, chpk[0], chpk[i]);
            }
            else{
            phil[i] = new Philosophers(i, chpk[i+1], chpk[i]);
            }
            
        }
        for(int i =0 ; i<5;>
            threads[i] = new Thread(phil[i]);
            threads[i].start();
           
        }
    }

}
----------------------------

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package diningphilosopy;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author FARHATH
 */
public class Philosophers implements Runnable {

    private int PhilId;
    private Chopstick left,  right;

    public Philosophers(int id, Chopstick left, Chopstick right) {
        this.PhilId = id;
        this.left = left;
        this.right = right;
    }

    public int getPhilId() {
        return PhilId;
    }

    public void setPhilId(int PhilId) {
        this.PhilId = PhilId;
    }

    public Chopstick getLeft() {
        return left;
    }

    public void setLeft(Chopstick left) {
        this.left = left;
    }

    public Chopstick getRight() {
        return right;
    }

    public void setRight(Chopstick right) {
        this.right = right;
    }

    public void run() {
        for (int i = 0; i <>
            try {
                Thread.sleep(1);
                boolean hasTakenUp = false;
                boolean hasAte = false;
                int attempt = 1;
                Thread.sleep(10);
                do {
                    
                    synchronized (right) {
                        synchronized (left) {
                            if (!right.isIsUp() && !left.isIsUp()) {
                                right.pickUp();
                                left.pickUp();
                                hasTakenUp = true;
                                System.out.println("Philosoper " + PhilId + " got the sticks in his " + attempt + " attempt "  );
                            }
                        }
                    }
                    
                    if (hasTakenUp) {
                        eat();
                        hasAte = true;
                    }
                    else{
                        System.out.println("Oophs ! philosopher "+ PhilId+ "didn't get the chance to eat in his "+ attempt+ " attempt  " );
                    }
                    
                    Thread.sleep(10);
                    attempt++;
                }while(!hasAte);
            } catch (InterruptedException ex) {
                Logger.getLogger(Philosophers.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void eat() {
        try {
            Thread.sleep(10);
            System.out.println("----The philosopher " + PhilId + " is eating now------");
            right.keepDown();
            Thread.sleep(10);
            left.keepDown();
            Thread.sleep(10);
            System.out.println("The philosopher " + PhilId + " is back to thinking now");
            Thread.sleep(10);
        } catch (InterruptedException ex) {
            Logger.getLogger(Philosophers.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
------------------

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package diningphilosopy;

/**
 *
 * @author FARHATH
 */
public class Chopstick {
private int stickId;
private boolean isUp;

    public Chopstick(int id){
        this.stickId = id; 
    }
    public int getStickId() {
        return stickId;
    }

    public void setStickId(int stickId) {
        this.stickId = stickId;
    }

    public boolean isIsUp() {
        return isUp;
    }

    public void setIsUp(boolean isUp) {
        this.isUp = isUp;
    }
    public boolean pickUp(){
       System.out.println("The chopstick  "+ stickId + " is picked up");
        isUp = true;
        return true;
    }
    public void keepDown(){
       System.out.println("The chopstick  "+ stickId + " is kept down back");
        isUp = false;
    }
}
-------------