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; return id;
} }
public synchronized int getBalance() { public int getBalance() {
return balance; return balance;
} }
public synchronized void transferAmount(int amount) { public void transferAmount(int amount) {
this.balance += amount; this.balance += amount;
} }
} }

View File

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

View File

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