Compare commits
No commits in common. "a7216b1642591d0d777a10a34be1c4ffabb1bf8b" and "b3a0d1f7a1d5f3ab38cee40dfa4d03e19fb3265e" have entirely different histories.
a7216b1642
...
b3a0d1f7a1
@ -1,33 +0,0 @@
|
|||||||
package exercises.ex_j;
|
|
||||||
|
|
||||||
class Diner {
|
|
||||||
private String name;
|
|
||||||
private boolean isHungry;
|
|
||||||
|
|
||||||
public Diner(String name) {
|
|
||||||
this.name = name;
|
|
||||||
isHungry = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isHungry() {
|
|
||||||
return isHungry;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void eatWith(Spoon spoon, Diner spouse) {
|
|
||||||
while (isHungry) {
|
|
||||||
if (spoon.getOwner() == this) {
|
|
||||||
if (spouse.isHungry()) {
|
|
||||||
System.out.println(getName() + ": You eat first my darling " + spouse.getName() + "!");
|
|
||||||
spoon.setOwner(spouse);
|
|
||||||
} else {
|
|
||||||
spoon.use();
|
|
||||||
this.isHungry = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package exercises.ex_j;
|
|
||||||
|
|
||||||
public class LivelockDinner {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
final Diner husband = new Diner("Bob");
|
|
||||||
final Diner wife = new Diner("Alice");
|
|
||||||
final Spoon spoon = new Spoon(husband);
|
|
||||||
|
|
||||||
new Thread(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
husband.eatWith(spoon, wife);
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
new Thread(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
wife.eatWith(spoon, husband);
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package exercises.ex_j;
|
|
||||||
|
|
||||||
class Spoon {
|
|
||||||
private Diner owner;
|
|
||||||
|
|
||||||
public synchronized Diner getOwner() {
|
|
||||||
return owner;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Spoon(Diner d) {
|
|
||||||
owner = d;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void setOwner(Diner d) {
|
|
||||||
owner = d;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void use() {
|
|
||||||
System.out.printf("%s has eaten!", owner.getName());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
package exercises.ex_k;
|
|
||||||
|
|
||||||
public class Customer implements Runnable {
|
|
||||||
private String name;
|
|
||||||
private PostOffice postOffice;
|
|
||||||
|
|
||||||
public Customer(String name, PostOffice postOffice) {
|
|
||||||
this.name = name;
|
|
||||||
this.postOffice = postOffice;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Package pkg;
|
|
||||||
while (true) {
|
|
||||||
pkg = new Package(this);
|
|
||||||
System.out.println("Registering " + pkg + "...");
|
|
||||||
postOffice.registerPackage(pkg);
|
|
||||||
System.out.println("Registered " + pkg);
|
|
||||||
try {
|
|
||||||
Thread.sleep((long) (Math.random() * 2000 + 1000));
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package exercises.ex_k;
|
|
||||||
|
|
||||||
public class Package {
|
|
||||||
private Customer sender;
|
|
||||||
|
|
||||||
public Package(Customer sender) {
|
|
||||||
this.sender = sender;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Package{" +
|
|
||||||
"sender=" + sender.getName() +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package exercises.ex_k;
|
|
||||||
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class PostOffice {
|
|
||||||
public static final int CAPACITY = 5;
|
|
||||||
private List<Package> packages = new LinkedList<>();
|
|
||||||
|
|
||||||
public synchronized void registerPackage(Package pkg) {
|
|
||||||
while (packages.size() >= CAPACITY) {
|
|
||||||
try {
|
|
||||||
System.out.println("[PostOffice] Storage is full, waiting");
|
|
||||||
wait();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
packages.add(pkg);
|
|
||||||
System.out.println("[PostOffice] Storage: " + packages.size() + " / " + CAPACITY);
|
|
||||||
notifyAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized Package getNextPackage() {
|
|
||||||
while (packages.isEmpty()) {
|
|
||||||
try {
|
|
||||||
System.out.println("[PostOffice] Storage is empty, waiting");
|
|
||||||
wait();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Package pkg = packages.removeFirst();
|
|
||||||
System.out.println("[PostOffice] Storage: " + packages.size() + " / " + CAPACITY);
|
|
||||||
notifyAll();
|
|
||||||
return pkg;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package exercises.ex_k;
|
|
||||||
|
|
||||||
public class Postman implements Runnable {
|
|
||||||
private PostOffice postOffice;
|
|
||||||
|
|
||||||
public Postman(PostOffice postOffice) {
|
|
||||||
this.postOffice = postOffice;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Package pkg;
|
|
||||||
while (true) {
|
|
||||||
pkg = postOffice.getNextPackage();
|
|
||||||
System.out.println("Delivering " + pkg);
|
|
||||||
try {
|
|
||||||
Thread.sleep(2000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package exercises.ex_k;
|
|
||||||
|
|
||||||
public class TestingGuardedBlocks {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
PostOffice postOffice = new PostOffice();
|
|
||||||
(new Thread(new Postman(postOffice))).start();
|
|
||||||
(new Thread(new Customer("Mrs Darbellay", postOffice))).start();
|
|
||||||
(new Thread(new Customer("Mrs Müller", postOffice))).start();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package exercises.ex_n;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
public class News {
|
|
||||||
private String news;
|
|
||||||
private LocalDateTime date;
|
|
||||||
|
|
||||||
public News(String news) {
|
|
||||||
this.news = news;
|
|
||||||
this.date = LocalDateTime.now();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "News{" +
|
|
||||||
"news='" + news + '\'' +
|
|
||||||
", date=" + date +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
package exercises.ex_n;
|
|
||||||
|
|
||||||
import java.util.Stack;
|
|
||||||
import java.util.concurrent.locks.Lock;
|
|
||||||
import java.util.concurrent.locks.ReadWriteLock;
|
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
||||||
|
|
||||||
public class NewsFeed {
|
|
||||||
private final Stack<News> newsStack = new Stack<>();
|
|
||||||
private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
|
||||||
private final Lock writeLock = lock.writeLock();
|
|
||||||
private final Lock readLock = lock.readLock();
|
|
||||||
|
|
||||||
public NewsFeed() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void put(News news) {
|
|
||||||
try {
|
|
||||||
writeLock.lock();
|
|
||||||
newsStack.push(news);
|
|
||||||
} finally {
|
|
||||||
writeLock.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public News read() {
|
|
||||||
News news;
|
|
||||||
try {
|
|
||||||
readLock.lock();
|
|
||||||
news = newsStack.peek();
|
|
||||||
} finally {
|
|
||||||
readLock.unlock();
|
|
||||||
}
|
|
||||||
return news;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package exercises.ex_n;
|
|
||||||
|
|
||||||
public class TestingReentrantReadWriteLock_News {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
NewsFeed newsFeed = new NewsFeed();
|
|
||||||
newsFeed.put(new News("START OF NEWS"));
|
|
||||||
|
|
||||||
// Create one writing thread
|
|
||||||
new Thread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
for (int i=0;i<200;i++) {
|
|
||||||
newsFeed.put(new News("News " + i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
|
|
||||||
// Create several reading threads
|
|
||||||
for (int i=0; i<20;i++) {
|
|
||||||
new Thread((new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
for (int i=0;i<20;i++) {
|
|
||||||
News news = newsFeed.read();
|
|
||||||
if (news != null) {
|
|
||||||
System.out.println("News read : "
|
|
||||||
+ newsFeed.read());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})).start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,51 +0,0 @@
|
|||||||
package exercises.ex_o;
|
|
||||||
|
|
||||||
import java.util.Stack;
|
|
||||||
import java.util.concurrent.locks.Condition;
|
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
|
||||||
|
|
||||||
public class LockedWithConditionsStack {
|
|
||||||
private final int capacity;
|
|
||||||
private final Stack<Integer> stack = new Stack<>();
|
|
||||||
private final ReentrantLock lock = new ReentrantLock();
|
|
||||||
private final Condition fullCondition = lock.newCondition();
|
|
||||||
private final Condition emptyCondition = lock.newCondition();
|
|
||||||
|
|
||||||
public LockedWithConditionsStack(int capacity) {
|
|
||||||
this.capacity = capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void pushToStack(int value) {
|
|
||||||
try {
|
|
||||||
lock.lock();
|
|
||||||
while (stack.size() == capacity) {
|
|
||||||
System.out.println("Stack is full, waiting until someone pops");
|
|
||||||
fullCondition.await();
|
|
||||||
}
|
|
||||||
stack.push(value);
|
|
||||||
emptyCondition.signalAll();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
lock.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int popFromStack() {
|
|
||||||
int value;
|
|
||||||
try {
|
|
||||||
lock.lock();
|
|
||||||
while (stack.isEmpty()) {
|
|
||||||
System.out.println("Stack is empty, waiting until someone pushes");
|
|
||||||
emptyCondition.await();
|
|
||||||
}
|
|
||||||
value = stack.pop();
|
|
||||||
fullCondition.signalAll();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
lock.unlock();
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
package exercises.ex_o;
|
|
||||||
|
|
||||||
public class NumberConsumer implements Runnable {
|
|
||||||
private LockedWithConditionsStack stack;
|
|
||||||
|
|
||||||
public NumberConsumer(LockedWithConditionsStack stack) {
|
|
||||||
this.stack = stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
while (true) {
|
|
||||||
System.out.println("Pop from stack: " + stack.popFromStack());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package exercises.ex_o;
|
|
||||||
|
|
||||||
public class NumberProducer implements Runnable {
|
|
||||||
private LockedWithConditionsStack stack;
|
|
||||||
public NumberProducer(LockedWithConditionsStack stack) {
|
|
||||||
this.stack = stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
for (int i = 0; i < 100; i++) {
|
|
||||||
System.out.println("Pushing to stack " + i + "...");
|
|
||||||
stack.pushToStack(i);
|
|
||||||
System.out.println("Pushed to stack " + i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package exercises.ex_o;
|
|
||||||
|
|
||||||
public class TestingReentrantLockWithConditions {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
LockedWithConditionsStack stack = new LockedWithConditionsStack(5);
|
|
||||||
(new Thread(new NumberProducer(stack))).start();
|
|
||||||
(new Thread(new NumberConsumer(stack))).start();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package exercises.ex_p;
|
|
||||||
|
|
||||||
public class Drum {
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
public Drum(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void play() {
|
|
||||||
System.out.println("Playing " + name);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package exercises.ex_p;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
public class Partition {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Drum bassDrum = new Drum("Bass Drum");
|
|
||||||
Drum floortom = new Drum("Floor Tom");
|
|
||||||
Drum snareDrum = new Drum("Snare Drum");
|
|
||||||
Drum rackTom = new Drum("Rack Tom");
|
|
||||||
Drum hiHat = new Drum("Hi-Hat");
|
|
||||||
Drum crashCymbal = new Drum("Crash Cymbal");
|
|
||||||
Drum rideCymbal = new Drum("Ride Cymbal");
|
|
||||||
Drum splashCymbal = new Drum("Splash Cymbal");
|
|
||||||
Drum chinaCymbal = new Drum("China Cymbal");
|
|
||||||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(9);
|
|
||||||
int initialDelay = 1000;
|
|
||||||
int quaver = 500; // croche
|
|
||||||
int crotchet = 1000; // noire
|
|
||||||
int minim = 2000; // blanche
|
|
||||||
TimeUnit unit = TimeUnit.MILLISECONDS;
|
|
||||||
|
|
||||||
Drum[] drums = {
|
|
||||||
bassDrum, floortom, snareDrum,
|
|
||||||
rackTom, hiHat, crashCymbal,
|
|
||||||
rideCymbal, splashCymbal, chinaCymbal
|
|
||||||
};
|
|
||||||
int[] notes = {quaver, crotchet, minim};
|
|
||||||
|
|
||||||
for (int i=0; i < 9; i++) {
|
|
||||||
Drum drum = drums[i];
|
|
||||||
int rhythm = notes[(int) (Math.random() * 3)];
|
|
||||||
executor.scheduleAtFixedRate(drum::play, initialDelay, rhythm, unit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package exercises.ex_p_bis;
|
|
||||||
|
|
||||||
public class Ansi {
|
|
||||||
public static final String RESET = "\u001B[0m";
|
|
||||||
public static final String BLACK = "\u001B[30m";
|
|
||||||
public static final String RED = "\u001B[31m";
|
|
||||||
public static final String GREEN = "\u001B[32m";
|
|
||||||
public static final String YELLOW = "\u001B[33m";
|
|
||||||
public static final String BLUE = "\u001B[34m";
|
|
||||||
public static final String PURPLE = "\u001B[35m";
|
|
||||||
public static final String CYAN = "\u001B[36m";
|
|
||||||
public static final String WHITE = "\u001B[37m";
|
|
||||||
}
|
|
@ -1,51 +0,0 @@
|
|||||||
package exercises.ex_p_bis;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ChristmasTree {
|
|
||||||
private static final String[] lines = {
|
|
||||||
" ★ ★ ★",
|
|
||||||
" ★ ╱╲ ",
|
|
||||||
" ★ ╱ ╲ ★ ",
|
|
||||||
" ╱ ╲ ",
|
|
||||||
" ★ ╱ ╲ ★ ",
|
|
||||||
" ╱ ╲ ",
|
|
||||||
" ╱─ ─╲ ",
|
|
||||||
" ╱ ╲ ★ ",
|
|
||||||
" ★ ╱ ╲ ",
|
|
||||||
" ╱────┐ ┌────╲ ",
|
|
||||||
" ★ │ │ ★ ",
|
|
||||||
};
|
|
||||||
public static final int W = 20;
|
|
||||||
public static final int H = 11;
|
|
||||||
private final Map<Integer, Light> lights = new HashMap<>();
|
|
||||||
|
|
||||||
public void addLight(int x, int y, Light light) {
|
|
||||||
lights.put(x + y * W, light);
|
|
||||||
light.setTree(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void show() {
|
|
||||||
String result = "";
|
|
||||||
for (int y=0; y < H; y++) {
|
|
||||||
String line = "";
|
|
||||||
for (int x=0; x < W; x++) {
|
|
||||||
String c = String.valueOf(lines[y].charAt(x));
|
|
||||||
if (c.equals("★")) {
|
|
||||||
c = Ansi.YELLOW + c + Ansi.RESET;
|
|
||||||
}
|
|
||||||
Light light = lights.get(x + y * W);
|
|
||||||
if (light != null) {
|
|
||||||
c = light.apply(c);
|
|
||||||
}
|
|
||||||
line += c;
|
|
||||||
}
|
|
||||||
if (y != 0) {
|
|
||||||
result += "\n";
|
|
||||||
}
|
|
||||||
result += line;
|
|
||||||
}
|
|
||||||
System.out.println(result);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
package exercises.ex_p_bis;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
public class ChristmasTreeTester {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
ChristmasTree tree = new ChristmasTree();
|
|
||||||
|
|
||||||
Light light1 = new Light(Ansi.RED);
|
|
||||||
Light light2 = new Light(Ansi.RED);
|
|
||||||
Light light3 = new Light(Ansi.GREEN);
|
|
||||||
Light light4 = new Light(Ansi.GREEN);
|
|
||||||
Light light5 = new Light(Ansi.WHITE);
|
|
||||||
Light light6 = new Light(Ansi.WHITE);
|
|
||||||
|
|
||||||
tree.addLight(8, 3, light1);
|
|
||||||
tree.addLight(11, 5, light2);
|
|
||||||
tree.addLight(13, 6, light3);
|
|
||||||
tree.addLight(6, 7, light4);
|
|
||||||
tree.addLight(10, 2, light5);
|
|
||||||
tree.addLight(12, 8, light6);
|
|
||||||
|
|
||||||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
|
|
||||||
Light[] lights = {light1, light2, light3, light4, light5, light6};
|
|
||||||
for (int i = 0; i < 6; i++) {
|
|
||||||
Light light = lights[i];
|
|
||||||
executor.scheduleAtFixedRate(light::toggle, 0, (long) (1000 + Math.random() * 2000), TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
package exercises.ex_p_bis;
|
|
||||||
|
|
||||||
public class Light {
|
|
||||||
private boolean isOn = false;
|
|
||||||
private String prefix = "";
|
|
||||||
private static final String suffix = Ansi.RESET;
|
|
||||||
private ChristmasTree tree;
|
|
||||||
|
|
||||||
public Light(String prefix) {
|
|
||||||
this.prefix = prefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void toggle() {
|
|
||||||
isOn = !isOn;
|
|
||||||
tree.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTree(ChristmasTree tree) {
|
|
||||||
this.tree = tree;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String apply(String c) {
|
|
||||||
if (isOn) {
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
if (c.equals(" ")) {
|
|
||||||
c = "⬤";
|
|
||||||
}
|
|
||||||
return prefix + c + suffix;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package exercises.ex_q1;
|
|
||||||
|
|
||||||
public class Pizza {
|
|
||||||
public void prepare() {
|
|
||||||
System.out.println("Preparing pizza.");
|
|
||||||
}
|
|
||||||
public void bake() {
|
|
||||||
System.out.println("Baking pizza.");
|
|
||||||
}
|
|
||||||
public void cut() {
|
|
||||||
System.out.println("Cutting pizza.");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package exercises.ex_q1;
|
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
public class Pizzaiolo implements Callable<Pizza> {
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
public Pizzaiolo(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void serve(Pizza pizza) {
|
|
||||||
System.out.println(name + "'s pizza is ready: pizza " + pizza.hashCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Pizza makePizza() throws InterruptedException {
|
|
||||||
Pizza pizza = new Pizza();
|
|
||||||
pizza.prepare();
|
|
||||||
Thread.sleep(200);
|
|
||||||
pizza.bake();
|
|
||||||
Thread.sleep(300);
|
|
||||||
pizza.cut();
|
|
||||||
Thread.sleep(100);
|
|
||||||
return pizza;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Pizza call() throws Exception {
|
|
||||||
System.out.println("Thread " + name + " started by the executor");
|
|
||||||
Pizza pizza = makePizza();
|
|
||||||
System.out.println("Thread " + name + " pizza finished");
|
|
||||||
return pizza;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package exercises.ex_q1;
|
|
||||||
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
|
|
||||||
public class Pizzeria {
|
|
||||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
|
||||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
||||||
Pizzaiolo marco = new Pizzaiolo("Marco");
|
|
||||||
Pizzaiolo antonio = new Pizzaiolo("Antonio");
|
|
||||||
|
|
||||||
Future<Pizza> future1 = executor.submit(marco);
|
|
||||||
Future<Pizza> future2 = executor.submit(antonio);
|
|
||||||
Future<Pizza> future3 = executor.submit(marco);
|
|
||||||
Future<Pizza> future4 = executor.submit(antonio);
|
|
||||||
|
|
||||||
marco.serve(future1.get());
|
|
||||||
antonio.serve(future2.get());
|
|
||||||
marco.serve(future3.get());
|
|
||||||
antonio.serve(future4.get());
|
|
||||||
|
|
||||||
executor.shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package exercises.ex_q2;
|
|
||||||
|
|
||||||
import java.util.concurrent.RecursiveAction;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
public class CustomRecursiveAction extends RecursiveAction {
|
|
||||||
private static final int threshold = 10;
|
|
||||||
private String song;
|
|
||||||
|
|
||||||
public CustomRecursiveAction(String song) {
|
|
||||||
this.song = song;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void compute() {
|
|
||||||
Logger logger = Logger.getAnonymousLogger();
|
|
||||||
if (song.length() <= threshold) {
|
|
||||||
logger.info("Splitting problem");
|
|
||||||
changeToUppercase();
|
|
||||||
} else {
|
|
||||||
logger.info("Problem is small enough");
|
|
||||||
int halfSize = song.length() / 2;
|
|
||||||
CustomRecursiveAction task1 = new CustomRecursiveAction(song.substring(0, halfSize));
|
|
||||||
CustomRecursiveAction task2 = new CustomRecursiveAction(song.substring(halfSize));
|
|
||||||
invokeAll(task1, task2);
|
|
||||||
song = task1.song + task2.song;
|
|
||||||
logger.info("Solution: " + song);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void changeToUppercase() {
|
|
||||||
song = song.toUpperCase();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
package exercises.ex_q2;
|
|
||||||
|
|
||||||
import java.util.concurrent.ForkJoinPool;
|
|
||||||
|
|
||||||
public class ForkJoinTest {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String song = "Georges Brassens (1964) - Les Copains d'abord"
|
|
||||||
+ "Non, ce n'était pas le radeau"
|
|
||||||
+ "De la Méduse, ce bateau"
|
|
||||||
+ "Qu'on se le dise au fond des ports"
|
|
||||||
+ "Dise au fond des ports"
|
|
||||||
+ "Il naviguait en père peinard"
|
|
||||||
+ "Sur la grand-mare des canards"
|
|
||||||
+ "Et s'appelait les Copains d'abord"
|
|
||||||
+ "Les Copains d'abord";
|
|
||||||
int processors = Runtime.getRuntime().availableProcessors();
|
|
||||||
System.out.println("Number of available processors: " + processors);
|
|
||||||
ForkJoinPool pool = new ForkJoinPool(processors);
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
pool.invoke(new CustomRecursiveAction(song));
|
|
||||||
long endTime = System.currentTimeMillis();
|
|
||||||
System.out.println(
|
|
||||||
"Fork/Join tasks took "
|
|
||||||
+ (endTime - startTime)
|
|
||||||
+ " milliseconds."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package exercises.ex_q3;
|
|
||||||
|
|
||||||
public record Book(String ISBN, String title, String authorFirstName, String authorLastName, int year) implements Comparable<Book> {
|
|
||||||
@Override
|
|
||||||
public int compareTo(Book o) {
|
|
||||||
return title.compareTo(o.title);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,67 +0,0 @@
|
|||||||
package exercises.ex_q3;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class BookGenerator {
|
|
||||||
private static final String[] adjectives = {
|
|
||||||
"big", "red", "small", "weird", "amazing", "fantastic", "friendly", "curious", "frightening",
|
|
||||||
};
|
|
||||||
private static final String[] nouns = {
|
|
||||||
"apple", "car", "gingerbread man", "octopus", "planet", "volcano", "bottle", "adventure"
|
|
||||||
};
|
|
||||||
private static final String[] determinants = {"the", "a"};
|
|
||||||
private static final String vowels = "aeiou";
|
|
||||||
|
|
||||||
private static final String[] firstnames = {
|
|
||||||
"John", "Patrick", "Gérard", "Jean-Michel", "Ronswalle", "Guilherme", "Jean-Claude", "Perceval", "Hector", "Ferguson",
|
|
||||||
"Azénor", "Clémentine", "Ailizabette", "Bérangère", "Clothilde", "Mélissandre"
|
|
||||||
};
|
|
||||||
|
|
||||||
private static final String[] lastnames = {
|
|
||||||
"de Mont-Castel", "Smith", "Martin", "Ferguson", "Lassalle", "Berset", "Constantin"
|
|
||||||
};
|
|
||||||
|
|
||||||
private static final Random random = new Random();
|
|
||||||
|
|
||||||
private static String getRandom(String[] list) {
|
|
||||||
int i = random.nextInt(list.length);
|
|
||||||
return list[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Book generateRandomBook() {
|
|
||||||
String noun = getRandom(nouns);
|
|
||||||
String adj = getRandom(adjectives);
|
|
||||||
String det = getRandom(determinants);
|
|
||||||
if (det.equals("a") && vowels.contains(adj.substring(0, 1))) {
|
|
||||||
det += "n";
|
|
||||||
}
|
|
||||||
String title = det + " " + adj + " " + noun;
|
|
||||||
title = title.substring(0, 1).toUpperCase() + title.substring(1);
|
|
||||||
|
|
||||||
String isbn = "";
|
|
||||||
int checksum = 0;
|
|
||||||
for (int i = 0; i < 9; i++) {
|
|
||||||
int digit = random.nextInt(10);
|
|
||||||
isbn += String.valueOf(digit);
|
|
||||||
if (i % 4 == 0) {
|
|
||||||
isbn += "-";
|
|
||||||
}
|
|
||||||
checksum += (10 - i) * digit;
|
|
||||||
}
|
|
||||||
checksum = checksum % 11;
|
|
||||||
if (checksum != 0) {
|
|
||||||
checksum = 11 - checksum;
|
|
||||||
}
|
|
||||||
if (checksum == 10) {
|
|
||||||
isbn += "X";
|
|
||||||
} else {
|
|
||||||
isbn += String.valueOf(checksum);
|
|
||||||
}
|
|
||||||
|
|
||||||
String firstname = getRandom(firstnames);
|
|
||||||
String lastname = getRandom(lastnames);
|
|
||||||
|
|
||||||
return new Book(isbn, title, firstname, lastname, random.nextInt(1000, 2025));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package exercises.ex_q3;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public class Library {
|
|
||||||
private static final int SIZE = 100_000;
|
|
||||||
private static final Book[] books = new Book[SIZE];
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
for (int i = 0; i < SIZE; i++) {
|
|
||||||
Book book = BookGenerator.generateRandomBook();
|
|
||||||
books[i] = book;
|
|
||||||
}
|
|
||||||
|
|
||||||
Arrays.parallelSort(books);
|
|
||||||
for (int i = 0; i < Math.min(15, SIZE); i++) {
|
|
||||||
System.out.println(books[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
package exercises.ex_r;
|
|
||||||
|
|
||||||
public class Customer implements Runnable {
|
|
||||||
private String name;
|
|
||||||
private PostOffice office;
|
|
||||||
|
|
||||||
public Customer(String name, PostOffice office) {
|
|
||||||
this.name = name;
|
|
||||||
this.office = office;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
while (true) {
|
|
||||||
Package pkg = new Package(this);
|
|
||||||
office.registerPackage(pkg);
|
|
||||||
try {
|
|
||||||
Thread.sleep((long) (1000L + 5000L * Math.random()));
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package exercises.ex_r;
|
|
||||||
|
|
||||||
public class Package {
|
|
||||||
private Customer sender;
|
|
||||||
public Package(Customer sender) {
|
|
||||||
this.sender = sender;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Package{" +
|
|
||||||
"sender=" + sender.getName() +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package exercises.ex_r;
|
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
|
||||||
|
|
||||||
public class PostOffice {
|
|
||||||
private BlockingQueue<Package> packages = new LinkedBlockingQueue<>();
|
|
||||||
|
|
||||||
public void registerPackage(Package pkg) {
|
|
||||||
packages.add(pkg);
|
|
||||||
System.out.println("Registered " + pkg + " (now " + packages.size() + ")");
|
|
||||||
}
|
|
||||||
|
|
||||||
public Package takePackage() throws InterruptedException {
|
|
||||||
Package pkg = packages.take();
|
|
||||||
System.out.println("Withdrawn " + pkg + " (now " + packages.size() + ")");
|
|
||||||
return pkg;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
package exercises.ex_r;
|
|
||||||
|
|
||||||
public class Postman implements Runnable {
|
|
||||||
private PostOffice office;
|
|
||||||
|
|
||||||
public Postman(PostOffice office) {
|
|
||||||
this.office = office;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
Package pkg = office.takePackage();
|
|
||||||
Thread.sleep((long) (1000L + 2000L * Math.random()));
|
|
||||||
System.out.println("Delivered " + pkg);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package exercises.ex_r;
|
|
||||||
|
|
||||||
public class TestingBlockingQueue_PostOffice {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
PostOffice postOffice = new PostOffice();
|
|
||||||
(new Thread(new Postman(postOffice))).start();
|
|
||||||
(new Thread(new Customer("Mrs Darbellay", postOffice))).start();
|
|
||||||
(new Thread(new Customer("Mrs Müller", postOffice))).start();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user