RECHERCHER :
COMMUNAUTE MP
Identifiez vous ...
Devenir Membre
J'ai oublié mon MDP
DOMAINE MP
Bavardages
Langages Généraux
Langages Web
Langages DotNet
Autres langages
Dev. Jeux Video
Sécurité
Sys. Exploitation
Graphismes
Logiciels
Réseaux
Bases de données
Méthodologies
Emplois High-tech
Aide juridique
Articles juridiques
FORUM
Index des forums
Ajouter un sujet
Rechercher sujet
Contact Responsable
Devenir modérateur
CHAT MP IRC
Votre pseudo ...
Srv: irc.moteurprog.com
Chan: #MoteurProg
PARTICIPER
Plus de 3500 emplois.
Rechercher un job
Déposez votre CV
Emplois High-tech

Visiteur MP

 aide programmation concurante

Forum : JAVA
Sous Catégorie : Aucune
Type du sujet : Sujet Normale
FAQ : FAQ JAVA

SUIVI DES SUJETS PAR MAIL

SUIVI PAR MAIL INACTIF

RESOLUTION DU SUJET SUJET RESOLU
BLOQUAGE DU SUJET SUJET ACTIF
APPARTENANCE A LA FAQ N'APPARTIENT PAS A LA FAQ


PAGE : [1]

POSTER UN NOUVEAU SUJET REPONDRE A CE SUJET

FORUM JAVA

PREMIERE PAGE

PAGE PRECEDENTE

Page précedente

Page suivante

PAGE SUIVANTE

DERNIERE PAGE
kaenef
Nouveau membre
Inscrit : 10/02/2008
Messages : 7
Message
#164167
Posté le 25/06/09 à 10:07
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); }


Attention aux balises CODE


merci d'avance

HAUT DE PAGE

PROFIL MEMBRE LUI ECRIRE 

Publicité
Inscrit : X
Messages : X
Message
#Aucun

HAUT DE PAGE

  

kaenef
Nouveau membre
Inscrit : 10/02/2008
Messages : 7
Message
#164220
Posté le 26/06/09 à 17:09
A force de chercher j'ai trouvé la solution.
j'ai bien envie de terminer par un "MERCI" mais alors :A QUI?

HAUT DE PAGE

PROFIL MEMBRE LUI ECRIRE 

litle_titeuf
Superviseur :
- Langages Gén.
- Langages Dot.
- Bases de don.
Modérateur :
- Ruby
Chef de projet(s) :
- Genetique algor.

Avatar de litle_titeuf
Inscrit : 11/05/2006
Messages : 259
Message
#164475
Posté le 15/07/09 à 12:40
Bonjour,

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 !

Merci de ne pas oublier le tag Image
ImageLe langage sms est interdit
(sous peine de voir la question retirée.)

APPEL-HADOPI-blackout-du-net-francais

HAUT DE PAGE

PROFIL MEMBRE LUI ECRIRE 
POSTER UN NOUVEAU SUJET REPONDRE A CE SUJET

PREMIERE PAGE

PAGE PRECEDENTE Page précédente

Page suivante

PAGE SUIVANTE DERNIERE PAGE

FORUM JAVA



    PAGE : [1]



.: Site Web développé par Yann Pingot et l'équipe MPWG avec www.evolvia-web.com :.