Browse Source

DiningPhilo

master
Ece Nur Keles 1 year ago
parent
commit
7feec0d3cd

+ 30
- 0
src/praktikum04/BetterPhilosopher.java View File

@@ -0,0 +1,30 @@
package praktikum04;

public class BetterPhilosopher extends Philosopher{

public BetterPhilosopher(int id, Fork left, Fork right) {
super(id, left, right);
}

public void allocateForks() {
Fork fork1 = left;
Fork fork2 = right;

if(left.getId() > right.getId()) {
fork1 = right;
fork2 = left;
}

System.out.println("Philosopher" + id + "is trying to get a fork" + fork1.getId());
while(!fork1.take())
Thread.yield();
System.out.println("Philosopher" + id + "got a fork" + fork1.getId());

System.out.println("Philosopher" + id + "is trying to get a fork" + fork2.getId());
while(!fork2.take())
Thread.yield();
System.out.println("Philosopher" + id + "got a fork" + fork2.getId());

}

}

+ 41
- 0
src/praktikum04/Fork.java View File

@@ -0,0 +1,41 @@
package praktikum04;

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 take() {
synchronized (this) {
if(inUse) {
return false;
}
inUse = true;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Gabel" + id + "konnte nicht angenommen werden");

}
return true;
}
}

public void put() {
synchronized (this) {
inUse = false;
}

}
}

+ 47
- 0
src/praktikum04/Philosopher.java View File

@@ -0,0 +1,47 @@
package praktikum04;

import java.awt.desktop.AppHiddenListener;

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() {

allocateForks();
System.out.println("Philosopher " + id + " is eating");

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Philosopher" + id + "was interrupted");
}
left.put();
right.put();

System.out.println("Philosopher" + id + "is done eating");


}

protected void allocateForks() {
System.out.println("Philosopher" + id + "is trying to get a fork." + left.getId());
while (!left.take())
Thread.yield();
System.out.println("Philosopher" + id + "got fork" + left.getId());
System.out.println("Philosopher" + id + "is trying to get a fork" + right.getId());
while (!right.take())
Thread.yield();
System.out.println("Philosopher" + id + "got fork" + right.getId());
}
}

+ 20
- 0
src/praktikum04/Table.java View File

@@ -0,0 +1,20 @@
package praktikum04;

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)};
Philosopher[] philosophers = {
new Philosopher(0, forks[0], forks[1]),
new Philosopher(1, forks[1], forks[2]),
new Philosopher(2, forks[2], forks[3]),
new Philosopher(3, forks[3], forks[4]),
new Philosopher(4, forks[4], forks[0])
};
for (Philosopher philosopher : philosophers) {
Thread t = new Thread(philosopher);
t.start();
}
}
}

+ 32
- 0
test/praktikum04/ForkTest.java View File

@@ -0,0 +1,32 @@
package praktikum04;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ForkTest {

@Test
void getId() {

Fork fork = new Fork(1);
fork.getId();
}

@Test
void take() {

Fork fork = new Fork(1);
}

@Test
void put() {

Fork fork = new Fork(1);
fork.take();

fork.put();


}
}

Loading…
Cancel
Save