Skip to content
Snippets Groups Projects
Commit 03c1df20 authored by ROUSSANALY Azim's avatar ROUSSANALY Azim
Browse files

First commit

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 576 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
/target/
/bindist/
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>chantier</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF8
encoding//src/main/resources=UTF8
encoding//src/test/java=UTF8
encoding//src/test/resources=UTF8
encoding/<project>=UTF8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=11
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
# Simulation d'un chantier : version CLI
Ce projet est un exemple d'utilisation de la librairie `java.util.concurrent` (Threads)
Il est utilisé à titre d'exemple pour le cours de Programmation Objets avancée
en Master MIAGE à l'IDMC (Université de Lorraine).
L'objectif est de simuler les activités respectifs 1) de livraisons régulières de briques
d'un entrepôt vers un cahntier et 2) de déplacements de ces briques vers un mur en construction à l'aide d'une brouette.
![Illustration](chantier.png)
## Exemple d'utilisation
```
% ./run -n 500
tart...
17:12:31.532444
17:12:31.543136: Démarrage du thread arret
17:12:33.548657: Camion:1/Voyage:1/Quantité:500
17:12:33.584158: Brouette:1/Voyage:1/Reste:480 briques
17:12:33.786717: Brouette:1/Voyage:2/Reste:460 briques
17:12:33.991228: Brouette:1/Voyage:3/Reste:440 briques
17:12:34.197269: Brouette:1/Voyage:4/Reste:420 briques
17:12:34.403169: Brouette:1/Voyage:5/Reste:400 briques
17:12:34.608119: Brouette:1/Voyage:6/Reste:380 briques
17:12:34.812954: Brouette:1/Voyage:7/Reste:360 briques
17:12:35.017615: Brouette:1/Voyage:8/Reste:340 briques
17:12:35.223068: Brouette:1/Voyage:9/Reste:320 briques
17:12:35.427913: Brouette:1/Voyage:10/Reste:300 briques
17:12:35.631917: Brouette:1/Voyage:11/Reste:280 briques
17:12:35.836695: Brouette:1/Voyage:12/Reste:260 briques
17:12:36.040747: Brouette:1/Voyage:13/Reste:240 briques
17:12:36.244075: Brouette:1/Voyage:14/Reste:220 briques
17:12:36.449793: Brouette:1/Voyage:15/Reste:200 briques
stock:0
17:12:36.654866: Brouette:1/Voyage:16/Reste:180 briques
17:12:36.862013: Brouette:1/Voyage:17/Reste:160 briques
17:12:37.065277: Brouette:1/Voyage:18/Reste:140 briques
17:12:37.269760: Brouette:1/Voyage:19/Reste:120 briques
17:12:37.472158: Brouette:1/Voyage:20/Reste:100 briques
17:12:37.677877: Brouette:1/Voyage:21/Reste:80 briques
17:12:37.883815: Brouette:1/Voyage:22/Reste:60 briques
17:12:38.088295: Brouette:1/Voyage:23/Reste:40 briques
17:12:38.292673: Brouette:1/Voyage:24/Reste:20 briques
17:12:38.497047: Brouette:1/Voyage:25/Reste:0 briques
stock:0
17:12:41.553656: Fin du thread arret
durée: 10019ms
```
##Auteur
Azim Roussanaly (IDMC/Université de Lorraine)
##Licence
Licence MIT
\ No newline at end of file
chantier/chantier.png

29.8 KiB

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.ul.miage</groupId>
<artifactId>chantier</artifactId>
<version>0.1</version>
<properties>
<!-- Le code source en UTF8 -->
<project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
<!-- Où stocker la distribution binaire -->
<project.bindist.dir>${project.basedir}/bindist</project.bindist.dir>
<!-- Nom de la classe principale -->
<project.main.classname>${project.groupId}.${project.artifactId}.Main</project.main.classname>
<!-- Nom du script (bindist) -->
<project.bin.appname>run</project.bin.appname>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<!-- create bat scripts for apps -->
<!-- mvn package | appassembler:assemble -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>win</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
<configuration>
<assembleDirectory>${project.bindist.dir}</assembleDirectory>
<repositoryLayout>flat</repositoryLayout>
<programs>
<program>
<platforms>
<platform>windows</platform>
</platforms>
<mainClass>${project.main.classname}</mainClass>
<id>${project.bin.appname}</id>
</program>
</programs>
</configuration>
</execution>
<execution>
<id>unix</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
<configuration>
<assembleDirectory>${project.bindist.dir}</assembleDirectory>
<repositoryLayout>flat</repositoryLayout>
<programs>
<program>
<platforms>
<platform>unix</platform>
</platforms>
<mainClass>${project.main.classname}</mainClass>
<id>${project.bin.appname}</id>
</program>
</programs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- create a distribution archive -->
<!-- mvn install ou assembly:single -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-zip</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>zip.xml</descriptor>
</descriptors>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package fr.ul.miage.chantier;
import java.time.LocalTime;
public class Brouette {
private static int compteur = 0;
//attribut
private int id;
private int voyage;
private Chantier chantier;
//constructeur
public Brouette(Chantier chantier) {
compteur++;
setChantier(chantier);
setId(compteur);
setVoyage(0);
}
//methodes
/**
* une brouette déplace 20 briques
*/
public int deplacer() {
int q = 0;
if (chantier.getNum()<=0) {
return q;
}
voyage++;
synchronized (chantier) {
q = chantier.get(20);
}
System.out.print(LocalTime.now());
System.out.printf(": Brouette:%d/Voyage:%d/Reste:%d briques%n",
id, voyage, chantier.getNum());
return q;
}
//setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getVoyage() {
return voyage;
}
public void setVoyage(int voyage) {
this.voyage = voyage;
}
//setters
public Chantier getChantier() {
return chantier;
}
public void setChantier(Chantier chantier) {
this.chantier = chantier;
}
}
package fr.ul.miage.chantier;
import java.time.Instant;
import java.time.LocalTime;
import java.util.Random;
import java.util.logging.Logger;
public class Camion {
private static final Logger LOG = Logger.getLogger(Camion.class.getName());
private static final Random r = new Random(Instant.now().getEpochSecond());
private static int compteur = 0;
//attributs
private int id;
private int voyage;
private Entrepot entrepot;
private Chantier chantier;
//constructeur
public Camion(Entrepot e, Chantier c) {
compteur++;
setId(compteur);
setVoyage(0);
setEntrepot(e);
setChantier(c);
}
//methodes
/**
* livraison de 0...1000 briques (aléatoire)
* durée de la livraison = 2s
* @return quantité de briques livrées
*/
public int livrer() {
if (entrepot.getStock() <= 0) {
//pas de livraison
return 0;
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
LOG.warning(e.getMessage());
}
voyage++;
int q = r.nextInt(1000);
int q2 = entrepot.get(q);
synchronized (chantier) {
chantier.add(q2);
}
System.out.print(LocalTime.now());
System.out.printf(": Camion:%d/Voyage:%d/Quantité:%d%n",
id, voyage, q2);
return q2;
}
//setters & getters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getVoyage() {
return voyage;
}
public void setVoyage(int voyage) {
this.voyage = voyage;
}
public Entrepot getEntrepot() {
return entrepot;
}
public void setEntrepot(Entrepot entrepot) {
this.entrepot = entrepot;
}
public Chantier getChantier() {
return chantier;
}
public void setChantier(Chantier chantier) {
this.chantier = chantier;
}
}
package fr.ul.miage.chantier;
public class Chantier {
//attributs
private int num;
//constructeurs
public Chantier() {
this.num = 0;
}
//methodes
public int add(int n) {
num += n;
return num;
}
public int get(int n) {
num = (num < n)?0:num-n;
return num;
}
//setters & getters
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
package fr.ul.miage.chantier;
public class Entrepot {
//attributs
private int stock;
//contructeur
public Entrepot(int stock) {
this.stock = stock;
System.out.printf("Création de l'entrepot avec %d briques %n", stock);
}
//methodes
public int get(int n) {
int q = n;
if (stock < n) {
q =stock;
stock = 0;
} else {
stock -= n;
}
return q;
}
//setters & getters
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
package fr.ul.miage.chantier;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
public class Main {
private static final Logger LOG = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
// paramètres
int num = 2000;//default
// options
Options options = new Options();
Option number = new Option("n", "number", true, "nombre de briques");
options.addOption(number);
// parser la ligne de commande
CommandLineParser parser = new DefaultParser();
try {
CommandLine line = parser.parse(options, args);
if (line.hasOption("n")) {
try {
num = Integer.parseInt(line.getOptionValue("n"));
} catch (NumberFormatException e) {
LOG.warning("Nombre de briques incorrect !");
}
}
} catch (ParseException exp) {
LOG.severe("Erreur dans la ligne de commande");
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("Demo", options);
System.exit(1);
}
//traitement
Entrepot entrepot = new Entrepot(num);
Chantier chantier = new Chantier();
Camion camion = new Camion(entrepot, chantier);
Brouette brouette = new Brouette(chantier);
System.out.println("Start...");
System.out.println(LocalTime.now());
Instant top = Instant.now();
ScheduledExecutorService pool = Executors.newScheduledThreadPool(3);
//thread d'arret du processus
final Runnable arret = new Runnable() {
public void run() {
System.out.print(LocalTime.now());
System.out.printf(": Démarrage du thread arret%n");
while ((entrepot.getStock() > 0)||(chantier.getNum() > 0)) {
try {
Thread.sleep(5000);
System.out.printf("stock:%d%n",entrepot.getStock());
} catch (InterruptedException e) {
}
}
System.out.print(LocalTime.now());
System.out.printf(": Fin du thread arret%n");
Duration duration = Duration.between(top, Instant.now());
System.out.printf("durée: %dms\n",duration.toMillis());
pool.shutdown();
}
};
pool.execute(arret);
final Runnable livraison = new Runnable() {
public void run() {
if (entrepot.getStock() > 0) {
int q = camion.livrer();
}
}
};
//pool.scheduleAtFixedRate(livraison, 0, 10, TimeUnit.SECONDS);
pool.scheduleWithFixedDelay(livraison, 0, 5, TimeUnit.SECONDS);
final Runnable deplacement = new Runnable() {
public void run() {
brouette.deplacer();
}
};
pool.scheduleWithFixedDelay(deplacement, 0, 200, TimeUnit.MILLISECONDS);
}
}
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>src</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
<include>*.xml</include>
<include>*.png</include>
<include>*.jpg</include>
<include>*.jpeg</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
<fileSet>
<directory>${project.basedir}/src</directory>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
<fileSet>
<directory>${project.basedir}/samples</directory>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
<fileSet>
<directory>${project.basedir}/mkdocs</directory>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
<fileSet>
<directory>${project.basedir}/bindist</directory>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment