Compare commits
12 Commits
a7216b1642
...
main
Author | SHA1 | Date | |
---|---|---|---|
b5540fc68d
|
|||
fd3af32c7c
|
|||
886bf5a1cd
|
|||
afc2fbfa8a
|
|||
c7d054e973
|
|||
fe3d1aac35
|
|||
e959cdd466
|
|||
1cfc40afed
|
|||
28ab30e735
|
|||
c89a9b3a90
|
|||
9be4e8a359
|
|||
dbd83223fe
|
124
.idea/uiDesigner.xml
generated
Normal file
124
.idea/uiDesigner.xml
generated
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Palette2">
|
||||||
|
<group name="Swing">
|
||||||
|
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Button" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="RadioButton" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="CheckBox" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Label" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||||
|
<preferred-size width="-1" height="20" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
</group>
|
||||||
|
</component>
|
||||||
|
</project>
|
80
README.md
Normal file
80
README.md
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
# Concurrent Programming
|
||||||
|
|
||||||
|
This repository holds all my answers to the exercises for the 202.2 Concurrent Programming course
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> Some exercises may not be complete, although most of them are fully functioning
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
* [Exercises](#exercises)
|
||||||
|
* [Lab 01 - Thread Objects and Synchronization](#lab-01---thread-objects-and-synchronization)
|
||||||
|
* [Lab 01 (bis) - Thread Objects and Synchronization](#lab-01-bis---thread-objects-and-synchronization)
|
||||||
|
* [Lab 02 - Liveness, Guarded Blocks and Immutable Objects](#lab-02---liveness-guarded-blocks-and-immutable-objects)
|
||||||
|
* [Lab 02 (bis) - Liveness, Guarded Blocks and Immutable Objects](#lab-02-bis---liveness-guarded-blocks-and-immutable-objects)
|
||||||
|
* [Lab 03 - Lock Objects](#lab-03---lock-objects)
|
||||||
|
* [Lab 03 (bis) - Lock Objects](#lab-03-bis---lock-objects)
|
||||||
|
* [Lab 04 - Executors, Callable and Fork-Join](#lab-04---executors-callable-and-fork-join)
|
||||||
|
* [Lab 05 - Concurrent Collections and Atomic Variables](#lab-05---concurrent-collections-and-atomic-variables)
|
||||||
|
* [Lab 06 - Synchronizers](#lab-06---synchronizers)
|
||||||
|
* [Lab 07 (Workshop) - Summarizing exercises](#lab-07-workshop---summarizing-exercises)
|
||||||
|
|
||||||
|
## Exercises
|
||||||
|
### Lab 01 - Thread Objects and Synchronization
|
||||||
|
- [A: Defining and Starting A Thread](src/exercises/ex_a/TestingSimpleThreads.java)
|
||||||
|
- [B: Pausing Execution With Sleep](src/exercises/ex_b/TestingSleep.java)
|
||||||
|
- [C: Interruptions](src/exercises/ex_c/TestingSleep.java)
|
||||||
|
- [D: Joins](src/exercises/ex_d/TestingJoins.java)
|
||||||
|
- [E: Thread Interference: Managing Bank Accounts](src/exercises/ex_e/TestingThreadInterferences.java)
|
||||||
|
- [F: Synchronized Methods: Ensuring consistent operations on bank accounts](src/exercises/ex_f/TestingThreadInterferences.java)
|
||||||
|
- [G: Synchronized Statements](src/exercises/ex_g/TestingSynchronizedStatements.java)
|
||||||
|
|
||||||
|
### Lab 01 (bis) - Thread Objects and Synchronization
|
||||||
|
- [Dbis: Joins](src/exercises/ex_d_bis/TestStepThreads.java)
|
||||||
|
|
||||||
|
### Lab 02 - Liveness, Guarded Blocks and Immutable Objects
|
||||||
|
- [H1: Refactoring from Deadlocks](src/exercises/ex_h1/TestThreadWithDeadlock.java)
|
||||||
|
- [H2: Bridge crossing without deadlock](src/exercises/ex_h2/TestDeadlockBridge.java)
|
||||||
|
- [I: Starvation: simulating the access of employees to a conference room](src/exercises/ex_i/TestingStarvation.java)
|
||||||
|
- [J: Livelock : couple eating](src/exercises/ex_j/LivelockDinner.java)
|
||||||
|
- [K: Guarded Blocks : managing packages in a post office](src/exercises/ex_k/TestingGuardedBlocks.java)
|
||||||
|
- ~~L: Immutable objects: From person to statue~~
|
||||||
|
|
||||||
|
### Lab 02 (bis) - Liveness, Guarded Blocks and Immutable Objects
|
||||||
|
- [Kbis1: Guarded Blocks : a service desk](src/exercises/ex_k_bis1/TestServiceDesk.java)
|
||||||
|
- ~~Kbis2: Playing Tennis~~
|
||||||
|
|
||||||
|
### Lab 03 - Lock Objects
|
||||||
|
- ~~M: ReentrantLock: Sharing a Vehicle in a couple~~
|
||||||
|
- [N: ReentrantReadWriteLock: News Feed](src/exercises/ex_n/TestingReentrantReadWriteLock_News.java)
|
||||||
|
- [O: ReentrantLockWithConditions: NumberProducersConsumers](src/exercises/ex_o/TestingReentrantLockWithConditions.java)
|
||||||
|
|
||||||
|
### Lab 03 (bis) - Lock Objects
|
||||||
|
- [Nbis: ReentrantReadWriteLock: Managing a library catalog system](src/exercises/ex_n_bis/LibraryCatalog.java)
|
||||||
|
|
||||||
|
### Lab 04 - Executors, Callable and Fork-Join
|
||||||
|
- [P: Executors for Drums](src/exercises/ex_p/Partition.java)
|
||||||
|
- [Pbis: Christmas Lights Task Scheduler](src/exercises/ex_p_bis/ChristmasTreeTester.java)
|
||||||
|
- [Q1: Callables and Futures for a Pizzeria](src/exercises/ex_q1/Pizzeria.java)
|
||||||
|
- [Q2: Fork/Join: Song](src/exercises/ex_q2/ForkJoinTest.java)
|
||||||
|
- [Q3: Arrays.parallelSort](src/exercises/ex_q3/Library.java)
|
||||||
|
- ~~Q4: Concurrent Array Sum Calculation~~
|
||||||
|
|
||||||
|
### Lab 05 - Concurrent Collections and Atomic Variables
|
||||||
|
- [R: BlockingQueue: Simulating a PostOffice](src/exercises/ex_r/TestingBlockingQueue_PostOffice.java)
|
||||||
|
- [S1: ConcurrentHashMap in the Farm](src/exercises/ex_s1/TestingConcurrentHashMap_Farm.java)
|
||||||
|
- [S2: Comparing Atomic Arrays and Standard Arrays](src/exercises/ex_s2/TestingAtomicInteger.java)
|
||||||
|
- [S3: java.util.concurrent.atomic package: Creativity](src/exercises/ex_s3/TestingAtomicLong.java)
|
||||||
|
|
||||||
|
### Lab 06 - Synchronizers
|
||||||
|
- [T: Semaphore for Dog Feeding](src/exercises/ex_t/TestingSemaphore_DogBreeder.java)
|
||||||
|
- [U: CountDownLatch: Starting the Family Trip](src/exercises/ex_u/TestingCountDownLatch_FamilyTrip.java)
|
||||||
|
- [V: CyclicBarrier: Patrouille de Glaciers](src/exercises/ex_v/TestingCyclicBarrier_PDG.java)
|
||||||
|
- [W: Exchanger: Bring my Car to the Garage](src/exercises/ex_w/TestingExchanger_Garage.java)
|
||||||
|
- [X: Phaser for the Next Family Bike Trip](src/exercises/ex_x/TestingPhaserFamilyBikeTrip.java)
|
||||||
|
- [y: Synchronized Race (former exam)](src/exercises/ex_y/RaceTester.java)
|
||||||
|
|
||||||
|
### Lab 07 (Workshop) - Summarizing exercises
|
||||||
|
- ~~Z1: The dining philosophers~~
|
||||||
|
- ~~Z2: The Sleeping Barber Problem~~
|
||||||
|
- ~~Z3: Print Queue Management~~
|
||||||
|
- [Z4: Concurrent Auction Problem](src/exercises/ex_z4/AuctionMain.java)
|
7
src/exercises/Utils.java
Normal file
7
src/exercises/Utils.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package exercises;
|
||||||
|
|
||||||
|
public class Utils {
|
||||||
|
public static void randomSleep(long min, long max) throws InterruptedException {
|
||||||
|
Thread.sleep((long) (min + Math.random() * max));
|
||||||
|
}
|
||||||
|
}
|
17
src/exercises/ex_k_bis1/Customer.java
Normal file
17
src/exercises/ex_k_bis1/Customer.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package exercises.ex_k_bis1;
|
||||||
|
|
||||||
|
public class Customer implements Runnable {
|
||||||
|
/*private String name;
|
||||||
|
private ServiceDesk serviceDesk;
|
||||||
|
|
||||||
|
public Customer(String name, ServiceDesk serviceDesk) {
|
||||||
|
this.name = name;
|
||||||
|
this.serviceDesk = serviceDesk;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
22
src/exercises/ex_k_bis1/TestServiceDesk.java
Normal file
22
src/exercises/ex_k_bis1/TestServiceDesk.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package exercises.ex_k_bis1;
|
||||||
|
|
||||||
|
public class TestServiceDesk {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
/*
|
||||||
|
ServiceDesk serviceDesk = new ServiceDesk(2);
|
||||||
|
for (int j = 0; j<3; j++) {
|
||||||
|
new Thread(new ServiceRepresentative(serviceDesk), "Employee-"+ j).start();
|
||||||
|
}
|
||||||
|
// Start customer threads
|
||||||
|
for (int i = 0; i < 10; i++) { // 30 customers
|
||||||
|
String name = "Customer-" + i;
|
||||||
|
new Thread(new Customer(name, serviceDesk)).start();
|
||||||
|
try {
|
||||||
|
Thread.sleep((int) (Math.random() * 1000)); // Random arrival
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
75
src/exercises/ex_n_bis/LibraryCatalog.java
Normal file
75
src/exercises/ex_n_bis/LibraryCatalog.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package exercises.ex_n_bis;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
|
|
||||||
|
public class LibraryCatalog {
|
||||||
|
private final Map<String, String> catalog = new HashMap<>();
|
||||||
|
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
|
||||||
|
private final Lock readLock = lock.readLock();
|
||||||
|
private final Lock writeLock = lock.writeLock();
|
||||||
|
|
||||||
|
public LibraryCatalog() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Book> getBooks() {
|
||||||
|
ArrayList<Book> books = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
readLock.lock();
|
||||||
|
catalog.forEach((title, author) -> {
|
||||||
|
books.add(new Book(title, author));
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
readLock.unlock();
|
||||||
|
}
|
||||||
|
return books;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String findBook(String title) {
|
||||||
|
String author;
|
||||||
|
try {
|
||||||
|
readLock.lock();
|
||||||
|
author = catalog.get(title);
|
||||||
|
} finally {
|
||||||
|
readLock.unlock();
|
||||||
|
}
|
||||||
|
if (author == null) {
|
||||||
|
System.out.println("The book '" + title + "' is not in the catalog");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addBook(String title, String author) {
|
||||||
|
try {
|
||||||
|
writeLock.lock();
|
||||||
|
catalog.put(title, author);
|
||||||
|
} finally {
|
||||||
|
writeLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeBook(String title) {
|
||||||
|
try {
|
||||||
|
writeLock.lock();
|
||||||
|
catalog.remove(title);
|
||||||
|
} finally {
|
||||||
|
writeLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateBook(String title, String newAuthor) {
|
||||||
|
try {
|
||||||
|
writeLock.lock();
|
||||||
|
catalog.replace(title, newAuthor);
|
||||||
|
} finally {
|
||||||
|
writeLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public record Book(String title, String author) {}
|
||||||
|
}
|
33
src/exercises/ex_s1/Cow.java
Normal file
33
src/exercises/ex_s1/Cow.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package exercises.ex_s1;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class Cow implements Runnable {
|
||||||
|
private String name;
|
||||||
|
private ConcurrentHashMap<String, Integer> stalls;
|
||||||
|
private final Random random = new Random();
|
||||||
|
|
||||||
|
public Cow(String name, ConcurrentHashMap<String, Integer> stalls) {
|
||||||
|
this.name = name;
|
||||||
|
this.stalls = stalls;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
Thread.sleep((long) (1000L + Math.random() * 5000L));
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
String key = "box" + random.nextInt(1, 4);
|
||||||
|
Integer units = stalls.replace(key, 0);
|
||||||
|
if (units == null || units == 0) {
|
||||||
|
System.out.println(name + " ate nothing");
|
||||||
|
} else {
|
||||||
|
System.out.println(name + " ate " + units + " units of food from " + key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
src/exercises/ex_s1/Farmer.java
Normal file
30
src/exercises/ex_s1/Farmer.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package exercises.ex_s1;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class Farmer implements Runnable {
|
||||||
|
private String name;
|
||||||
|
private ConcurrentHashMap<String, Integer> stalls;
|
||||||
|
private final Random random = new Random();
|
||||||
|
|
||||||
|
public Farmer(String name, ConcurrentHashMap<String, Integer> stalls) {
|
||||||
|
this.name = name;
|
||||||
|
this.stalls = stalls;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
Thread.sleep((long) (1000L + Math.random() * 2000L));
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
String key = "box" + random.nextInt(1, 4);
|
||||||
|
int units = random.nextInt(10, 100);
|
||||||
|
stalls.put(key, units);
|
||||||
|
System.out.println(name + " puts " + units + " units of food in " + key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
src/exercises/ex_s1/TestingConcurrentHashMap_Farm.java
Normal file
14
src/exercises/ex_s1/TestingConcurrentHashMap_Farm.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package exercises.ex_s1;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class TestingConcurrentHashMap_Farm {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ConcurrentHashMap<String, Integer> animalStallMap = new ConcurrentHashMap<>();
|
||||||
|
new Thread(new Farmer("Verena", animalStallMap)).start();
|
||||||
|
new Thread(new Farmer("Pierre", animalStallMap)).start();
|
||||||
|
for (int i = 1; i < 10; i++) {
|
||||||
|
new Thread(new Cow("Cow" + i, animalStallMap)).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
42
src/exercises/ex_s2/TestingAtomicInteger.java
Normal file
42
src/exercises/ex_s2/TestingAtomicInteger.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package exercises.ex_s2;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||||
|
|
||||||
|
public class TestingAtomicInteger {
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
int[] nonAtomic = new int[5];
|
||||||
|
AtomicIntegerArray atomic = new AtomicIntegerArray(5);
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
nonAtomic[i] = i;
|
||||||
|
atomic.set(i, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Starting values atomic array: " + atomic);
|
||||||
|
System.out.println("Starting values of non atomic array: " + Arrays.toString(nonAtomic));
|
||||||
|
System.out.println("Creating 3 threads to increment the values within those two arrays");
|
||||||
|
|
||||||
|
Thread[] threads = new Thread[3];
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
threads[i] = new Thread(() -> {
|
||||||
|
for (int j = 0; j < 100000; j++) {
|
||||||
|
for (int k = 0; k < 5; k++) {
|
||||||
|
nonAtomic[k] += 1;
|
||||||
|
atomic.incrementAndGet(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
threads[i].start();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
threads[i].join();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Final values atomic array: " + atomic);
|
||||||
|
System.out.println("Final values of non atomic array: " + Arrays.toString(nonAtomic));
|
||||||
|
}
|
||||||
|
}
|
20
src/exercises/ex_s3/AbstractTweet.java
Normal file
20
src/exercises/ex_s3/AbstractTweet.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package exercises.ex_s3;
|
||||||
|
|
||||||
|
public abstract class AbstractTweet {
|
||||||
|
private String author;
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
public AbstractTweet(String author, String content) {
|
||||||
|
this.author = author;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void addLike();
|
||||||
|
public abstract void removeLike();
|
||||||
|
public abstract long getLikes();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "@" + author + " tweeted \"" + content + "\" (" + getLikes() + " likes)";
|
||||||
|
}
|
||||||
|
}
|
27
src/exercises/ex_s3/AtomicTweet.java
Normal file
27
src/exercises/ex_s3/AtomicTweet.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package exercises.ex_s3;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
public class AtomicTweet extends AbstractTweet {
|
||||||
|
|
||||||
|
private AtomicLong likes = new AtomicLong(0);
|
||||||
|
|
||||||
|
public AtomicTweet(String author, String content) {
|
||||||
|
super(author, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addLike() {
|
||||||
|
likes.incrementAndGet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeLike() {
|
||||||
|
likes.updateAndGet(i -> i > 0 ? i - 1 : i);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getLikes() {
|
||||||
|
return likes.get();
|
||||||
|
}
|
||||||
|
}
|
32
src/exercises/ex_s3/TestingAtomicLong.java
Normal file
32
src/exercises/ex_s3/TestingAtomicLong.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package exercises.ex_s3;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TestingAtomicLong {
|
||||||
|
public static String[] names = {
|
||||||
|
"Alice", "Bob", "Charlie", "Derek", "Emily",
|
||||||
|
"Fionna", "Greg", "Harry", "Isabella", "Julia"
|
||||||
|
};
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
List<AbstractTweet> tweets = new ArrayList<>();
|
||||||
|
AbstractTweet tweet1 = new Tweet("Alice", "Java is cool !");
|
||||||
|
AbstractTweet tweet2 = new AtomicTweet("Bob", "ISC is the best !");
|
||||||
|
tweets.add(tweet1);
|
||||||
|
tweets.add(tweet2);
|
||||||
|
|
||||||
|
Thread[] users = new Thread[10];
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
users[i] = new Thread(new User(names[i], tweets));
|
||||||
|
users[i].start();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
users[i].join();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(tweet1);
|
||||||
|
System.out.println(tweet2);
|
||||||
|
}
|
||||||
|
}
|
21
src/exercises/ex_s3/Tweet.java
Normal file
21
src/exercises/ex_s3/Tweet.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package exercises.ex_s3;
|
||||||
|
|
||||||
|
public class Tweet extends AbstractTweet {
|
||||||
|
private long likes = 0;
|
||||||
|
|
||||||
|
public Tweet(String author, String content) {
|
||||||
|
super(author, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addLike() {
|
||||||
|
likes += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeLike() {
|
||||||
|
likes -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getLikes() {
|
||||||
|
return likes;
|
||||||
|
}
|
||||||
|
}
|
24
src/exercises/ex_s3/User.java
Normal file
24
src/exercises/ex_s3/User.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package exercises.ex_s3;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class User implements Runnable {
|
||||||
|
private final String name;
|
||||||
|
private final List<AbstractTweet> tweets;
|
||||||
|
|
||||||
|
public User(String name, List<AbstractTweet> tweets) {
|
||||||
|
this.name = name;
|
||||||
|
this.tweets = tweets;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
for (int i = 0; i < 100000; i++) {
|
||||||
|
if (i % 10 == 9) {
|
||||||
|
tweets.forEach(AbstractTweet::removeLike);
|
||||||
|
} else {
|
||||||
|
tweets.forEach(AbstractTweet::addLike);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
src/exercises/ex_t/BowlSemaphore.java
Normal file
24
src/exercises/ex_t/BowlSemaphore.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package exercises.ex_t;
|
||||||
|
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
|
public class BowlSemaphore {
|
||||||
|
private boolean isFull = false;
|
||||||
|
private Semaphore semaphore = new Semaphore(1);
|
||||||
|
|
||||||
|
public boolean fill() throws InterruptedException {
|
||||||
|
semaphore.acquire();
|
||||||
|
boolean wasFull = isFull;
|
||||||
|
isFull = true;
|
||||||
|
semaphore.release();
|
||||||
|
return wasFull;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean empty() throws InterruptedException {
|
||||||
|
semaphore.acquire();
|
||||||
|
boolean wasFull = isFull;
|
||||||
|
isFull = false;
|
||||||
|
semaphore.release();
|
||||||
|
return wasFull;
|
||||||
|
}
|
||||||
|
}
|
29
src/exercises/ex_t/Dog.java
Normal file
29
src/exercises/ex_t/Dog.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package exercises.ex_t;
|
||||||
|
|
||||||
|
import exercises.Utils;
|
||||||
|
|
||||||
|
public class Dog implements Runnable {
|
||||||
|
private final String name;
|
||||||
|
private final BowlSemaphore[] bowls;
|
||||||
|
|
||||||
|
public Dog(String name, BowlSemaphore[] bowls) {
|
||||||
|
this.name = name;
|
||||||
|
this.bowls = bowls;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
BowlSemaphore bowl = bowls[i];
|
||||||
|
try {
|
||||||
|
if (bowl.empty()) {
|
||||||
|
System.out.println(name + " ate from bowl " + i);
|
||||||
|
Utils.randomSleep(10000, 20000);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (InterruptedException _) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
28
src/exercises/ex_t/Feeder.java
Normal file
28
src/exercises/ex_t/Feeder.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package exercises.ex_t;
|
||||||
|
|
||||||
|
import exercises.Utils;
|
||||||
|
|
||||||
|
public class Feeder implements Runnable {
|
||||||
|
private final String name;
|
||||||
|
private final BowlSemaphore[] bowls;
|
||||||
|
|
||||||
|
public Feeder(String name, BowlSemaphore[] bowls) {
|
||||||
|
this.name = name;
|
||||||
|
this.bowls = bowls;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
BowlSemaphore bowl = bowls[i];
|
||||||
|
try {
|
||||||
|
if (!bowl.fill()) {
|
||||||
|
System.out.println(name + " filled bowl " + i);
|
||||||
|
Utils.randomSleep(1000, 3000);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException _) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
16
src/exercises/ex_t/TestingSemaphore_DogBreeder.java
Normal file
16
src/exercises/ex_t/TestingSemaphore_DogBreeder.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package exercises.ex_t;
|
||||||
|
|
||||||
|
public class TestingSemaphore_DogBreeder {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
BowlSemaphore[] bowlsSemaphores = new BowlSemaphore[3];
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
bowlsSemaphores[i] = new BowlSemaphore();
|
||||||
|
}
|
||||||
|
|
||||||
|
new Thread(new Feeder("Marco", bowlsSemaphores)).start();
|
||||||
|
new Thread(new Feeder("Luisa", bowlsSemaphores)).start();
|
||||||
|
for (int i = 1; i < 10; i++) {
|
||||||
|
new Thread(new Dog("Dog" + i, bowlsSemaphores)).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
src/exercises/ex_u/FamilyMember.java
Normal file
22
src/exercises/ex_u/FamilyMember.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package exercises.ex_u;
|
||||||
|
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
|
public class FamilyMember implements Runnable {
|
||||||
|
private final String name;
|
||||||
|
private final Vehicle vehicle;
|
||||||
|
private final CountDownLatch countDownLatch;
|
||||||
|
|
||||||
|
public FamilyMember(String name, Vehicle vehicle, CountDownLatch countDownLatch) {
|
||||||
|
this.name = name;
|
||||||
|
this.vehicle = vehicle;
|
||||||
|
this.countDownLatch = countDownLatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
vehicle.addSuitcase(name + "'s suitcase");
|
||||||
|
System.out.println(name + " has loaded their suitcase");
|
||||||
|
countDownLatch.countDown();
|
||||||
|
}
|
||||||
|
}
|
23
src/exercises/ex_u/TestingCountDownLatch_FamilyTrip.java
Normal file
23
src/exercises/ex_u/TestingCountDownLatch_FamilyTrip.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package exercises.ex_u;
|
||||||
|
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
|
public class TestingCountDownLatch_FamilyTrip {
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
Vehicle seatAlhambra = new Vehicle();
|
||||||
|
seatAlhambra.printVehicleContent();
|
||||||
|
String[] family = {"Jean", "Anna", "Joseph", "Martha", "Eleonore", "Paul", "Catarina"};
|
||||||
|
// Create a countDownLatch
|
||||||
|
CountDownLatch countDownLatch = new CountDownLatch(family.length);
|
||||||
|
|
||||||
|
// Start the family members
|
||||||
|
// and synchronize them before the start
|
||||||
|
for (int i = 0; i < family.length; i++) {
|
||||||
|
new Thread(new FamilyMember(family[i], seatAlhambra, countDownLatch)).start();
|
||||||
|
}
|
||||||
|
countDownLatch.await();
|
||||||
|
|
||||||
|
seatAlhambra.printVehicleContent();
|
||||||
|
System.out.println("Family trip can start");
|
||||||
|
}
|
||||||
|
}
|
14
src/exercises/ex_u/Vehicle.java
Normal file
14
src/exercises/ex_u/Vehicle.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package exercises.ex_u;
|
||||||
|
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
|
public class Vehicle {
|
||||||
|
private Queue<String> suitcases = new ConcurrentLinkedQueue<>();
|
||||||
|
public void addSuitcase(String suitcase) {
|
||||||
|
suitcases.add(suitcase);
|
||||||
|
}
|
||||||
|
public void printVehicleContent() {
|
||||||
|
System.out.println("Suitcases: " + suitcases);
|
||||||
|
}
|
||||||
|
}
|
20
src/exercises/ex_v/Referee.java
Normal file
20
src/exercises/ex_v/Referee.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package exercises.ex_v;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Referee implements Runnable {
|
||||||
|
private final List<Result> results;
|
||||||
|
|
||||||
|
public Referee(List<Result> results) {
|
||||||
|
this.results = results;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println("Referee starts to establish ranking...");
|
||||||
|
results.sort(Result::compareTo);
|
||||||
|
System.out.println("PDG Team Ranking:");
|
||||||
|
results.forEach(System.out::println);
|
||||||
|
}
|
||||||
|
}
|
28
src/exercises/ex_v/Result.java
Normal file
28
src/exercises/ex_v/Result.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package exercises.ex_v;
|
||||||
|
|
||||||
|
public class Result implements Comparable<Result>{
|
||||||
|
private final int time;
|
||||||
|
private final String team;
|
||||||
|
public Result(String team, int time) {
|
||||||
|
this.time = time;
|
||||||
|
this.team = team;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTime() {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTeam() {
|
||||||
|
return team;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Result result) {
|
||||||
|
return this.getTime() - result.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getTeam() + " : " + getTime() + "h";
|
||||||
|
}
|
||||||
|
}
|
34
src/exercises/ex_v/Team.java
Normal file
34
src/exercises/ex_v/Team.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package exercises.ex_v;
|
||||||
|
|
||||||
|
import exercises.Utils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.BrokenBarrierException;
|
||||||
|
import java.util.concurrent.CyclicBarrier;
|
||||||
|
|
||||||
|
public class Team implements Runnable {
|
||||||
|
private final String name;
|
||||||
|
private final List<Result> results;
|
||||||
|
private final CyclicBarrier barrier;
|
||||||
|
|
||||||
|
public Team(int i, List<Result> results, CyclicBarrier barrier) {
|
||||||
|
this.name = "Team " + i;
|
||||||
|
this.results = results;
|
||||||
|
this.barrier = barrier;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println(name + " : Started the race");
|
||||||
|
try {
|
||||||
|
Utils.randomSleep(1000, 5000);
|
||||||
|
} catch (InterruptedException _) {}
|
||||||
|
|
||||||
|
Result result = new Result(name, (int) (Math.random() * 20 + 1));
|
||||||
|
System.out.println(name + " : Finished the race in " + result.getTime() + " hours and is waiting for other teams");
|
||||||
|
results.add(result);
|
||||||
|
try {
|
||||||
|
barrier.await();
|
||||||
|
} catch (InterruptedException | BrokenBarrierException _) {}
|
||||||
|
}
|
||||||
|
}
|
22
src/exercises/ex_v/TestingCyclicBarrier_PDG.java
Normal file
22
src/exercises/ex_v/TestingCyclicBarrier_PDG.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package exercises.ex_v;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.BrokenBarrierException;
|
||||||
|
import java.util.concurrent.CyclicBarrier;
|
||||||
|
|
||||||
|
public class TestingCyclicBarrier_PDG {
|
||||||
|
private static int NUM_TEAMS = 20;
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<Result> results = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
System.out.println("Starting PDG with " + NUM_TEAMS + " teams ");
|
||||||
|
CyclicBarrier barrier = new CyclicBarrier(NUM_TEAMS, new Referee(results));
|
||||||
|
|
||||||
|
// Start the team threads and synchronize them.
|
||||||
|
// Make sure finally to order to order the results and print them
|
||||||
|
for (int i = 0; i < NUM_TEAMS; i++) {
|
||||||
|
new Thread(new Team(i, results, barrier)).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
src/exercises/ex_w/Car.java
Normal file
19
src/exercises/ex_w/Car.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package exercises.ex_w;
|
||||||
|
|
||||||
|
public class Car {
|
||||||
|
private String name;
|
||||||
|
private String licensePlateNumber;
|
||||||
|
public Car(String name, String licensePlateNumber) {
|
||||||
|
this.name = name;
|
||||||
|
this.licensePlateNumber = licensePlateNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLicensePlateNumber() {
|
||||||
|
return licensePlateNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
18
src/exercises/ex_w/Customer.java
Normal file
18
src/exercises/ex_w/Customer.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package exercises.ex_w;
|
||||||
|
|
||||||
|
public class Customer implements Runnable {
|
||||||
|
private final String name;
|
||||||
|
private final String licensePlateNumber;
|
||||||
|
private Car car;
|
||||||
|
|
||||||
|
public Customer(String name, Car car) {
|
||||||
|
this.name = name;
|
||||||
|
this.car = car;
|
||||||
|
licensePlateNumber = car.getLicensePlateNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
33
src/exercises/ex_w/Garage.java
Normal file
33
src/exercises/ex_w/Garage.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package exercises.ex_w;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Exchanger;
|
||||||
|
|
||||||
|
public class Garage implements Runnable {
|
||||||
|
private List<Car> courtesyCars = new ArrayList<>();
|
||||||
|
private Exchanger<Car> receiveExchanger = new Exchanger<>();
|
||||||
|
|
||||||
|
public Garage() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Car leaveForRepair(Car car) throws InterruptedException {
|
||||||
|
return receiveExchanger.exchange(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
Car courtesyCar = courtesyCars.removeFirst();
|
||||||
|
try {
|
||||||
|
Car toRepair = receiveExchanger.exchange(courtesyCar);
|
||||||
|
|
||||||
|
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
src/exercises/ex_w/TestingExchanger_Garage.java
Normal file
12
src/exercises/ex_w/TestingExchanger_Garage.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package exercises.ex_w;
|
||||||
|
|
||||||
|
public class TestingExchanger_Garage {
|
||||||
|
String[] carTypes = {"Seat Alhambra", "BMW Mini", "Tesla 3"};
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Create a Customer thread and a Garage thread
|
||||||
|
// that use an exchanger for replacing the customer car
|
||||||
|
// with a courtesy car during the reparation. At the end of the day,
|
||||||
|
// the cars are exchanged back.
|
||||||
|
Garage garage = new Garage();
|
||||||
|
}
|
||||||
|
}
|
35
src/exercises/ex_x/FamilyMember.java
Normal file
35
src/exercises/ex_x/FamilyMember.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package exercises.ex_x;
|
||||||
|
|
||||||
|
import exercises.Utils;
|
||||||
|
|
||||||
|
import java.util.concurrent.Phaser;
|
||||||
|
|
||||||
|
public class FamilyMember implements Runnable {
|
||||||
|
private final String name;
|
||||||
|
private final Phaser phaser;
|
||||||
|
private final int numSteps;
|
||||||
|
|
||||||
|
public FamilyMember(String name, Phaser phaser, int numSteps) {
|
||||||
|
this.name = name;
|
||||||
|
this.phaser = phaser;
|
||||||
|
this.numSteps = numSteps;
|
||||||
|
phaser.register();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
int step = 0;
|
||||||
|
while (step < numSteps) {
|
||||||
|
step = phaser.getPhase() + 1;
|
||||||
|
try {
|
||||||
|
System.out.println(name + ": Step " + step + " started");
|
||||||
|
Utils.randomSleep(1000, 3000);
|
||||||
|
phaser.arriveAndAwaitAdvance();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
phaser.arriveAndDeregister();
|
||||||
|
System.out.println(name + " has arrived");
|
||||||
|
}
|
||||||
|
}
|
24
src/exercises/ex_x/TestingPhaserFamilyBikeTrip.java
Normal file
24
src/exercises/ex_x/TestingPhaserFamilyBikeTrip.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package exercises.ex_x;
|
||||||
|
|
||||||
|
import java.util.concurrent.Phaser;
|
||||||
|
|
||||||
|
public class TestingPhaserFamilyBikeTrip {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String[] family = {"Father", "Mother", "Son", "Daughter_1", "Daughter_2"};
|
||||||
|
String[] routes = {"Sierre", "Sion", "Martigny", "St-Maurice", "Aigle", "Vevey"};
|
||||||
|
Phaser phaser = new Phaser();
|
||||||
|
phaser.register();
|
||||||
|
|
||||||
|
System.out.println("The family leaves Brig for a bike trip to Vevey");
|
||||||
|
for (int i = 0; i < family.length; i++) {
|
||||||
|
new Thread(new FamilyMember(family[i], phaser, routes.length)).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < routes.length; i++) {
|
||||||
|
phaser.arriveAndAwaitAdvance();
|
||||||
|
System.out.println("All family members arrived to " + routes[i]);
|
||||||
|
}
|
||||||
|
phaser.arriveAndDeregister();
|
||||||
|
System.out.println("End of the bike trip.");
|
||||||
|
}
|
||||||
|
}
|
21
src/exercises/ex_y/RaceTester.java
Normal file
21
src/exercises/ex_y/RaceTester.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package exercises.ex_y;
|
||||||
|
|
||||||
|
import java.util.concurrent.Phaser;
|
||||||
|
|
||||||
|
public class RaceTester {
|
||||||
|
public static final int NUM_RUNNERS = 10;
|
||||||
|
public static final int NUM_STAGES = 5;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
Phaser phaser = new Phaser();
|
||||||
|
phaser.register();
|
||||||
|
for (int i = 0; i < NUM_RUNNERS; i++) {
|
||||||
|
new Thread(new Runner(NUM_STAGES, phaser)).start();
|
||||||
|
}
|
||||||
|
for (int i = 0; i <= NUM_STAGES; i++) {
|
||||||
|
phaser.arriveAndAwaitAdvance();
|
||||||
|
}
|
||||||
|
System.out.println("Finished race");
|
||||||
|
phaser.arriveAndDeregister();
|
||||||
|
}
|
||||||
|
}
|
43
src/exercises/ex_y/Runner.java
Normal file
43
src/exercises/ex_y/Runner.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package exercises.ex_y;
|
||||||
|
|
||||||
|
import exercises.Utils;
|
||||||
|
|
||||||
|
import java.util.concurrent.Phaser;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
public class Runner implements Runnable {
|
||||||
|
private static AtomicInteger nextId = new AtomicInteger(0);
|
||||||
|
private final int id;
|
||||||
|
private final int maxStep;
|
||||||
|
private final Phaser phaser;
|
||||||
|
|
||||||
|
public Runner(int maxStep, Phaser phaser) {
|
||||||
|
id = nextId.getAndIncrement();
|
||||||
|
this.maxStep = maxStep;
|
||||||
|
this.phaser = phaser;
|
||||||
|
phaser.register();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println(this + " is on the starting line");
|
||||||
|
phaser.arriveAndAwaitAdvance();
|
||||||
|
|
||||||
|
while (phaser.getPhase() <= this.maxStep) {
|
||||||
|
System.out.println(this + " started stage " + phaser.getPhase());
|
||||||
|
try {
|
||||||
|
Utils.randomSleep(1000, 5000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
System.out.println(this + " finished stage " + phaser.getPhase());
|
||||||
|
phaser.arriveAndAwaitAdvance();
|
||||||
|
}
|
||||||
|
phaser.arriveAndDeregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Runner " + id;
|
||||||
|
}
|
||||||
|
}
|
1
src/exercises/ex_y/answers.txt
Normal file
1
src/exercises/ex_y/answers.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
We should use a Phaser as it allows for threads to wait on each other before moving to the next step
|
26
src/exercises/ex_z4/AuctionMain.java
Normal file
26
src/exercises/ex_z4/AuctionMain.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package exercises.ex_z4;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
|
||||||
|
public class AuctionMain {
|
||||||
|
public static final String[] names = {
|
||||||
|
"Sabrina", "José", "Patrick", "Salomé", "Lisa", "Alfred", "Annna", "Alex", "Kevin"
|
||||||
|
};
|
||||||
|
|
||||||
|
public static final Item[] items = {
|
||||||
|
new Item("Album de Sabrina Carpenter"),
|
||||||
|
new Item("Chaussette gauche de Freddie Mercury"),
|
||||||
|
new Item("Peigne de Michael Jackson"),
|
||||||
|
new Item("Décapsuleur de Kurt Cobain")
|
||||||
|
};
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Auctioneer auctioneer = new Auctioneer("Paul", new ArrayList(Arrays.stream(items).toList()));
|
||||||
|
new Thread(auctioneer).start();
|
||||||
|
for (int i = 0; i < names.length; i++) {
|
||||||
|
new Thread(new Bidder(names[i], auctioneer)).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
src/exercises/ex_z4/AuctionState.java
Normal file
8
src/exercises/ex_z4/AuctionState.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package exercises.ex_z4;
|
||||||
|
|
||||||
|
public enum AuctionState {
|
||||||
|
PRESENTING_ITEM,
|
||||||
|
BIDDING,
|
||||||
|
SOLD,
|
||||||
|
FINISHED
|
||||||
|
}
|
161
src/exercises/ex_z4/Auctioneer.java
Normal file
161
src/exercises/ex_z4/Auctioneer.java
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
package exercises.ex_z4;
|
||||||
|
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
import java.util.concurrent.locks.Condition;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
import static exercises.ex_z4.AuctionState.*;
|
||||||
|
|
||||||
|
public class Auctioneer implements Runnable {
|
||||||
|
private static final DecimalFormat df = new DecimalFormat("#.00");
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
private final Queue<Bid> bids = new ConcurrentLinkedQueue<>();
|
||||||
|
private final ArrayList<Item> items;
|
||||||
|
private final Lock bidLock = new ReentrantLock();
|
||||||
|
private final Condition bidCond = bidLock.newCondition();
|
||||||
|
public final Lock stateLock = new ReentrantLock();
|
||||||
|
public final Condition stateCond = stateLock.newCondition();
|
||||||
|
|
||||||
|
private Bid maximumBid;
|
||||||
|
private Thread countdownThread = null;
|
||||||
|
private AuctionState auctionState = PRESENTING_ITEM;
|
||||||
|
|
||||||
|
|
||||||
|
public Auctioneer(String name, ArrayList<Item> items) {
|
||||||
|
this.name = name;
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void announce(String msg) {
|
||||||
|
System.out.println("[Auctioneer " + name + "] " + msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setState(AuctionState newState) {
|
||||||
|
stateLock.lock();
|
||||||
|
try {
|
||||||
|
auctionState = newState;
|
||||||
|
stateCond.signalAll();
|
||||||
|
} finally {
|
||||||
|
stateLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void presentItem() throws InterruptedException {
|
||||||
|
Item item = items.getFirst();
|
||||||
|
announce("Now selling this new item: '" + item.name() + "'");
|
||||||
|
announce("Get ready to bid...");
|
||||||
|
Thread.sleep(2000);
|
||||||
|
announce("Bidding open !");
|
||||||
|
maximumBid = null;
|
||||||
|
setState(BIDDING);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void placeBid(Bid bid) {
|
||||||
|
if (auctionState == BIDDING) {
|
||||||
|
System.out.println(bid.bidder() + " bids $" + df.format(bid.amount()));
|
||||||
|
bidLock.lock();
|
||||||
|
try {
|
||||||
|
bids.add(bid);
|
||||||
|
bidCond.signalAll();
|
||||||
|
} finally {
|
||||||
|
bidLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void countdown() {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
System.out.println("Three");
|
||||||
|
Thread.sleep(1000);
|
||||||
|
System.out.println("Two");
|
||||||
|
Thread.sleep(1000);
|
||||||
|
System.out.println("One");
|
||||||
|
Thread.sleep(1000);
|
||||||
|
setState(SOLD);
|
||||||
|
bidLock.lock();
|
||||||
|
try {
|
||||||
|
bidCond.signalAll();
|
||||||
|
} finally {
|
||||||
|
bidLock.unlock();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException _) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startCountdown() {
|
||||||
|
countdownThread = new Thread(this::countdown);
|
||||||
|
countdownThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Bid getMaximumBid() {
|
||||||
|
return maximumBid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuctionState getAuctionState() {
|
||||||
|
return auctionState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void finalizeBidding() throws InterruptedException {
|
||||||
|
if (maximumBid == null) {
|
||||||
|
announce("Nobody wants this items ?!");
|
||||||
|
} else {
|
||||||
|
announce("And sold for $" + df.format(maximumBid.amount()) + " !");
|
||||||
|
Item item = items.removeFirst();
|
||||||
|
announce(maximumBid.bidder() + " is now the proud owner of " + item);
|
||||||
|
maximumBid.bidder().addItem(item);
|
||||||
|
}
|
||||||
|
Thread.sleep(2000);
|
||||||
|
if (items.isEmpty()) {
|
||||||
|
setState(FINISHED);
|
||||||
|
} else {
|
||||||
|
setState(PRESENTING_ITEM);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (auctionState != FINISHED) {
|
||||||
|
try {
|
||||||
|
switch (auctionState) {
|
||||||
|
case PRESENTING_ITEM -> {
|
||||||
|
presentItem();
|
||||||
|
}
|
||||||
|
case BIDDING -> {
|
||||||
|
Bid current_bid = bids.poll();
|
||||||
|
if (current_bid == null) {
|
||||||
|
if (maximumBid != null) {
|
||||||
|
startCountdown();
|
||||||
|
}
|
||||||
|
bidLock.lock();
|
||||||
|
try {
|
||||||
|
bidCond.await();
|
||||||
|
} finally {
|
||||||
|
bidLock.unlock();
|
||||||
|
}
|
||||||
|
if (countdownThread != null) {
|
||||||
|
countdownThread.interrupt();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (maximumBid == null || current_bid.amount() > maximumBid.amount()) {
|
||||||
|
maximumBid = current_bid;
|
||||||
|
announce("New best bid : " + df.format(maximumBid.amount()) + " by " + maximumBid.bidder());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case SOLD -> {
|
||||||
|
finalizeBidding();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
announce("The auction is now finished ! Thank you for participating !");
|
||||||
|
}
|
||||||
|
}
|
5
src/exercises/ex_z4/Bid.java
Normal file
5
src/exercises/ex_z4/Bid.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package exercises.ex_z4;
|
||||||
|
|
||||||
|
public record Bid(Bidder bidder, double amount) {
|
||||||
|
|
||||||
|
}
|
78
src/exercises/ex_z4/Bidder.java
Normal file
78
src/exercises/ex_z4/Bidder.java
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package exercises.ex_z4;
|
||||||
|
|
||||||
|
import exercises.Utils;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static exercises.ex_z4.AuctionState.BIDDING;
|
||||||
|
import static exercises.ex_z4.AuctionState.FINISHED;
|
||||||
|
|
||||||
|
public class Bidder implements Runnable {
|
||||||
|
private static int nextId = 0;
|
||||||
|
private final String name;
|
||||||
|
private final int id;
|
||||||
|
private final List<Item> items = new ArrayList<>();
|
||||||
|
private Double lastBid = null;
|
||||||
|
private final Auctioneer auctioneer;
|
||||||
|
|
||||||
|
public Bidder(String name, Auctioneer auctioneer) {
|
||||||
|
this.name = name;
|
||||||
|
this.id = nextId++;
|
||||||
|
this.auctioneer = auctioneer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
AuctionState state = auctioneer.getAuctionState();
|
||||||
|
if (state == BIDDING) {
|
||||||
|
Random rand = new Random();
|
||||||
|
double addon = rand.nextDouble(500);
|
||||||
|
Bid maximumBid = auctioneer.getMaximumBid();
|
||||||
|
double base = maximumBid == null ? 0 : maximumBid.amount();
|
||||||
|
lastBid = base + addon;
|
||||||
|
auctioneer.placeBid(new Bid(this, lastBid));
|
||||||
|
long minSleep = (long) (base * 10);
|
||||||
|
try {
|
||||||
|
Utils.randomSleep(minSleep, minSleep + 1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
} else if (state == FINISHED) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
auctioneer.stateLock.lock();
|
||||||
|
try {
|
||||||
|
auctioneer.stateCond.await();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
auctioneer.stateLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(Item item){
|
||||||
|
items.add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getLastBid() {
|
||||||
|
return lastBid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "(" + id + ") " + name;
|
||||||
|
}
|
||||||
|
}
|
5
src/exercises/ex_z4/Item.java
Normal file
5
src/exercises/ex_z4/Item.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package exercises.ex_z4;
|
||||||
|
|
||||||
|
public record Item(String name) {
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user