added ex E
This commit is contained in:
parent
790c1ffea0
commit
1e8e6eb1e9
17
src/exercicses/ex_e/Account.java
Normal file
17
src/exercicses/ex_e/Account.java
Normal file
@ -0,0 +1,17 @@
|
||||
package exercicses.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/exercicses/ex_e/Customer.java
Normal file
26
src/exercicses/ex_e/Customer.java
Normal file
@ -0,0 +1,26 @@
|
||||
package exercicses.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/exercicses/ex_e/TestingThreadInterferences.java
Normal file
10
src/exercicses/ex_e/TestingThreadInterferences.java
Normal file
@ -0,0 +1,10 @@
|
||||
package exercicses.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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user