Compare commits

..

No commits in common. "master" and "feedback" have entirely different histories.

3 changed files with 11 additions and 21 deletions

View File

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

View File

@ -22,8 +22,7 @@ public class AccountTransferSimulation {
(account1.getBalance() + account2.getBalance() + account3.getBalance()));
System.out.println("Start of Transaction");
AccountTransferTask task1 = new AccountTransferTask(account1, account3, 2);
//AccountTransferTask task1 = new AccountTransferTask(account3, account1, 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);
forks[forkId] = new Fork(forkId, mutex);
}
/**
@ -42,15 +42,10 @@ 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
while(!(leftFork.getState() == FREE && rightFork.getState() == FREE)){
forkDelay(); //wait if not both forks are available
}
leftFork.acquire(philosopherId);
leftFork(philosopherId).acquire(philosopherId);
forkDelay();
rightFork.acquire(philosopherId);
rightFork(philosopherId).acquire(philosopherId);
}
/**
@ -141,12 +136,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) {
public Fork(int id, Lock mutex) {
this.id = id;
this.ownerId = -1;
this.mutex = new ReentrantLock();;
this.mutex = mutex;
this.cond = mutex.newCondition();
}
@ -200,10 +195,6 @@ class ForkManager {
public String toString() {
return "Fork {" + "id=" + id + ", state=" + state + ", owner=" + ownerId + '}';
}
public ForkState getState(){
return state;
}
}
/*