Final version
parent
3cee7198b9
commit
285200c86e
|
@ -3,13 +3,14 @@ package pp;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.locks.Condition;
|
import java.util.concurrent.locks.Condition;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
public class Philosopher extends Thread implements IPhilosopher {
|
public class Philosopher extends Thread implements IPhilosopher {
|
||||||
private int seat;
|
private int seat;
|
||||||
private Philosopher rechts;
|
private Philosopher rechts;
|
||||||
private Philosopher links;
|
private Philosopher links;
|
||||||
private Lock table;
|
private Lock table;
|
||||||
private Condition condition ;
|
private Condition condition;
|
||||||
private volatile boolean stopped;
|
private volatile boolean stopped;
|
||||||
private volatile boolean eating;
|
private volatile boolean eating;
|
||||||
private final Random random;
|
private final Random random;
|
||||||
|
@ -20,6 +21,12 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
this.stopped = false;
|
this.stopped = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String... args) {
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
@ -36,10 +43,13 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
table.lock();
|
table.lock();
|
||||||
try {
|
try {
|
||||||
|
|
||||||
while(this.links.eating || this.rechts.eating)
|
while (this.links.eating || this.rechts.eating) {
|
||||||
|
log(seat, " : isst gerade!");
|
||||||
this.condition.await();
|
this.condition.await();
|
||||||
|
}
|
||||||
|
|
||||||
this.eating = true;
|
this.eating = true;
|
||||||
}finally {
|
} finally {
|
||||||
table.unlock();
|
table.unlock();
|
||||||
}
|
}
|
||||||
Thread.sleep(this.random.nextInt(PhilosopherExperiment.MAX_EATING_DURATION_MS));
|
Thread.sleep(this.random.nextInt(PhilosopherExperiment.MAX_EATING_DURATION_MS));
|
||||||
|
@ -58,7 +68,6 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setLeft(IPhilosopher left) {
|
public void setLeft(IPhilosopher left) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
@ -92,4 +101,7 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
this.stopped = true;
|
this.stopped = true;
|
||||||
interrupt();
|
interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue