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();
Boulangerie b = new Boulangerie();
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.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 {
private final ArrayList<Patisserie> sweets = new ArrayList<>();
/**
*
*/
public synchronized void depose(Patisserie p) {
public synchronized boolean depose(Patisserie p) {
this.notify();
sweets.add(p);
return true;
}
/**
*
*/
public synchronized Patisserie achete() {
while(sweets.size() == 0) {
while(getStock() == 0) {
try {this.wait();} catch (InterruptedException ignored) {}}
Patisserie pat = sweets.get(0);
sweets.remove(pat);
return pat;
}
/**
*
*/
public void getStock() {
// TODO implement here
public int getStock(){
return sweets.size();
}
}

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

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

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

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

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