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;
    }
}
-------------

Monday, March 10, 2008

Photos of Babies Deformed at Birth as aResult of Depleted Uranium

America always talk about peace.. and say that they are peace makers in the world. And say that the Muslims are the terrorist.
And the fight to maintain the peace in the world
Here are some of the great peace milestones that America laid in the heart of the innocent people.
And who is there to speak against it......
Is it a war against TRRORISM or war against PEACE

www.mindfully.org/Nucs/2003/DU-Baby2003.htm

Monday, March 3, 2008

Websphere

when i heard about websphere i was confused a lot about what does it really mean. any how found out some fact about it which i like to share with you.

Websphere is a brand name of IBM which consist of a family of products. But in common Websphere Application Server (WAS) (- it is an application server) is simply ment by websphere.
It consist of 3 components:
1. Foundation tools

2. Business Portal

3.Business integration

If u really like to learn about it ....
http://books.google.lk/books?hl=en&id=H4Ilf0ZiJZ0C&dq=java+programming+on+ibm+websphere&printsec=frontcover&source=web&ots=b5RRDfMbS7&sig=9IIEmEdoIrC7SmeurDC-hXYZ4Hk#PPA39,M1

Because just now I'm also refering to it :D

Sunday, February 24, 2008

first blog :)

so this is my first blog .Thought of starting a bolg just to inprove my language ;) . Any how now am in the later part of the training. And feel stupid of writing the report :(.

nothing much there for me to say. but try to keep on posting :D