Final version
parent
3cee7198b9
commit
285200c86e
|
@ -3,43 +3,53 @@ 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;
|
||||||
|
|
||||||
public Philosopher() {
|
public Philosopher() {
|
||||||
this.random = new Random();
|
this.random = new Random();
|
||||||
this.seat = 0;
|
this.seat = 0;
|
||||||
this.stopped = false;
|
this.stopped = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static void main(String... args) {
|
||||||
public void run() {
|
|
||||||
try {
|
// TODO
|
||||||
while (this.stopped == false) {
|
}
|
||||||
think();
|
|
||||||
eat();
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// Thread was interrupted, exit gracefully
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (this.stopped == false) {
|
||||||
|
think();
|
||||||
|
eat();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// Thread was interrupted, exit gracefully
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void eat() throws InterruptedException {
|
private void eat() throws InterruptedException {
|
||||||
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;
|
}
|
||||||
}finally {
|
|
||||||
|
this.eating = true;
|
||||||
|
} 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));
|
||||||
|
@ -57,7 +67,6 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setLeft(IPhilosopher left) {
|
public void setLeft(IPhilosopher left) {
|
||||||
|
@ -82,14 +91,17 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
public void setTable(Lock table) {
|
public void setTable(Lock table) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
this.table = table;
|
this.table = table;
|
||||||
condition = this.table.newCondition();
|
condition = this.table.newCondition();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopPhilosopher() {
|
public void stopPhilosopher() {
|
||||||
|
|
||||||
this.stopped = true;
|
this.stopped = true;
|
||||||
interrupt();
|
interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue