started ex Z4
This commit is contained in:
parent
c7d054e973
commit
afc2fbfa8a
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>
|
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) {}
|
||||
}
|
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();
|
||||
}
|
||||
}
|
23
src/exercises/ex_z4/AuctionMain.java
Normal file
23
src/exercises/ex_z4/AuctionMain.java
Normal file
@ -0,0 +1,23 @@
|
||||
package exercises.ex_z4;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
public class AuctionMain {
|
||||
public static final String[] names = {
|
||||
"Sabrina", "José", "Patrick", "Salomé", "Lisa", "Alfred", "Annna", "Alex", "Kevin"
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
Semaphore mutex = new Semaphore(1);
|
||||
ConcurrentLinkedQueue<Bid> bids = new ConcurrentLinkedQueue<>();
|
||||
Auctioneer auctioneer = new Auctioneer("Paul", bids, mutex);
|
||||
|
||||
new Thread(auctioneer).start();
|
||||
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
new Thread(new Bidder(names[i])).start();
|
||||
}
|
||||
}
|
||||
}
|
81
src/exercises/ex_z4/Auctioneer.java
Normal file
81
src/exercises/ex_z4/Auctioneer.java
Normal file
@ -0,0 +1,81 @@
|
||||
package exercises.ex_z4;
|
||||
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
public class Auctioneer implements Runnable {
|
||||
private String name;
|
||||
private ConcurrentLinkedQueue<Bid> bids;
|
||||
private Semaphore mutex;
|
||||
private Bid maximumBid;
|
||||
private Thread countdownThread = null;
|
||||
|
||||
public Auctioneer(String name, ConcurrentLinkedQueue<Bid> bids, Semaphore mutex) {
|
||||
this.name = name;
|
||||
this.bids = bids;
|
||||
this.mutex = mutex;
|
||||
}
|
||||
|
||||
public void placeBid(Bid bid) {
|
||||
bids.add(bid);
|
||||
notify();
|
||||
}
|
||||
|
||||
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);
|
||||
System.out.println("End of the Auction !");
|
||||
notify();
|
||||
} catch (InterruptedException _) {}
|
||||
}
|
||||
|
||||
public void startCountdown() {
|
||||
countdownThread = new Thread(this::countdown);
|
||||
countdownThread.start();
|
||||
}
|
||||
|
||||
public Bid getMaximumBid() {
|
||||
return maximumBid;
|
||||
}
|
||||
|
||||
public void endAuction(){
|
||||
System.out.println("End of the auction ! ");
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
mutex.acquire();
|
||||
Bid current_bid = bids.poll();
|
||||
if (current_bid == null) {
|
||||
startCountdown();
|
||||
wait();
|
||||
if (bids.isEmpty()) {
|
||||
endAuction();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!bids.isEmpty()) {
|
||||
if (current_bid.amount() > maximumBid.amount()) {
|
||||
maximumBid = current_bid;
|
||||
System.out.println("New bid : " + maximumBid.amount() + " by " + maximumBid.bidder());
|
||||
mutex.release();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
mutex.release();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
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) {
|
||||
|
||||
}
|
44
src/exercises/ex_z4/Bidder.java
Normal file
44
src/exercises/ex_z4/Bidder.java
Normal file
@ -0,0 +1,44 @@
|
||||
package exercises.ex_z4;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
|
||||
public Bidder(String name) {
|
||||
this.name = name;
|
||||
this.id = nextId++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(true){
|
||||
Random rand = new Random();
|
||||
double addon = rand.nextDouble(500);
|
||||
lastBid += addon;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user