Bonjour,
je suis un passionné de programmation et voilà je debute en programmation systeme(en autodidacte).un ami a moi m'a parlé du fameux probleme des philosophes.en cherchant sur internet je suis tombé sur un programme(en JAVA) que j'ai un peu modifié afin de pouvoir entrer les parametres moi meme.mais ce pendant je souhaiterai pouvoir le modifier encore afin de :
-pouvoir determiner moi meme lequel des philosophes doit commencer à manger
- et si possible interrompre un philosophe entrain de manger pour que son voisin puisse manger.
je pense qu'en comprenant mieux le programme je pourrais bien le modifier alors j'aimerais que quelqu'un puisse m'explique l'implementation des classes en( particulier "philosophers" que je ne comprends pas tres bien )de ce programme.
quelle est le role des differentes methodes de ces classes?
comment interagissent elles avec l'ensemble du programme?
le programme est le suivant:
(il comporte trois classe(fichiers)phil, philosophers et informable
************************************
import java.io.*;
public class phil implements Informable
{ public static void main(String args[])
{ int n = 0;
if (args.length > 0)
{ try { n = Integer.parseInt(args[0]); }
catch (NumberFormatException e) {}
}
System.out.println("entrer le nombre de philosophes");
n=lireInt();
new Philosophers(new phil()).startPhilosophers(n);
}
private static final String doing[] = {"thinking", "hungry", "eating"};
/*
public synchronized void inform(int changed, int state)
{ String doing = "";
switch (state)
{ case Philosophers.THINKING:
doing = "thinking";
break;
case Philosophers.HUNGRY:
doing = "hungry";
break;
case Philosophers.EATING:
doing = "eating";
break;
}
System.out.println(changed + " " + doing);
}*/
public synchronized void inform(int changed, int state)
{ System.out.println(changed + " " + doing[state]);
}
public static int lireInt()
{ int ligne=0;
String ligneLue=null;
char car;
do {
car='o';
try {
ligneLue=lireString();
ligne=Integer.parseInt(ligneLue);
}
catch (NumberFormatException e) {
System.out.println("ERREUR DE LECTURE");
System.out.println("Veuillez entrer un nombre entier.");
car='n';
}
}while (car=='n');
return ligne;
}
public static String lireString()
{ String ligne=new String();
try {
InputStreamReader f=new InputStreamReader(System.in);
BufferedReader lecteur=new BufferedReader(f);
ligne=lecteur.readLine();
}
catch (IOException e) {
System.out.println("ERREUR DE LECTURE");
System.exit(1);
}
return ligne;
}
}
***********************************
import java.util.*;
public class Philosophers implements Runnable
{ private static final int MINTHINK = 20000;
private static final int MAXTHINK = 40000;
private static final int MINEAT = 20000;
private static final int MAXEAT = 40000;
public static final int THINKING = 0;
public static final int HUNGRY = 1;
public static final int EATING = 2;
private Informable animator;
private Random rnd = new Random();
private boolean isRunning = false;
private int n = 0;
private boolean chopstickFree[] = null;
private Thread th[] = null;
Philosophers(Informable a)
{ animator = a;
}
public void startPhilosophers(int n)
{ if (isRunning) stopPhilosophers();
this.n = n;
chopstickFree = new boolean[n];
th = new Thread[n];
for (int i = 0; i < n; i++)
chopstickFree[i] = true;
for (int i = 0; i < n; i++)
(th[i] = new Thread(this, "" + i)).start();
isRunning = true;
}
public void stopPhilosophers()
{ if (isRunning)
{ for (int i = 0; i < n; i++)
th[i].interrupt();
isRunning = false;
}
}
private int me()
{ return Integer.parseInt(Thread.currentThread().getName());
}
private void think() throws InterruptedException
{ animator.inform(me(), THINKING);
Thread.sleep(MINTHINK + rnd.nextInt(MAXTHINK - MINTHINK));
}
private void eat() throws InterruptedException
{ animator.inform(me(), EATING);
Thread.sleep(MINEAT + rnd.nextInt(MAXEAT - MINEAT));
}
private boolean canEat()
{ return chopstickFree[me()] && chopstickFree[(me() + 1) % n];
}
private synchronized void takeChopsticks() throws InterruptedException
{ while (!canEat()) wait();
chopstickFree[me()] = chopstickFree[(me() + 1) % n] = false;
}
private synchronized void leaveChopsticks()
{ chopstickFree[me()] = chopstickFree[(me() + 1) % n] = true;
notifyAll();
}
public void run()
{ while(true)
{ try {think();}
catch (InterruptedException e) {break;}
animator.inform(me(), HUNGRY);
try {takeChopsticks();}
catch (InterruptedException e) {break;}
try {eat();}
catch (InterruptedException e) {break;}
leaveChopsticks();
}
}
}
***********************
abstract public interface Informable
{ public abstract void inform(int changed, int state);
}
Si tu veux progresser en programmation, tu devrais peut être faire se programme toi même, enfin trouver une solution a ce problème tout seul.
Si tu as des difficulté sur ce que tu essaye de faire, tu pourra venir ici nous demander de l'aide.
Le programmes que tu balance plus haut est complexe, ce n'est pas notre rôle et notre volonté de dire comment marche tel ou tel programme, pour progresser il faut pensé par soi même.
__________________________ PARTICIPER AUX DÉFIS MP !