diff --git a/Changelog.md b/Changelog.md index 727df9d..aa0cde1 100644 --- a/Changelog.md +++ b/Changelog.md @@ -11,6 +11,12 @@ ## Next release +## v0.4.9 + +- [F] volatile modifier to every integer attribute in Semaphore: meanSleepTimeAfterAwait_ms. +- [F] volatile modifier to every integer attribute in Monitor: inPurgatory, pendingSignals and meanSleepTimeAfterAwait. +- [F] Fabrica and Consumo allow 0 mean times now. + ## v0.4.8 - [T] Makefile added diff --git a/README.md b/README.md index b5da97a..53e388d 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,7 @@ cclib is a Java library used in concurrency related course at Universidad Polit Run `make` or `./gradlew tasks` and follow instructions. -## TODO - -- Tryer.java to take advantage from generics (result type) and lambda. -- Check of mean times are really the mean of the indicated value: for - instance, nextInt(2 * n) generates an integer from 0 to 2n-1, this - means the mean is (2n-1)/2 that is not n. +We are following the git workflow *git flow*: https://guides.github.com/introduction/flow/ ## Classes in the cclib @@ -51,3 +46,13 @@ The rest of the classes are used in the class assignments. Most names are in Spa in a shot). - Productor (producer): instances are threads that simulate production and storage of products + +## TODO + +- The library should be split, one for concurrency mechanisms, another + for classes used in "Concurrencia" assignments at UPM. +- Tryer.java to take advantage from generics (result type) and lambda. +- Check of mean times are really the mean of the indicated value: for + instance, nextInt(2 * n) generates an integer from 0 to 2n-1, this + means the mean is (2n-1)/2 that is not n. + diff --git a/build.gradle b/build.gradle index 09f7e09..d179475 100644 --- a/build.gradle +++ b/build.gradle @@ -9,9 +9,11 @@ plugins { // Apply the java-library plugin to add support for Java Library id 'java-library' + // // Support for building a distribution ZIP for a Java library (JAR file for the library and its dependencies) + // id 'java-library-distribution' } -version = "0.4.8" +version = "0.4.9" repositories { // Use jcenter for resolving your dependencies. @@ -29,3 +31,14 @@ dependencies { // Use JUnit test framework testImplementation 'junit:junit:4.12' } + +// Add source to jar files (by default jar tasks only include compiled classes) +jar { + from sourceSets.main.allSource +} + +// Task to zip documentation +task buildDoc(type: Zip, dependsOn: javadoc) { + from 'build/docs' + archiveName rootProject.name+"-"+version+"-docs.zip" +} diff --git a/src/main/java/es/upm/babel/cclib/Consumo.java b/src/main/java/es/upm/babel/cclib/Consumo.java index ad0e297..e497b49 100644 --- a/src/main/java/es/upm/babel/cclib/Consumo.java +++ b/src/main/java/es/upm/babel/cclib/Consumo.java @@ -33,18 +33,10 @@ static public void establecerTiempoMedioCons(int tmc_ms) { * Simula el consumo de un producto. */ static public void consumir(Producto prod) { - int t; - ConcIO.printfnl("inicio consumo: " + prod + "..."); - t = random.nextInt(2 * tiempo_medio_cons_ms); - try { - Thread.sleep(t); - } catch (Exception ex) { - ConcIO.printfnl("excepción capturada: " + ex); - ex.printStackTrace(); - } finally { - ConcIO.printfnl("fin consumo en " + t + "ms."); - } - } + ConcIO.printfnl("inicio consumo: " + prod + "..."); + simularConsumo(prod); + ConcIO.printfnl("fin consumo: " + prod); + } /** * Simula el consumo de un paquete de productos. @@ -53,19 +45,27 @@ static public void consumir(Producto[] prods) { ConcIO.printfnl("inicio consumo de " + prods.length + " productos..."); for (int i = 0; i < prods.length; i++) { - int t; - t = random.nextInt(2 * tiempo_medio_cons_ms); - try { - Thread.sleep(t); - } catch (Exception ex) { - ConcIO.printfnl("excepción capturada: " + ex); - ex.printStackTrace(); - } finally { - ConcIO.printfnl("consumido producto " - + prods[i] + " en " + t + "ms."); - } + simularConsumo(prods[i]); + ConcIO.printfnl("producto consumido: " + prods[i]); } ConcIO.printfnl("fin consumo del paquete de " + prods.length + " productos"); - } + } + + /** + * Simula el consumo de un producto. + * + * @return tiempo de consumo + */ + static public void simularConsumo(Producto prod) { + if (tiempo_medio_cons_ms > 0) { + int t = random.nextInt(2 * tiempo_medio_cons_ms); + try { + Thread.sleep(t); + } catch (Exception ex) { + ConcIO.printfnl("excepción capturada: " + ex); + ex.printStackTrace(); + } + } + } } diff --git a/src/main/java/es/upm/babel/cclib/Fabrica.java b/src/main/java/es/upm/babel/cclib/Fabrica.java index f002c74..4aaf045 100644 --- a/src/main/java/es/upm/babel/cclib/Fabrica.java +++ b/src/main/java/es/upm/babel/cclib/Fabrica.java @@ -34,18 +34,9 @@ static public void establecerTiempoMedioProd(int tmp_ms) { */ static public Producto producir() { Producto prod; - int t; ConcIO.printfnl("inicio producción..."); - t = random.nextInt(2 * tiempo_medio_prod_ms); - try { - Thread.sleep(t); - } catch (Exception ex) { - ConcIO.printfnl("excepción capturada: " + ex); - ex.printStackTrace(); - } finally { - prod = new Producto(); - ConcIO.printfnl("fin producción: " + prod + " en " + t + "ms."); - } + prod = simularProduccion(); + ConcIO.printfnl("fin producción: " + prod); return prod; } @@ -57,21 +48,27 @@ static public Producto[] producir(int n) { ConcIO.printfnl("inicio producción de " + prods.length + " productos..."); for (int i = 0; i < prods.length; i++) { - int t; - t = random.nextInt(2 * tiempo_medio_prod_ms); - try { - Thread.sleep(t); - } catch (Exception ex) { - ConcIO.printfnl("excepción capturada: " + ex); - ex.printStackTrace(); - } finally { - prods[i] = new Producto(); - ConcIO.printfnl("producido producto: " - + prods[i] + " en " + t + "ms."); - } + prods[i] = simularProduccion(); + ConcIO.printfnl("producto producido: " + prods[i]); } ConcIO.printfnl("fin producción del paquete de " + prods.length + " productos"); return prods; } + + /** + * Simula una producción de un producto. + */ + static private Producto simularProduccion() { + if (tiempo_medio_prod_ms > 0) { + int t = random.nextInt(2 * tiempo_medio_prod_ms); + try { + Thread.sleep(t); + } catch (Exception ex) { + ConcIO.printfnl("excepción capturada: " + ex); + ex.printStackTrace(); + } + } + return new Producto(); + } } diff --git a/src/main/java/es/upm/babel/cclib/Monitor.java b/src/main/java/es/upm/babel/cclib/Monitor.java index a2bffeb..2d5de47 100644 --- a/src/main/java/es/upm/babel/cclib/Monitor.java +++ b/src/main/java/es/upm/babel/cclib/Monitor.java @@ -24,13 +24,13 @@ public class Monitor { * Number of threads that were moved to the purgatory and have not * reached heaven yet (they may have been signalled). */ - private int inPurgatory = 0; + private volatile int inPurgatory = 0; /** * Number of signalled threads which have not completed monitor * re-entrance, not counting those signalled in purgatory. */ - private int pendingSignals = 0; + private volatile int pendingSignals = 0; /** * In order to force more arbitrary interleavings on the use of @@ -39,7 +39,7 @@ public class Monitor { * no sleep will be executed and the interleaving will be the * "natural" one. */ - private int meanSleepTimeAfterAwait_ms = 0; + private volatile int meanSleepTimeAfterAwait_ms = 0; /** * Random number generator for sleeping time after await. diff --git a/src/main/java/es/upm/babel/cclib/Semaphore.java b/src/main/java/es/upm/babel/cclib/Semaphore.java index cc949f0..9a39d2c 100644 --- a/src/main/java/es/upm/babel/cclib/Semaphore.java +++ b/src/main/java/es/upm/babel/cclib/Semaphore.java @@ -16,7 +16,7 @@ public class Semaphore { * no sleep will be executed and the interleaving will be the * "natural" one. */ - static private int meanSleepTimeAfterAwait_ms = 0; + static private volatile int meanSleepTimeAfterAwait_ms = 0; /** * Random number generator for sleeping time after await. @@ -29,7 +29,7 @@ public class Semaphore { public Semaphore() { n = new java.util.concurrent.Semaphore(0,true); } - + /** * Semaphore constructor. Internal counter initizalised to n if n * is positive, 0 otherwise. @@ -38,7 +38,7 @@ public Semaphore(int n) { int i = n > 0 ? n : 0; this.n = new java.util.concurrent.Semaphore(i,true); } - + /** * If the parameter is greater than 0 more arbitrary interleavings * will be introduced.