renamed folders
This commit is contained in:
8
src/exercises/ex_a/HelloRunnable.java
Normal file
8
src/exercises/ex_a/HelloRunnable.java
Normal file
@ -0,0 +1,8 @@
|
||||
package exercises.ex_a;
|
||||
|
||||
public class HelloRunnable implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Hello from a class implementing the Runnable interface");
|
||||
}
|
||||
}
|
8
src/exercises/ex_a/HelloThread.java
Normal file
8
src/exercises/ex_a/HelloThread.java
Normal file
@ -0,0 +1,8 @@
|
||||
package exercises.ex_a;
|
||||
|
||||
public class HelloThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Hello from a class extending the Thread class");
|
||||
}
|
||||
}
|
8
src/exercises/ex_a/TestingSimpleThreads.java
Normal file
8
src/exercises/ex_a/TestingSimpleThreads.java
Normal file
@ -0,0 +1,8 @@
|
||||
package exercises.ex_a;
|
||||
|
||||
public class TestingSimpleThreads {
|
||||
public static void main(String[] args) {
|
||||
new HelloThread().start();
|
||||
new Thread(new HelloRunnable()).start();
|
||||
}
|
||||
}
|
28
src/exercises/ex_b/SleepMessages.java
Normal file
28
src/exercises/ex_b/SleepMessages.java
Normal file
@ -0,0 +1,28 @@
|
||||
package exercises.ex_b;
|
||||
|
||||
public class SleepMessages implements Runnable {
|
||||
public 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"
|
||||
};
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (String msg : song) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
System.out.println(msg);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
7
src/exercises/ex_b/TestingSleep.java
Normal file
7
src/exercises/ex_b/TestingSleep.java
Normal file
@ -0,0 +1,7 @@
|
||||
package exercises.ex_b;
|
||||
|
||||
public class TestingSleep {
|
||||
public static void main(String[] args) {
|
||||
new Thread(new SleepMessages()).start();
|
||||
}
|
||||
}
|
28
src/exercises/ex_c/SleepMessages.java
Normal file
28
src/exercises/ex_c/SleepMessages.java
Normal file
@ -0,0 +1,28 @@
|
||||
package exercises.ex_c;
|
||||
|
||||
public class SleepMessages implements Runnable {
|
||||
public 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"
|
||||
};
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (String msg : song) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
System.out.println(msg);
|
||||
} catch (InterruptedException e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
14
src/exercises/ex_c/TestingSleep.java
Normal file
14
src/exercises/ex_c/TestingSleep.java
Normal file
@ -0,0 +1,14 @@
|
||||
package exercises.ex_c;
|
||||
|
||||
public class TestingSleep {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread thread = new Thread(new SleepMessages());
|
||||
thread.start();
|
||||
|
||||
System.out.println("Waiting 3 seconds");
|
||||
Thread.sleep(3000);
|
||||
System.out.println("Stopping thread");
|
||||
thread.interrupt();
|
||||
System.out.println("Stopped");
|
||||
}
|
||||
}
|
23
src/exercises/ex_d/EmptyingThread.java
Normal file
23
src/exercises/ex_d/EmptyingThread.java
Normal file
@ -0,0 +1,23 @@
|
||||
package exercises.ex_d;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class EmptyingThread extends Thread {
|
||||
private Stack<Integer> source;
|
||||
private Stack<Integer> target;
|
||||
|
||||
public EmptyingThread(Stack<Integer> source, Stack<Integer> target) {
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
int v;
|
||||
while (!source.isEmpty()) {
|
||||
v = source.pop();
|
||||
System.out.println("Number popped from source stack and pushed to target stack: " + v);
|
||||
target.push(v);
|
||||
}
|
||||
}
|
||||
}
|
31
src/exercises/ex_d/FillingThread.java
Normal file
31
src/exercises/ex_d/FillingThread.java
Normal file
@ -0,0 +1,31 @@
|
||||
package exercises.ex_d;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class FillingThread extends Thread {
|
||||
private Stack<Integer> source;
|
||||
private Stack<Integer> target;
|
||||
private Thread emptyingThread;
|
||||
|
||||
public FillingThread(Stack<Integer> source, Stack<Integer> target, Thread emptyingThread) {
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
this.emptyingThread = emptyingThread;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
emptyingThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("--- Starting Filling back ---");
|
||||
int v;
|
||||
while (!target.isEmpty()) {
|
||||
v = target.pop();
|
||||
System.out.println("From target stack to source stack " + v);
|
||||
source.push(v);
|
||||
}
|
||||
}
|
||||
}
|
18
src/exercises/ex_d/TestingJoins.java
Normal file
18
src/exercises/ex_d/TestingJoins.java
Normal file
@ -0,0 +1,18 @@
|
||||
package exercises.ex_d;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class TestingJoins {
|
||||
public static void main(String[] args) {
|
||||
Stack<Integer> sourceStack = new Stack<Integer>();
|
||||
Stack<Integer> targetStack = new Stack<Integer>();
|
||||
// Populating the first sourceStack
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
sourceStack.push(i);
|
||||
}
|
||||
Thread emptyingThread = new EmptyingThread(sourceStack, targetStack);
|
||||
Thread fillingThread = new FillingThread(sourceStack, targetStack, emptyingThread);
|
||||
emptyingThread.start();
|
||||
fillingThread.start();
|
||||
}
|
||||
}
|
36
src/exercises/ex_d_bis/StepThread.java
Normal file
36
src/exercises/ex_d_bis/StepThread.java
Normal file
@ -0,0 +1,36 @@
|
||||
package exercises.ex_d_bis;
|
||||
|
||||
public class StepThread extends Thread {
|
||||
private int i;
|
||||
private String startMsg;
|
||||
private String endMsg;
|
||||
private long duration;
|
||||
private Thread dependency;
|
||||
|
||||
public StepThread(int i, String startMsg, String endMsg, long duration, Thread dependency) {
|
||||
this.i = i;
|
||||
this.startMsg = startMsg;
|
||||
this.endMsg = endMsg;
|
||||
this.duration = duration;
|
||||
this.dependency = dependency;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (dependency != null) {
|
||||
try {
|
||||
dependency.join();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Stage " + i + ": " + startMsg);
|
||||
try {
|
||||
Thread.sleep(duration);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("Stage " + i + ": " + endMsg);
|
||||
}
|
||||
}
|
30
src/exercises/ex_d_bis/TestStepThreads.java
Normal file
30
src/exercises/ex_d_bis/TestStepThreads.java
Normal file
@ -0,0 +1,30 @@
|
||||
package exercises.ex_d_bis;
|
||||
|
||||
public class TestStepThreads {
|
||||
public static void main(String[] args) {
|
||||
Thread rawMatPrep = new StepThread(
|
||||
1,
|
||||
"Preparing raw materials...",
|
||||
"Completed raw materials preparation.",
|
||||
1000,
|
||||
null
|
||||
);
|
||||
Thread assembly = new StepThread(
|
||||
2,
|
||||
"Assembling the product...",
|
||||
"Completed product assembly.",
|
||||
2000,
|
||||
rawMatPrep
|
||||
);
|
||||
Thread qualityCtrl = new StepThread(
|
||||
3,
|
||||
"Quality control...",
|
||||
"Quality control completed.",
|
||||
500,
|
||||
assembly
|
||||
);
|
||||
rawMatPrep.start();
|
||||
assembly.start();
|
||||
qualityCtrl.start();
|
||||
}
|
||||
}
|
17
src/exercises/ex_e/Account.java
Normal file
17
src/exercises/ex_e/Account.java
Normal file
@ -0,0 +1,17 @@
|
||||
package exercises.ex_e;
|
||||
|
||||
public class Account {
|
||||
private int balance;
|
||||
|
||||
public void withdraw(int amount) {
|
||||
balance -= amount;
|
||||
}
|
||||
|
||||
public void deposit(int amount) {
|
||||
balance += amount;
|
||||
}
|
||||
|
||||
public int getBalance() {
|
||||
return balance;
|
||||
}
|
||||
}
|
26
src/exercises/ex_e/Customer.java
Normal file
26
src/exercises/ex_e/Customer.java
Normal file
@ -0,0 +1,26 @@
|
||||
package exercises.ex_e;
|
||||
|
||||
public class Customer implements Runnable {
|
||||
private String name;
|
||||
private Account account;
|
||||
|
||||
public Customer(String name, Account account) {
|
||||
this.name = name;
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
account.deposit((int) (Math.random() * 1000));
|
||||
System.out.println(name + " has deposited money on account with new balance " + account.getBalance());
|
||||
account.withdraw((int) (Math.random() * 1000));
|
||||
System.out.println(name + " has withdrawn money from the account with new balance " + account.getBalance());
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
10
src/exercises/ex_e/TestingThreadInterferences.java
Normal file
10
src/exercises/ex_e/TestingThreadInterferences.java
Normal file
@ -0,0 +1,10 @@
|
||||
package exercises.ex_e;
|
||||
|
||||
public class TestingThreadInterferences {
|
||||
public static void main(String[] args) {
|
||||
// shared account object
|
||||
Account account = new Account();
|
||||
(new Thread(new Customer("Damien", account))).start();
|
||||
(new Thread(new Customer("Hélène", account))).start();
|
||||
}
|
||||
}
|
17
src/exercises/ex_f/Account.java
Normal file
17
src/exercises/ex_f/Account.java
Normal file
@ -0,0 +1,17 @@
|
||||
package exercises.ex_f;
|
||||
|
||||
public class Account {
|
||||
private int balance;
|
||||
|
||||
public synchronized void withdraw(int amount) {
|
||||
balance -= amount;
|
||||
}
|
||||
|
||||
public synchronized void deposit(int amount) {
|
||||
balance += amount;
|
||||
}
|
||||
|
||||
public synchronized int getBalance() {
|
||||
return balance;
|
||||
}
|
||||
}
|
26
src/exercises/ex_f/Customer.java
Normal file
26
src/exercises/ex_f/Customer.java
Normal file
@ -0,0 +1,26 @@
|
||||
package exercises.ex_f;
|
||||
|
||||
public class Customer implements Runnable {
|
||||
private String name;
|
||||
private Account account;
|
||||
|
||||
public Customer(String name, Account account) {
|
||||
this.name = name;
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
account.deposit((int) (Math.random() * 1000));
|
||||
System.out.println(name + " has deposited money on account with new balance " + account.getBalance());
|
||||
account.withdraw((int) (Math.random() * 1000));
|
||||
System.out.println(name + " has withdrawn money from the account with new balance " + account.getBalance());
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
10
src/exercises/ex_f/TestingThreadInterferences.java
Normal file
10
src/exercises/ex_f/TestingThreadInterferences.java
Normal file
@ -0,0 +1,10 @@
|
||||
package exercises.ex_f;
|
||||
|
||||
public class TestingThreadInterferences {
|
||||
public static void main(String[] args) {
|
||||
// shared account object
|
||||
Account account = new Account();
|
||||
(new Thread(new Customer("Damien", account))).start();
|
||||
(new Thread(new Customer("Hélène", account))).start();
|
||||
}
|
||||
}
|
23
src/exercises/ex_g/Account.java
Normal file
23
src/exercises/ex_g/Account.java
Normal file
@ -0,0 +1,23 @@
|
||||
package exercises.ex_g;
|
||||
|
||||
public class Account {
|
||||
private int balance;
|
||||
|
||||
public void withdraw(int amount) {
|
||||
synchronized (this) {
|
||||
balance -= amount;
|
||||
}
|
||||
}
|
||||
|
||||
public void deposit(int amount) {
|
||||
synchronized (this) {
|
||||
balance += amount;
|
||||
}
|
||||
}
|
||||
|
||||
public int getBalance() {
|
||||
synchronized (this) {
|
||||
return balance;
|
||||
}
|
||||
}
|
||||
}
|
26
src/exercises/ex_g/BankEmployee.java
Normal file
26
src/exercises/ex_g/BankEmployee.java
Normal file
@ -0,0 +1,26 @@
|
||||
package exercises.ex_g;
|
||||
|
||||
public class BankEmployee implements Runnable {
|
||||
private String name;
|
||||
private Account account;
|
||||
|
||||
public BankEmployee(String name, Account account) {
|
||||
this.name = name;
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
account.deposit((int) (Math.random() * 1000));
|
||||
System.out.println(name + " has deposited money on account with new balance " + account.getBalance());
|
||||
account.withdraw((int) (Math.random() * 1000));
|
||||
System.out.println(name + " has withdrawn money from the account with new balance " + account.getBalance());
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
10
src/exercises/ex_g/TestingSynchronizedStatements.java
Normal file
10
src/exercises/ex_g/TestingSynchronizedStatements.java
Normal file
@ -0,0 +1,10 @@
|
||||
package exercises.ex_g;
|
||||
|
||||
public class TestingSynchronizedStatements {
|
||||
public static void main(String[] args) {
|
||||
// shared account object
|
||||
Account account = new Account();
|
||||
(new Thread(new BankEmployee("Mrs Fournier", account))).start();
|
||||
(new Thread(new BankEmployee("Mr Schmidhalter", account))).start();
|
||||
}
|
||||
}
|
43
src/exercises/ex_h1/TestThreadWithDeadlock.java
Normal file
43
src/exercises/ex_h1/TestThreadWithDeadlock.java
Normal file
@ -0,0 +1,43 @@
|
||||
package exercises.ex_h1;
|
||||
|
||||
public class TestThreadWithDeadlock {
|
||||
public static Object Lock1 = new Object();
|
||||
public static Object Lock2 = new Object();
|
||||
|
||||
public static void main(String args[]) {
|
||||
ThreadDemo1 T1 = new ThreadDemo1();
|
||||
ThreadDemo2 T2 = new ThreadDemo2();
|
||||
T1.start();
|
||||
T2.start();
|
||||
}
|
||||
|
||||
private static class ThreadDemo1 extends Thread {
|
||||
public void run() {
|
||||
synchronized (Lock1) {
|
||||
System.out.println("Thread 1: Holding lock 1...");
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {}
|
||||
System.out.println("Thread 1: Waiting for lock 2...");
|
||||
synchronized (Lock2) {
|
||||
System.out.println("Thread 1: Holding lock 1 & 2...");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private static class ThreadDemo2 extends Thread {
|
||||
public void run() {
|
||||
synchronized (Lock1) {
|
||||
System.out.println("Thread 2: Holding lock 1...");
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
System.out.println("Thread 2: Waiting for lock 2...");
|
||||
synchronized (Lock2) {
|
||||
System.out.println("Thread 2: Holding lock 1 & 2...");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
32
src/exercises/ex_i/Employee.java
Normal file
32
src/exercises/ex_i/Employee.java
Normal file
@ -0,0 +1,32 @@
|
||||
package exercises.ex_i;
|
||||
|
||||
public class Employee extends Thread {
|
||||
private String name;
|
||||
private String room;
|
||||
private int exageratedTimeUsageFactor;
|
||||
|
||||
public Employee(String name, String room, int exageratedTimeUsageFactor) {
|
||||
this.name = name;
|
||||
this.room = room;
|
||||
this.exageratedTimeUsageFactor = exageratedTimeUsageFactor;
|
||||
}
|
||||
|
||||
public void useRoom(int time) {
|
||||
synchronized (room) {
|
||||
System.out.println(name + " uses room for time: " + time);
|
||||
try {
|
||||
Thread.sleep(time);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(name + " thread started");
|
||||
while (true) {
|
||||
useRoom(100 * exageratedTimeUsageFactor);
|
||||
}
|
||||
}
|
||||
}
|
13
src/exercises/ex_i/TestingStarvation.java
Normal file
13
src/exercises/ex_i/TestingStarvation.java
Normal file
@ -0,0 +1,13 @@
|
||||
package exercises.ex_i;
|
||||
|
||||
public class TestingStarvation {
|
||||
public static void main(String[] args) {
|
||||
String sharedRoom = "Polaris";
|
||||
Employee t1 = new Employee("Emma", sharedRoom, 1);
|
||||
Employee t2 = new Employee("Jean", sharedRoom, 100);
|
||||
|
||||
t1.start();
|
||||
t2.start();
|
||||
System.out.println("Main thread ended");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user