From 8dcdcfc2a9d63b885d94b4b20da84c2dbc6f721c Mon Sep 17 00:00:00 2001
From: Rodolfoba1 <38680428+Rodolfoba1@users.noreply.github.com>
Date: Tue, 5 Apr 2022 22:50:19 -0600
Subject: [PATCH] Monitores
Rodolfo Bacilio Chivalan de Leon 1561817
---
Thread Race/nbproject/build-impl.xml | 93 ++++++++---------------
Thread Race/nbproject/genfiles.properties | 10 +--
Thread Race/nbproject/project.properties | 8 +-
Thread Race/nbproject/project.xml | 1 -
Thread Race/src/main/frmMain.java | 51 ++++++++++++-
5 files changed, 88 insertions(+), 75 deletions(-)
diff --git a/Thread Race/nbproject/build-impl.xml b/Thread Race/nbproject/build-impl.xml
index e8263ef..3a748d0 100644
--- a/Thread Race/nbproject/build-impl.xml
+++ b/Thread Race/nbproject/build-impl.xml
@@ -46,8 +46,8 @@ is divided into following sections:
-
-
+
+
@@ -119,43 +119,7 @@ is divided into following sections:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Must set platform.home
- Must set platform.bootcp
- Must set platform.java
- Must set platform.javac
-
- The J2SE Platform is not correctly set up.
- Your active platform is: ${platform.active}, but the corresponding property "platforms.${platform.active}.home" is not found in the project's properties files.
- Either open the project in the IDE and setup the Platform with the same name or add it manually.
- For example like this:
- ant -Duser.properties.file=<path_to_property_file> jar (where you put the property "platforms.${platform.active}.home" in a .properties file)
- or ant -Dplatforms.${platform.active}.home=<path_to_JDK_home> jar (where no properties file is used)
-
+
@@ -278,6 +242,20 @@ is divided into following sections:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -365,7 +343,7 @@ is divided into following sections:
-
+
@@ -416,7 +394,7 @@ is divided into following sections:
-
+
@@ -458,7 +436,7 @@ is divided into following sections:
-
+
@@ -537,7 +515,7 @@ is divided into following sections:
-
+
@@ -565,7 +543,7 @@ is divided into following sections:
-
+
@@ -641,7 +619,7 @@ is divided into following sections:
-
+
@@ -872,9 +850,6 @@ is divided into following sections:
-
-
-
@@ -924,7 +899,7 @@ is divided into following sections:
-
+
@@ -958,7 +933,7 @@ is divided into following sections:
-
+
@@ -990,7 +965,7 @@ is divided into following sections:
-
+
@@ -1224,7 +1199,7 @@ is divided into following sections:
To run this application from the command line without Ant, try:
- ${platform.java} -jar "${dist.jar.resolved}"
+ java -jar "${dist.jar.resolved}"
@@ -1326,8 +1301,8 @@ is divided into following sections:
-
-
+
+
@@ -1520,19 +1495,16 @@ is divided into following sections:
-
-
-
-
+
-
+
-
+
@@ -1714,7 +1686,6 @@ is divided into following sections:
-
diff --git a/Thread Race/nbproject/genfiles.properties b/Thread Race/nbproject/genfiles.properties
index f9b38c4..6c79c61 100644
--- a/Thread Race/nbproject/genfiles.properties
+++ b/Thread Race/nbproject/genfiles.properties
@@ -1,8 +1,8 @@
-build.xml.data.CRC32=7fe5bf0b
+build.xml.data.CRC32=02f50314
build.xml.script.CRC32=23a17979
-build.xml.stylesheet.CRC32=f85dc8f2@1.102.0.48
+build.xml.stylesheet.CRC32=f85dc8f2@1.96.0.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=7fe5bf0b
-nbproject/build-impl.xml.script.CRC32=28cea8aa
-nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.102.0.48
+nbproject/build-impl.xml.data.CRC32=02f50314
+nbproject/build-impl.xml.script.CRC32=63ea047b
+nbproject/build-impl.xml.stylesheet.CRC32=f89f7d21@1.96.0.48
diff --git a/Thread Race/nbproject/project.properties b/Thread Race/nbproject/project.properties
index dc97d8e..d2f008f 100644
--- a/Thread Race/nbproject/project.properties
+++ b/Thread Race/nbproject/project.properties
@@ -4,7 +4,7 @@ annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=Thread Race
-application.vendor=mfmatul
+application.vendor=DELL
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
@@ -46,8 +46,8 @@ javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
-javac.source=11
-javac.target=11
+javac.source=15
+javac.target=15
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
@@ -77,7 +77,7 @@ main.class=main.frmMain
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
-platform.active=JDK_11
+platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
diff --git a/Thread Race/nbproject/project.xml b/Thread Race/nbproject/project.xml
index bba268e..791aa9a 100644
--- a/Thread Race/nbproject/project.xml
+++ b/Thread Race/nbproject/project.xml
@@ -4,7 +4,6 @@
Thread Race
-
diff --git a/Thread Race/src/main/frmMain.java b/Thread Race/src/main/frmMain.java
index 980d87e..035354d 100644
--- a/Thread Race/src/main/frmMain.java
+++ b/Thread Race/src/main/frmMain.java
@@ -15,7 +15,8 @@
* @author migu_
*/
public class frmMain extends javax.swing.JFrame {
-
+ static final int N = 3; // constante que proporciona el tamaño del búfer
+ static super_monitor monitor = new super_monitor(); // crea instancia de un nuevo monitor
int contador = 1;
// Procesos
Proceso hilo1;
@@ -54,17 +55,59 @@ public void run() {
this.numeroAGenerar = (int)(Math.random()* 9 + 1);
this.miEtiqueta.setText(String.valueOf(numeroAGenerar));
// Región crítica
- regionCritica[posicion] = this.numeroAGenerar;
+ monitor.insertar(this.numeroAGenerar);
+ regionCritica[posicion] = monitor.eliminar();
+ posicion++;
String contenidoRC = "[" + String.valueOf(regionCritica[0]) + "]";
contenidoRC += "[" + String.valueOf(regionCritica[1]) + "]";
contenidoRC += "[" + String.valueOf(regionCritica[2]) + "]";
lblRegionCritica.setText(contenidoRC);
- posicion++;
// Operaciones post región crítica
System.out.println("Proceso finalizado con status: 0");
}
}
-
+
+ //////reutilizamos un poco de codigo
+ static class super_monitor{ // monitor como solución
+ private int bufer[] = new int[N];
+ private int cuenta=0, inf=0, sup=0; // contadores e índices
+
+ /**
+ * synchronized = Una vez un hilo ha empezado a ejecutar ese método,
+ * no se permitirá que ningún otro hilo empiece a ejecutar ningún otro
+ * método synchronized de ese objeto
+ */
+ public synchronized void insertar(int val){
+ if (cuenta==N)
+ ir_a_estado_inactivo(); // si el búfer está lleno, pasa al estado inactivo
+ bufer[sup]=val; // inserta un elemento en el búfer
+ sup=(sup+1)%N; // ranura en la que se va a colocar el siguiente elemento
+ cuenta=cuenta+1; // ahora hay un elemento más en el búfer
+ System.out.println(val + " ingresado");
+ if (cuenta==1)
+ notify(); // si el consumidor estaba inactivo, lo despierta [signal]
+ }
+
+ public synchronized int eliminar(){
+ int val;
+ if (cuenta==0)
+ ir_a_estado_inactivo(); // si el búfer está vacío, pasa al estado inactivo
+ val=bufer[inf]; // obtiene un elemento del búfer
+ inf = (inf + 1) %N; // ranura en la que se va a colocar el siguiente elemento
+ cuenta=cuenta-1; // un elemento menos en el búfer
+ if (cuenta==N-1)
+ notify(); // si el productor estaba inactivo, lo despierta [signal]
+ return val;
+ }
+
+ private void ir_a_estado_inactivo(){
+ try{
+ wait(); // Duerme al proceso en turno
+ }
+ catch(InterruptedException exc){};
+ }
+ }
+
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always