Solved Task Philosopher

This commit is contained in:
romanschenk37 2022-04-07 13:31:57 +02:00
parent e0669b7382
commit b5fc96ef94
1 changed files with 17 additions and 8 deletions

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, mutex); forks[forkId] = new Fork(forkId);
} }
/** /**
@ -42,10 +42,15 @@ 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
leftFork(philosopherId).acquire(philosopherId); while(!(leftFork.getState() == FREE && rightFork.getState() == FREE)){
forkDelay();
}
leftFork.acquire(philosopherId);
forkDelay(); forkDelay();
rightFork(philosopherId).acquire(philosopherId); rightFork.acquire(philosopherId);
} }
/** /**
@ -136,12 +141,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, Lock mutex) { public Fork(int id) {
this.id = id; this.id = id;
this.ownerId = -1; this.ownerId = -1;
this.mutex = mutex; this.mutex = new ReentrantLock();;
this.cond = mutex.newCondition(); this.cond = mutex.newCondition();
} }
@ -195,6 +200,10 @@ 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;
}
} }
/* /*