cena de los filosofos en java

6
import java.util.*; public class Philosopher implements Runnable { private int id; private DiningRoom diningRoom; public Philosopher(int i, DiningRoom dr) { id=i; diningRoom=dr; } public void think() { System.out.println(id+": thinking.."); try { Thread.sleep(50+(int)(Math.random()*800.0)); } catch (Exception e) { } } public void run() { for (int i=0; i<3; i++) { think(); diningRoom.dine(id); } } public static void main(String args[]) { DiningRoom dr=new DiningRoom(); Philosopher p[]={ new Philosopher(0,dr), new Philosopher(1,dr), new Philosopher(2,dr), new Philosopher(3,dr), new Philosopher(4,dr) }; for (int i=0; i<p.length; i++) new Thread(p[i]).start(); } } class DiningRoom { private final static int THINKING=0; private final static int HUNGRY=1; private final static int EATING=2; private int[] state = { THINKING,THINKING,THINKING,THINKING,THINKING };

Upload: cesar-ulloa

Post on 29-Nov-2015

85 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Cena de Los Filosofos en Java

import java.util.*;

public class Philosopher implements Runnable {

private int id; private DiningRoom diningRoom;

public Philosopher(int i, DiningRoom dr) { id=i; diningRoom=dr; } public void think() {

System.out.println(id+": thinking..");try { Thread.sleep(50+(int)(Math.random()*800.0)); }catch (Exception e) { }

} public void run() { for (int i=0; i<3; i++) { think(); diningRoom.dine(id); } }

public static void main(String args[]) {DiningRoom dr=new DiningRoom();Philosopher p[]={ new Philosopher(0,dr),

new Philosopher(1,dr), new Philosopher(2,dr),new Philosopher(3,dr), new Philosopher(4,dr) };

for (int i=0; i<p.length; i++)new Thread(p[i]).start();

}}

class DiningRoom {

private final static int THINKING=0; private final static int HUNGRY=1; private final static int EATING=2;

private int[] state = { THINKING,THINKING,THINKING,THINKING,THINKING };

private void eat(int p) {System.out.println(p+": eating..");try { Thread.sleep(50+(int)(Math.random()*800.0)); }catch (Exception e) { }

} public void dine(int p) { grabForks(p); eat(p);

Page 2: Cena de Los Filosofos en Java

releaseForks(p); } private synchronized void grabForks(int p) { state[p]=HUNGRY;

System.out.println(p+": waiting & hungry.."); test(p); while ( state[p] != EATING ) try { wait(); } catch(Exception e) {} } private synchronized void releaseForks(int p) { state[p]=THINKING; test((p+4)%5); test((p+1)%5); } private void test(int p) { if ( state[p] == HUNGRY && state[(p+1)%5] != EATING && state[(p+4)%5] != EATING ) { state[p]=EATING; notifyAll(); } }}

Segundo

class Semaphore extends Object{ private int count;

public Semaphore(int startingCount) { count = startingCount; }

public void down(){ synchronized (this) { while (count <= 0) { // We must wait try { wait(); } catch (InterruptedException ex) { // I was interupted, continue onwards } } // We can decrement the count count--; } }

public void up(){ synchronized (this) {

Page 3: Cena de Los Filosofos en Java

count++; // notify a waiting thread to wakeup if (count == 1 ) { notify(); } } }}

Tercero

// Philosopher.java

import Semaphore;

public class Philosopher extends Thread { // Shared by all Philosophers public final static int N = 5; // Number of philosophers

public final static int THINKING = 0; // Philosopher is thinking public final static int HUNGRY = 1; // Philosopher is hungry public final static int EATING = 2; // Philosopher is eating

private static int state[] = new int[N]; // Array to keep track of everyones state

private static Semaphore mutex = new Semaphore(1); // Mutual exclusion for // critical regions private static Semaphore s[] = new Semaphore[N]; // One for each Philosopher

// Instance variable public int myNumber; // Which philosopher am I public int myLeft; // Number of my left neighbor public int myRight; // Number of my right neighbor

public Philosopher(int i) { // Make a philosopher myNumber = i; myLeft = (i+N-1) % N; // Compute the left neighbor myRight = (i+1) % N; // Compute the right neighbor }

public void run() { // And away we go

Page 4: Cena de Los Filosofos en Java

while(true){ think(); // Philosopher is thinking take_forks(); // Acquire two forks or block eat(); // Yum-yum, spahgetti put_forks(); // Put both forks back on the table } }

public void take_forks(){ // Take the forks I need mutex.down(); // Enter critical region state[myNumber] = HUNGRY; // Record the fact that I am hungry test(myNumber); // Try to acquire two forks mutex.up(); // Leave critical region s[myNumber].down(); // Block if forks were not acquired }

public void put_forks(){ mutex.down(); // Enter critical region state[myNumber] = THINKING; // Philosopher has finished eating test(myLeft); // See if left neighbor can now eat test(myRight); // See if right neighbor can now eat mutex.up(); // Leave critical region }

public void test(int k){ // Test philosopher k, // from 0 to N-1 int onLeft = (k+N-1) % N; // K's left neighbor int onRight = (k+1) % N; // K's right neighbor if( state[k] == HUNGRY && state[onLeft] != EATING && state[onRight] != EATING ) { // Grab those forks state[k] = EATING; s[k].up(); } }

public void think(){ System.out.println("Philosopher " + myNumber + " is thinking"); try { sleep(1000); } catch (InterruptedException ex){ } }

public void eat(){ System.out.println("Philosopher " + myNumber + " is eating");

Page 5: Cena de Los Filosofos en Java

try { sleep(5000); } catch (InterruptedException ex){ } }

public static void main(String args[]) {

Philosopher p[] = new Philosopher[N];

for(int i=0; i<N; i++) { // Create each philosopher and their semaphore p[i] = new Philosopher(i); s[i] = new Semaphore(0);

// Start the threads running p[i].start(); } }}