Compare commits

..

4 Commits

Author SHA1 Message Date
romanschenk37 ac876c7979 solved Task AccountTransfer 2022-04-07 14:37:54 +02:00
romanschenk37 7dfa09a53e Changed Task Philosopher 2022-04-07 14:03:18 +02:00
romanschenk37 b5fc96ef94 Solved Task Philosopher 2022-04-07 13:31:57 +02:00
github-classroom[bot] e0669b7382
Setting up GitHub Classroom Feedback 2022-03-31 09:49:58 +00:00
3 changed files with 21 additions and 11 deletions

View File

@ -13,11 +13,11 @@ public class Account {
return id;
}
public int getBalance() {
public synchronized int getBalance() {
return balance;
}
public void transferAmount(int amount) {
public synchronized void transferAmount(int amount) {
this.balance += amount;
}
}

View File

@ -22,7 +22,8 @@ public class AccountTransferSimulation {
(account1.getBalance() + account2.getBalance() + account3.getBalance()));
System.out.println("Start of Transaction");
AccountTransferTask task1 = new AccountTransferTask(account3, account1, 2);
AccountTransferTask task1 = new AccountTransferTask(account1, account3, 2);
//AccountTransferTask task1 = new AccountTransferTask(account3, account1, 2);
AccountTransferTask task2 = new AccountTransferTask(account3, account2, 1);
AccountTransferTask task3 = new AccountTransferTask(account2, account1, 2);

View File

@ -16,7 +16,7 @@ class ForkManager {
// maximum concurrent threads acquiring forks - used for internal statistics
private static final LockFreeMax concurrentAcquires = new LockFreeMax();
private final Lock mutex; // shared mutex for all forks
//private final Lock mutex; // shared mutex for all forks
private final int delayTime; // delay in milliseconds between acquiring / releasing the forks
private final int numForks; // amount of forks to be managed
private final Fork[] forks; // array of managed forks
@ -27,12 +27,12 @@ class ForkManager {
* @param delayTime delay in milliseconds between acquiring / releasing the forks
*/
public ForkManager(int numForks, int delayTime) {
this.mutex = new ReentrantLock();
//this.mutex = new ReentrantLock();
this.delayTime = delayTime;
this.numForks = numForks;
this.forks = new Fork[numForks];
for (int forkId = 0; forkId < numForks; forkId++)
forks[forkId] = new Fork(forkId, mutex);
forks[forkId] = new Fork(forkId);
}
/**
@ -42,10 +42,15 @@ class ForkManager {
* @param philosopherId id of the philosopher
*/
public void acquireForks(int philosopherId) throws InterruptedException {
Fork leftFork = leftFork(philosopherId);
Fork rightFork = rightFork(philosopherId);
// acquire forks sequentially
leftFork(philosopherId).acquire(philosopherId);
while(!(leftFork.getState() == FREE && rightFork.getState() == FREE)){
forkDelay(); //wait if not both forks are available
}
leftFork.acquire(philosopherId);
forkDelay();
rightFork(philosopherId).acquire(philosopherId);
rightFork.acquire(philosopherId);
}
/**
@ -136,12 +141,12 @@ class ForkManager {
/**
* Constructor for a fork.
* @param id unique id of the fork
* @param mutex shared mutex to use for wait conditions
* //@param mutex shared mutex to use for wait conditions
*/
public Fork(int id, Lock mutex) {
public Fork(int id) {
this.id = id;
this.ownerId = -1;
this.mutex = mutex;
this.mutex = new ReentrantLock();;
this.cond = mutex.newCondition();
}
@ -195,6 +200,10 @@ class ForkManager {
public String toString() {
return "Fork {" + "id=" + id + ", state=" + state + ", owner=" + ownerId + '}';
}
public ForkState getState(){
return state;
}
}
/*