diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml
new file mode 100644
index 0000000..2b63946
--- /dev/null
+++ b/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+
\ No newline at end of file
diff --git a/out/production/TP2/Main.class b/out/production/TP2/Main.class
index 9a93b5f..4e1b9e5 100644
Binary files a/out/production/TP2/Main.class and b/out/production/TP2/Main.class differ
diff --git a/out/production/TP2/Main2.class b/out/production/TP2/Main2.class
new file mode 100644
index 0000000..6500bdf
Binary files /dev/null and b/out/production/TP2/Main2.class differ
diff --git a/out/production/TP2/sweet/Boulangerie.class b/out/production/TP2/sweet/Boulangerie.class
new file mode 100644
index 0000000..a552c87
Binary files /dev/null and b/out/production/TP2/sweet/Boulangerie.class differ
diff --git a/out/production/TP2/sweet/BoulangerieThreadSafe.class b/out/production/TP2/sweet/BoulangerieThreadSafe.class
new file mode 100644
index 0000000..558050e
Binary files /dev/null and b/out/production/TP2/sweet/BoulangerieThreadSafe.class differ
diff --git a/out/production/TP2/sweet/Client.class b/out/production/TP2/sweet/Client.class
new file mode 100644
index 0000000..16ea4c1
Binary files /dev/null and b/out/production/TP2/sweet/Client.class differ
diff --git a/out/production/TP2/sweet/Gateau.class b/out/production/TP2/sweet/Gateau.class
new file mode 100644
index 0000000..c834982
Binary files /dev/null and b/out/production/TP2/sweet/Gateau.class differ
diff --git a/out/production/TP2/sweet/LimitedClient.class b/out/production/TP2/sweet/LimitedClient.class
new file mode 100644
index 0000000..1055c60
Binary files /dev/null and b/out/production/TP2/sweet/LimitedClient.class differ
diff --git a/out/production/TP2/sweet/Patisserie.class b/out/production/TP2/sweet/Patisserie.class
new file mode 100644
index 0000000..f8bab47
Binary files /dev/null and b/out/production/TP2/sweet/Patisserie.class differ
diff --git a/out/production/TP2/sweet/Patissier.class b/out/production/TP2/sweet/Patissier.class
new file mode 100644
index 0000000..bec6178
Binary files /dev/null and b/out/production/TP2/sweet/Patissier.class differ
diff --git a/out/production/TP2/world/ThreadWeaver.class b/out/production/TP2/world/ThreadWeaver.class
new file mode 100644
index 0000000..453f2b7
Binary files /dev/null and b/out/production/TP2/world/ThreadWeaver.class differ
diff --git a/src/Main.java b/src/Main.java
index 1f5c4cc..a233f4a 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -1,5 +1,23 @@
+import sweet.Boulangerie;
+import sweet.Client;
+import sweet.Patissier;
+import world.ThreadWeaver;
+
public class Main {
public static void main(String[] args) {
- System.out.println("Hello World!"); // Display the string.
+ ThreadWeaver tw = new ThreadWeaver();
+ Boulangerie b = new Boulangerie();
+ Patissier p = new Patissier(b);
+ tw.addRunners(p, new Client(b));
+ tw.weave();
+ tw.run();
+
+ try{
+ Thread.sleep(2000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ p.shouldRun.set(false);
+ tw.recover(100L);
}
}
diff --git a/src/Main2.java b/src/Main2.java
new file mode 100644
index 0000000..63d0342
--- /dev/null
+++ b/src/Main2.java
@@ -0,0 +1,24 @@
+import sweet.Boulangerie;
+import sweet.BoulangerieThreadSafe;
+import sweet.Client;
+import sweet.Patissier;
+import world.ThreadWeaver;
+
+public class Main2 {
+ public static void main(String[] args) {
+ ThreadWeaver tw = new ThreadWeaver();
+ Boulangerie b = new BoulangerieThreadSafe();
+ Patissier p = new Patissier(b);
+ tw.addRunners(p, new Client(b));
+ tw.weave();
+ tw.run();
+
+ try{
+ Thread.sleep(2000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ p.shouldRun.set(false);
+ tw.recover(100L);
+ }
+}
\ No newline at end of file
diff --git a/src/sweet/BoulangerieThreadSafe.java b/src/sweet/BoulangerieThreadSafe.java
new file mode 100644
index 0000000..414c0bc
--- /dev/null
+++ b/src/sweet/BoulangerieThreadSafe.java
@@ -0,0 +1,29 @@
+package sweet;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+public class BoulangerieThreadSafe extends Boulangerie{
+ private final BlockingQueue sweets = new ArrayBlockingQueue(50);
+
+ @Override
+ public void depose(Patisserie p) {
+ try{
+ sweets.put(p);
+ }catch(InterruptedException ignored){}
+ }
+
+ @Override
+ public Patisserie achete() {
+ try{return sweets.take();} catch (InterruptedException e) {}
+ return null;
+ }
+
+ @Override
+ public void getStock() {
+ // TODO implement here
+ }
+}
diff --git a/src/sweet/Client.java b/src/sweet/Client.java
index 5f0dc21..6d0c75e 100644
--- a/src/sweet/Client.java
+++ b/src/sweet/Client.java
@@ -4,15 +4,25 @@ import java.util.*;
*
*/
public class Client implements Runnable {
+ protected final Boulangerie local;
/**
* Default constructor
*/
- public Client() {
+ public Client(Boulangerie b) {
+ local = b;
}
@Override
public void run() {
-
+ while(true) {
+ local.achete();
+ System.out.println("J'ai acheté ma patisserie");
+ try {
+ Thread.sleep(80);
+ } catch (InterruptedException e) {
+ break;
+ }
+ }
}
}
\ No newline at end of file
diff --git a/src/sweet/LimitedClient.java b/src/sweet/LimitedClient.java
new file mode 100644
index 0000000..72e908c
--- /dev/null
+++ b/src/sweet/LimitedClient.java
@@ -0,0 +1,20 @@
+package sweet;
+
+public class LimitedClient extends Client{
+ public LimitedClient(Boulangerie b) {
+ super(b);
+ }
+
+ @Override
+ public void run() {
+ while(true) {
+ local.achete();
+ System.out.println("J'ai acheté ma patisserie - Limited");
+ try {
+ Thread.sleep(80);
+ } catch (InterruptedException e) {
+ break;
+ }
+ }
+ }
+}
diff --git a/src/sweet/Patissier.java b/src/sweet/Patissier.java
index 4fdab00..8112f73 100644
--- a/src/sweet/Patissier.java
+++ b/src/sweet/Patissier.java
@@ -1,19 +1,32 @@
package sweet;
+import world.ThreadWeaver;
+
import java.util.*;
+import java.util.concurrent.atomic.AtomicBoolean;
/**
*
*/
public class Patissier implements Runnable {
-
+ public AtomicBoolean shouldRun = new AtomicBoolean(true);
+ private final Boulangerie local;
/**
* Default constructor
*/
- public Patissier() {
+ public Patissier(Boulangerie b) {
+ local = b;
}
@Override
public void run() {
-
+ while(shouldRun.get()) {
+ local.depose(new Patisserie());
+ System.out.println("J'ai produit ma patisserie");
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ break;
+ }
+ }
}
}
\ No newline at end of file
diff --git a/src/world/threadWeaver.java b/src/world/ThreadWeaver.java
similarity index 71%
rename from src/world/threadWeaver.java
rename to src/world/ThreadWeaver.java
index df60c6b..0d6f700 100644
--- a/src/world/threadWeaver.java
+++ b/src/world/ThreadWeaver.java
@@ -1,14 +1,14 @@
package world;
-import java.lang.reflect.Array;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
-public class threadWeaver {
+public class ThreadWeaver {
private final List runners = new ArrayList();
private final List managed = new ArrayList();
- public void addRunners(Runnable... runners){
-
+ public void addRunners(Runnable... addedRunners){
+ runners.addAll(Arrays.stream(addedRunners).toList());
}
public void weave(){
@@ -26,7 +26,7 @@ public class threadWeaver {
public void recover(){
for(Thread t : managed){
try {
- t.wait();
+ t.join();
}catch(InterruptedException ie){
System.out.println(ie.getMessage());
}
@@ -37,19 +37,20 @@ public class threadWeaver {
public void recover(Long timeout){
for(Thread t : managed){
try {
- t.wait(timeout);
+ t.join(timeout);
}catch(InterruptedException ie){
System.out.println(ie.getMessage());
}
- managed.remove(t);
}
termina();
}
public void termina(){
for(Thread t : managed){
- System.out.println("Thread "+t.getName()+" has not stopped being cleaned up");
- t.interrupt();
+ if(t.isAlive()) {
+ System.out.println("Thread " + t.getName() + " has not stopped being cleaned up");
+ t.interrupt();
+ }
}
managed.clear();
runners.clear();