Added final eating Philosopher

This commit is contained in:
Tim 2024-11-12 15:11:14 +01:00
parent def506778a
commit 9508c22d55
5 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package Praktikum03;
public class BetterPhilosopher extends Philosopher {
public BetterPhilosopher(int id, Fork left, Fork right) {
super(id, left, right);
}
@Override
public void run() {
if (left.getId() < right.getId()) {
tryToTakeFork(left);
tryToTakeFork(right);
}else{
tryToTakeFork(right);
tryToTakeFork(left);
}
eat();
}
}

33
src/Praktikum03/Fork.java Normal file
View File

@ -0,0 +1,33 @@
package Praktikum03;
public class Fork {
private boolean inUse = false;
private final int id;
public Fork(int id){
this.id = id;
}
public int getId(){
return id;
}
public boolean isInUse(){
return inUse;
}
public synchronized boolean take() {
if(inUse){
return false;
}else {
inUse = true;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Fehler");
}
return true;
}
}
public synchronized void put(){
inUse = false;
}
}

View File

@ -0,0 +1,47 @@
package Praktikum03;
public class Philosopher implements Runnable {
protected int id;
protected final Fork left;
protected final Fork right;
public Philosopher(int id, Fork left, Fork right) {
this.id = id;
this.left = left;
this.right = right;
}
@Override
public void run() {
tryToTakeFork(left);
tryToTakeFork(right);
eat();
}
protected void eat() {
System.out.println("Philosopher " + id + " is eating");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
left.put();
right.put();
System.out.println("Philosopher " + id + " finished eating");
}
protected void tryToTakeFork(Fork fork) {
System.out.println("Philosopher " + id + " is trying to get fork " + fork.getId());
while(!fork.take()){
Thread.yield();
}
System.out.println("Philosopher " + id + " got fork " + fork.getId());
}
}

View File

@ -0,0 +1,19 @@
package Praktikum03;
public class Table {
public static void main(String[] args) {
Fork[] forks = { new Fork(0), new Fork(1), new Fork(2),
new Fork(3), new Fork(4)};
BetterPhilosopher[] philosophers = {
new BetterPhilosopher(0, forks[0], forks[1]),
new BetterPhilosopher(1, forks[1], forks[2]),
new BetterPhilosopher(2, forks[2], forks[3]),
new BetterPhilosopher(3, forks[3], forks[4]),
new BetterPhilosopher(4, forks[4], forks[0])
};
for (BetterPhilosopher philosopher : philosophers) {
Thread t = new Thread(philosopher);
t.start();
}
}
}

View File

@ -0,0 +1,31 @@
package Praktikum03;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ForkTest {
@Test
void getId() {
Fork fork = new Fork(1);
assertEquals(1, fork.getId());
}
@Test
void take() throws InterruptedException {
Fork fork = new Fork(2);
assertTrue(fork.take());
assertFalse(fork.take());
fork.put();
assertTrue(fork.take());
}
@Test
void put() throws InterruptedException {
Fork fork = new Fork(3);
fork.take();
fork.put();
assertFalse(fork.isInUse());
}
}