Elliott LE GUEHENNEC 2 years ago
commit 8603c04e28

Binary file not shown.

Binary file not shown.

@ -8,7 +8,9 @@ public class Main {
ThreadWeaver tw = new ThreadWeaver(); ThreadWeaver tw = new ThreadWeaver();
Boulangerie b = new Boulangerie(); Boulangerie b = new Boulangerie();
Patissier p = new Patissier(b); Patissier p = new Patissier(b);
tw.addRunners(p, new Client(b)); Client c1 = new Client(b);
Client c2 = new Client(b);
tw.addRunners(p, c1, c2);
tw.weave(); tw.weave();
tw.run(); tw.run();

@ -0,0 +1,21 @@
import sweet.*;
import world.ThreadWeaver;
public class Main3A {
public static void main(String[] args) {
ThreadWeaver tw = new ThreadWeaver();
Boulangerie b = new BoulangerieThreadSafe();
Patissier p = new Patissier(b);
tw.addRunners(p, new LimitedClient(b));
tw.weave();
tw.run();
try{
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
p.shouldRun.set(false);
tw.recover();
}
}

@ -0,0 +1,21 @@
import sweet.*;
import world.ThreadWeaver;
public class Main3B {
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();
}
}

@ -7,30 +7,23 @@ import java.util.*;
public class Boulangerie { public class Boulangerie {
private final ArrayList<Patisserie> sweets = new ArrayList<>(); private final ArrayList<Patisserie> sweets = new ArrayList<>();
/** public synchronized boolean depose(Patisserie p) {
*
*/
public synchronized void depose(Patisserie p) {
this.notify(); this.notify();
sweets.add(p); sweets.add(p);
return true;
} }
/**
*
*/
public synchronized Patisserie achete() { public synchronized Patisserie achete() {
while(sweets.size() == 0) { while(getStock() == 0) {
try {this.wait();} catch (InterruptedException ignored) {}} try {this.wait();} catch (InterruptedException ignored) {}}
Patisserie pat = sweets.get(0); Patisserie pat = sweets.get(0);
sweets.remove(pat); sweets.remove(pat);
return pat; return pat;
} }
/** public int getStock(){
* return sweets.size();
*/
public void getStock() {
// TODO implement here
} }
} }

@ -7,23 +7,25 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class BoulangerieThreadSafe extends Boulangerie{ public class BoulangerieThreadSafe extends Boulangerie{
private final BlockingQueue<Patisserie> sweets = new ArrayBlockingQueue<Patisserie>(50); private final BlockingQueue<Patisserie> sweets = new ArrayBlockingQueue<Patisserie>(10);
@Override @Override
public void depose(Patisserie p) { public boolean depose(Patisserie p) {
try{ try{
if(sweets.remainingCapacity() <= 0) return false;
sweets.put(p); sweets.put(p);
}catch(InterruptedException ignored){} }catch(InterruptedException ignored){return false;}
return true;
} }
@Override @Override
public Patisserie achete() { public Patisserie achete() {
try{return sweets.take();} catch (InterruptedException e) {} try{return sweets.take();} catch (InterruptedException ignored) {}
return null; return null;
} }
@Override @Override
public void getStock() { public int getStock() {
// TODO implement here return sweets.size();
} }
} }

@ -15,8 +15,8 @@ public class Client implements Runnable {
@Override @Override
public void run() { public void run() {
while(true) { while(local.getStock() < 9999) {
local.achete(); if(local.achete() == Gateau.GATEAU_EMPOISONNE) break;
System.out.println("J'ai acheté ma patisserie"); System.out.println("J'ai acheté ma patisserie");
try { try {
Thread.sleep(80); Thread.sleep(80);

@ -8,7 +8,7 @@ import java.util.*;
* *
*/ */
public class Gateau extends Patisserie { public class Gateau extends Patisserie {
public static final Gateau GATEAU_EMPOISONNE = new Gateau();
/** /**
* Default constructor * Default constructor
*/ */

@ -1,6 +1,7 @@
package sweet; package sweet;
public class LimitedClient extends Client{ public class LimitedClient extends Client{
private int consumed = 0;
public LimitedClient(Boulangerie b) { public LimitedClient(Boulangerie b) {
super(b); super(b);
} }
@ -9,6 +10,8 @@ public class LimitedClient extends Client{
public void run() { public void run() {
while(true) { while(true) {
local.achete(); local.achete();
consumed++;
if(consumed > 10) break;
System.out.println("J'ai acheté ma patisserie - Limited"); System.out.println("J'ai acheté ma patisserie - Limited");
try { try {
Thread.sleep(80); Thread.sleep(80);

@ -9,7 +9,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/ */
public class Patissier implements Runnable { public class Patissier implements Runnable {
public AtomicBoolean shouldRun = new AtomicBoolean(true); public AtomicBoolean shouldRun = new AtomicBoolean(true);
private final Boulangerie local; protected final Boulangerie local;
/** /**
* Default constructor * Default constructor
*/ */
@ -20,7 +20,9 @@ public class Patissier implements Runnable {
@Override @Override
public void run() { public void run() {
while(shouldRun.get()) { while(shouldRun.get()) {
local.depose(new Patisserie()); if(!local.depose(new Patisserie())){
shouldRun.set(false);
}
System.out.println("J'ai produit ma patisserie"); System.out.println("J'ai produit ma patisserie");
try { try {
Thread.sleep(100); Thread.sleep(100);

@ -0,0 +1,31 @@
package sweet;
import java.util.concurrent.atomic.AtomicBoolean;
/**
*
*/
public class PatissierSuicidal extends Patissier {
public AtomicBoolean shouldRun = new AtomicBoolean(true);
/**
* Default constructor
*/
public PatissierSuicidal(Boulangerie b) {
super(b);
}
@Override
public void run() {
while(shouldRun.get()) {
if(!local.depose(new Patisserie())){
shouldRun.set(false);
}
System.out.println("J'ai produit ma patisserie");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
}
}
}
Loading…
Cancel
Save