49 lines
1.3 KiB
Java
49 lines
1.3 KiB
Java
|
//: concurrency/Philosopher.java
|
|||
|
// <20>2015 MindView LLC: see Copyright.txt
|
|||
|
// A dining philosopher
|
|||
|
import java.util.concurrent.*;
|
|||
|
import java.util.*;
|
|||
|
import static com.mindviewinc.util.Print.*;
|
|||
|
|
|||
|
public class Philosopher implements Runnable {
|
|||
|
private Chopstick left;
|
|||
|
private Chopstick right;
|
|||
|
private final int id;
|
|||
|
private final int ponderFactor;
|
|||
|
private Random rand = new Random(47);
|
|||
|
private void pause() throws InterruptedException {
|
|||
|
if(ponderFactor == 0) return;
|
|||
|
TimeUnit.MILLISECONDS.sleep(
|
|||
|
rand.nextInt(ponderFactor * 250));
|
|||
|
}
|
|||
|
public Philosopher(Chopstick left, Chopstick right,
|
|||
|
int ident, int ponder) {
|
|||
|
this.left = left;
|
|||
|
this.right = right;
|
|||
|
id = ident;
|
|||
|
ponderFactor = ponder;
|
|||
|
}
|
|||
|
@Override
|
|||
|
public void run() {
|
|||
|
try {
|
|||
|
while(!Thread.interrupted()) {
|
|||
|
print(this + " " + "thinking");
|
|||
|
pause();
|
|||
|
// Philosopher becomes hungry
|
|||
|
print(this + " " + "grabbing right");
|
|||
|
right.take();
|
|||
|
print(this + " " + "grabbing left");
|
|||
|
left.take();
|
|||
|
print(this + " " + "eating");
|
|||
|
pause();
|
|||
|
right.drop();
|
|||
|
left.drop();
|
|||
|
}
|
|||
|
} catch(InterruptedException e) {
|
|||
|
print(this + " " + "exiting via interrupt");
|
|||
|
}
|
|||
|
}
|
|||
|
@Override
|
|||
|
public String toString() { return "Philosopher " + id; }
|
|||
|
} ///:~
|