Buscar

SOLUÇÃO EM JAVA - PROBLEMA JANTAR DOS FILOSOFOS

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/build/built-jar.properties
#Sun, 12 Feb 2017 23:51:53 -0300
C\:\\Users\\Daniel\\Desktop\\Jantar_filosofo\\Jantar_filosofo=
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/build/classes/.netbeans_automatic_build
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/build/classes/.netbeans_update_resources
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/build/classes/jantar_filosofo/Filosofo.class
package jantar_filosofo;
public synchronized class Filosofo extends Thread {
 int id;
 final int PENSANDO;
 final int FAMINTO;
 final int COMENDO;
 public void Filosofo(String, int);
 public void ComFome();
 public void Come();
 public void Pensa();
 public void LargarGarfo() throws InterruptedException;
 public void PegarGarfo() throws InterruptedException;
 public void TentarObterGarfos();
 public void run();
 public int VizinhoDireita();
 public int VizinhoEsquerda();
}
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/build/classes/jantar_filosofo/Principal.class
package jantar_filosofo;
public synchronized class Principal {
 static java.util.concurrent.Semaphore mutex;
 static java.util.concurrent.Semaphore[] semaforos;
 static Filosofo[] filosofos;
 static int[] estado;
 public void Principal();
 public static void main(String[]);
 static void <clinit>();
}
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/build.xml
 
 Builds, tests, and runs the project Jantar_filosofo.
 
 
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/dist/Jantar_filosofo.jar
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.7
Created-By: 1.8.0_121-b13 (Oracle Corporation)
Class-Path: 
X-COMMENT: Main-Class will be added automatically by build
Main-Class: jantar_filosofo.Principal
.netbeans_automatic_build
.netbeans_update_resources
jantar_filosofo/Filosofo.class
package jantar_filosofo;
public synchronized class Filosofo extends Thread {
 private int identificador;
 private garfo esquerdo;
 private garfo direito;
 public void Filosofo(int, garfo, garfo);
 public void run();
}
jantar_filosofo/Principal.class
package jantar_filosofo;
public synchronized class Principal {
 static java.util.concurrent.Semaphore controleGarfo;
 public void Principal();
 public static void main(String[]);
 static void <clinit>();
}
jantar_filosofo/garfo.class
package jantar_filosofo;
public synchronized class garfo {
 public int identificador;
 private boolean disponivel;
 public void garfo(int);
 public static boolean pegarGarfo(garfo, garfo, int) throws InterruptedException;
 public static void soltarGarfo(garfo, garfo, int) throws InterruptedException;
}
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/dist/README.TXT
========================
BUILD OUTPUT DESCRIPTION
========================
When you build an Java application project that has a main class, the IDE
automatically copies all of the JAR
files on the projects classpath to your projects dist/lib folder. The IDE
also adds each of the JAR files to the Class-Path element in the application
JAR files manifest file (MANIFEST.MF).
To run the project from the command line, go to the dist folder and
type the following:
java -jar "Jantar_filosofo.jar" 
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Notes:
* If two JAR files on the project classpath have the same name, only the first
JAR file is copied to the lib folder.
* Only JAR files are copied to the lib folder.
If the classpath contains other types of files or folders, these files (folders)
are not copied.
* If a library on the projects classpath also has a Class-Path element
specified in the manifest,the content of the Class-Path element has to be on
the projects runtime path.
* To set a main class in a standard Java project, right-click the project node
in the Projects window and choose Properties. Then click Run and enter the
class name in the Main Class field. Alternatively, you can manually type the
class name in the manifest Main-Class element.
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/manifest.mf
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/nbproject/build-impl.xml
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set src.dir
 Must set test.src.dir
 Must set build.dir
 Must set dist.dir
 Must set build.classes.dir
 Must set dist.javadoc.dir
 Must set build.test.classes.dir
 Must set build.test.results.dir
 Must set build.classes.excludes
 Must set dist.jar
Must set javac.includes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 No tests executed.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set JVM to use for profiling in profiler.info.jvm
 Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
Must select some files in the IDE or set javac.includes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 To run this application from the command line without Ant, try:
 
 java -jar "${dist.jar.resolved}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set debug.class
 
 
 
 
 Must select one file in the IDE or set debug.class
 
 
 
 
 Must set fix.includes
 
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set profile.class
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 
 Must select some files in the IDE or set test.includes
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set javac.includes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Some tests failed; see details above.
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set test.includes
 
 
 
 Some tests failed; see details above.
 
 
 
 Must select some files in the IDE or set test.class
 Must select some method in the IDE or set test.method
 
 
 
 Some tests failed; see details above.
 
 
 
 
 Must select one file in the IDE or set test.class
 
 
 
 Must select one file in the IDE or set test.class
 Must select some method in the IDE or set test.method
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/nbproject/genfiles.properties
build.xml.data.CRC32=93d15612
build.xml.script.CRC32=5a5a0b19
build.xml.stylesheet.CRC32=8064a381@1.79.1.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=93d15612
nbproject/build-impl.xml.script.CRC32=71a9c56d
nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/nbproject/private/private.properties
compile.on.save=true
user.properties.file=C:\\Users\\Fabricia\\AppData\\Roaming\\NetBeans\\8.1\\build.properties
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/nbproject/private/private.xml
 
 
 
 
 
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/nbproject/project.properties
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
 ${run.classpath}
debug.test.classpath=\
 ${run.test.classpath}
# Os arquivos em build.classes.dir que devem ser exclu\u00eddos do jar de distribui\u00e7\u00e3o
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/Jantar_filosofo.jar
dist.javadoc.dir=${dist.dir}/javadoc
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.processorpath=\
 ${javac.classpath}
javac.source=1.7
javac.target=1.7
javac.test.classpath=\
 ${javac.classpath}:\
 ${build.classes.dir}
javac.test.processorpath=\
 ${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=jantar_filosofo.Principal
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
 ${javac.classpath}:\
 ${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.test.classpath=\
 ${javac.test.classpath}:\
 ${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/nbproject/project.xml
 
 org.netbeans.modules.java.j2seproject
 
 
 Jantar_filosofo
 
 
 
 
 
 
 
 
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/src/jantar_filosofo/Filosofo.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jantar_filosofo;
public class Filosofo extends Thread {
 int id;
 //estados
 final int PENSANDO = 0;
 final int FAMINTO = 1;
 final int COMENDO = 2;
 
 public Filosofo(String nome, int id) {
 super(nome);
 this.id = id;
 }
 public void ComFome() {
 Principal.estado[this.id] = 1;
 System.out.println("O Filósofo " + getName() + " está FAMINTO!");
 }
 public void Come() {
 Principal.estado[this.id] = 2;
 System.out.println("O Filósofo " + getName() + " está COMENDO!");
 try {
 Thread.sleep(1000L);
 } catch (InterruptedException ex) {
 System.out.println("ERROR>" + ex.getMessage());
 }
 }
 public void Pensa() {
 Principal.estado[this.id] = 0;
 System.out.println("O Filósofo " + getName() + " está PENSANDO!");
 try {
 Thread.sleep(1000L);
 } catch (InterruptedException ex) {
 System.out.println("ERROR>" + ex.getMessage());
 }
 }
 public void LargarGarfo() throws InterruptedException {
 Principal.mutex.acquire();
 Pensa();
 /*Quando um filosofo largar os garfos, o vizinho da esquera e da direita
 podem tentar pegar os garfos*/
 Principal.filosofos[VizinhoEsquerda()].TentarObterGarfos();
 Principal.filosofos[VizinhoDireita()].TentarObterGarfos();
 Principal.mutex.release();
 }
 public void PegarGarfo() throws InterruptedException {
 Principal.mutex.acquire();
 ComFome();
 //caso a condição for verdadeira, semaforo(1), permitindo
 //que o filosofo obtenha os garfos
 TentarObterGarfos(); 
 Principal.mutex.release();
 //caso a condição não seja verdadeira, o filosofo vai ficar travado
 //no seu respectivo indice do semaforo, até chegar sua vez novamente
 //para tentar pegar os garfos
 Principal.semaforos[this.id].acquire();//semaforos[this.id] = new Semaphore(0)
 }
 public void TentarObterGarfos() {
 //se o filosofo estiver faminto e o vizinho esquerdo e direito não
 //estiver comendo, chama metodo come();
 if (Principal.estado[this.id] == 1
 && Principal.estado[VizinhoEsquerda()] != 2
 && Principal.estado[VizinhoDireita()] != 2) {
 Come();
 Principal.semaforos[this.id].release();//semaforos[this.id] = new Semaphore(1)
 } else {
 System.out.println(getName() + " não conseguiu comer!");
 }
 }
 @Override
 public void run() {
 try {
 Pensa();
 System.out.println("");
 do {
 PegarGarfo();
 Thread.sleep(1000L);
 LargarGarfo();
 } while (true);
 } catch (InterruptedException ex) {
 System.out.println("ERROR>" + ex.getMessage());
 return;
 }
 }
 public int VizinhoDireita() {
 return (this.id + 1) % 5;
 }
 public int VizinhoEsquerda() {
 // filosofo 0 recebe vizinho a esquerda 4, porque se calcularmos
 //o resultado será -1.
 if (this.id == 0) { 
 return 4;
 } else {
 return (this.id - 1) % 5;
 }
 }
}
Jantar_filosofo/Jantar_filosofo2/Jantar_filosofo/src/jantar_filosofo/Principal.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jantar_filosofo;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Fabricia
 */
public class Principal {
 static Semaphore mutex = new Semaphore(1);
 //semaforo para cada filosofo
 static Semaphore[] semaforos = new Semaphore[5]; 
 static Filosofo[] filosofos = new Filosofo[5];
 static int[] estado = new int[5];
 public static void main(String[] args) {
 //todos os filosofos iniciam pensando
 for (int i = 0; i < estado.length; i++) {
 estado[i] = 0;
 }
 // Inicializa todos filósofos
 filosofos[0] = new Filosofo("Sócrates", 0);
 filosofos[1] = new Filosofo("Platão", 1);
 filosofos[2] = new Filosofo("Aristótoles", 2);
 filosofos[3] = new Filosofo("Tales", 3);
 filosofos[4] = new Filosofo("Parmênides", 4);
 
 //saber quais garfos pertence aos filosofos
 for (int i = 0; i < filosofos.length; i++) { 
 System.out.println("garfo " + i + " - filosofo " + i + " - garfo " + (i + 1) % 5); 
 }
 System.out.println(""); 
 /*semaforos[0] = new Semaphore(0);
 semaforos[1] = new Semaphore(0);
 semaforos[2] = new Semaphore(0);
 semaforos[3] = new Semaphore(0);
 semaforos[4] = new Semaphore(0);*/
 for (int i = 0; i < semaforos.length; i++) {
 semaforos[i] = new Semaphore(0);
 }
 /*filosofos[0].start();
 filosofos[1].start();
 filosofos[2].start();
 filosofos[3].start();
 filosofos[4].start();*/ 
 for (int i = 0; i < filosofos.length; i++) {
 filosofos[i].start();
 }
 try {
 Thread.sleep(10000);
 System.exit(0);
 } catch (InterruptedException ex) {
 Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
 }
 }
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais