Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- === modified file 'concurrency/DeadlockingDiningPhilosophers.java'
- --- concurrency/DeadlockingDiningPhilosophers.java 2012-12-04 05:49:42 +0000
- +++ concurrency/DeadlockingDiningPhilosophers.java 2012-12-08 13:48:47 +0000
- @@ -15,11 +15,12 @@
- size = Integer.parseInt(args[1]);
- ExecutorService exec = Executors.newCachedThreadPool();
- Chopstick[] sticks = new Chopstick[size];
- + LinkedBlockingQueue<Chopstick> bin = new LinkedBlockingQueue<Chopstick>();
- for(int i = 0; i < size; i++)
- sticks[i] = new Chopstick();
- for(int i = 0; i < size; i++)
- - exec.execute(new Philosopher(
- - sticks[i], sticks[(i+1) % size], i, ponder));
- + exec.execute(new Philosopher(
- + sticks[i], sticks[(i+1) % size], bin, i, ponder));
- if(args.length == 3 && args[2].equals("timeout"))
- TimeUnit.SECONDS.sleep(5);
- else {
- @@ -29,3 +30,12 @@
- exec.shutdownNow();
- }
- } /* (Execute to see output) *///:~
- +
- +/**
- + * Ex21(page1229): When the Philosopher puts only one chopstick in the
- + * bin, then the dealock does occur. If the Philosopher puts two
- + * chopsticks the bin -- we end having 10 chopsticks in the queue --
- + * every chopstick is shared by two Philosopher, so in this case
- + * (though it is erroneus, we don't reach Deadlock.)
- + */
- +
- === modified file 'concurrency/Philosopher.java'
- --- concurrency/Philosopher.java 2012-12-04 05:49:42 +0000
- +++ concurrency/Philosopher.java 2012-12-08 13:48:47 +0000
- @@ -7,6 +7,7 @@
- import static util.Print.*;
- public class Philosopher implements Runnable {
- + private BlockingQueue<Chopstick> bin;
- private Chopstick left;
- private Chopstick right;
- private final int id;
- @@ -17,16 +18,16 @@
- TimeUnit.MILLISECONDS.sleep(
- rand.nextInt(ponderFactor * 250));
- }
- - public Philosopher(Chopstick left, Chopstick right,
- + public Philosopher(Chopstick left, Chopstick right, BlockingQueue<Chopstick> bin,
- int ident, int ponder) {
- this.left = left;
- this.right = right;
- + this.bin = bin;
- id = ident;
- ponderFactor = ponder;
- }
- public void run() {
- try {
- - while(!Thread.interrupted()) {
- print(this + " " + "thinking");
- pause();
- // Philosopher becomes hungry
- @@ -36,9 +37,22 @@
- left.take();
- print(this + " " + "eating");
- pause();
- - right.drop();
- + right.drop();
- left.drop();
- - }
- + print("putting left to bin."); bin.put(left);
- + while(!Thread.interrupted()) {
- + print(this + " " + "thinking");
- + pause();
- + // Philosopher becomes hungry
- + print(this + " " + "grabbing right from bin");
- + right = bin.take();
- + print(this + " " + "grabbing left from bin");
- + left = bin.take();
- + print(this + " " + "eating");
- + pause();
- + print(this + " " + "putting right to bin "); bin.put(right);
- + print(this + " " + "putting left to bin "); bin.put(left);
- + }
- } catch(InterruptedException e) {
- print(this + " " + "exiting via interrupt");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement