Skip to content

Instantly share code, notes, and snippets.

@rokon12
Created November 14, 2017 14:24
Show Gist options
  • Save rokon12/2bc444aca475eeb88b887bf54d852d7a to your computer and use it in GitHub Desktop.
Save rokon12/2bc444aca475eeb88b887bf54d852d7a to your computer and use it in GitHub Desktop.
Dinning Philosopher
package com.bazlur.ds.thread.diningphiloshoper;
/**
* @author Bazlur Rahman Rokon
* @since 11/14/17.
*/
public class Chopstick {
}
package com.bazlur.ds.thread.diningphiloshoper;
/**
* @author Bazlur Rahman Rokon
* @since 11/14/17.
*/
public class DiningPhilosophers {
public static void main(String[] args) {
Philosopher[] philosophers = new Philosopher[5];
Chopstick chopstick[] = new Chopstick[5];
for (int i = 0; i < chopstick.length; i++) {
chopstick[i] = new Chopstick();
}
for (int i = 0; i < philosophers.length; i++) {
Chopstick leftFork = chopstick[i];
Chopstick rightFork = chopstick[(i + 1) % chopstick.length];
philosophers[i] = new Philosopher(leftFork, rightFork);
Thread t = new Thread(philosophers[i], "Philosopher " + (i + 1));
t.start();
}
}
}
package com.bazlur.ds.thread.diningphiloshoper;
/**
* @author Bazlur Rahman Rokon
* @since 11/14/17.
*/
public class Philosopher implements Runnable {
private Chopstick leftChopstick;
private Chopstick rightChopstick;
public Philosopher(Chopstick leftChopstick, Chopstick rightChopstick) {
this.leftChopstick = leftChopstick;
this.rightChopstick = rightChopstick;
}
@Override
public void run() {
try {
while (true) {
doAction(System.nanoTime() + ": Thinking");
synchronized (leftChopstick) {
doAction(System.nanoTime() + ": Picked up left fork");
synchronized (rightChopstick) {
// eating
doAction(System.nanoTime() + ": Picked up right fork - eating");
doAction(System.nanoTime() + ": Put down right fork");
}
// Back to thinking
doAction(System.nanoTime() + ": Put down left fork. Back to thinking");
}
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
return;
}
}
private void doAction(String action) throws InterruptedException {
System.out.println(Thread.currentThread().getName() + " " + action);
Thread.sleep(((int) (Math.random() * 100)));
}
}
@rokon12
Copy link
Author

rokon12 commented Nov 14, 2017

Found one Java-level deadlock:

"Philosopher 5":
waiting to lock monitor 0x00007fe68c004e28 (object 0x000000076d2c8dd0, a com.bazlur.ds.thread.diningphiloshoper.Chopstick),
which is held by "Philosopher 1"
"Philosopher 1":
waiting to lock monitor 0x00007fe68c0062c8 (object 0x000000076d2c8de0, a com.bazlur.ds.thread.diningphiloshoper.Chopstick),
which is held by "Philosopher 2"
"Philosopher 2":
waiting to lock monitor 0x00007fe68c006218 (object 0x000000076d2c8df0, a com.bazlur.ds.thread.diningphiloshoper.Chopstick),
which is held by "Philosopher 3"
"Philosopher 3":
waiting to lock monitor 0x00007fe668002178 (object 0x000000076d2c8e00, a com.bazlur.ds.thread.diningphiloshoper.Chopstick),
which is held by "Philosopher 4"
"Philosopher 4":
waiting to lock monitor 0x00007fe668003778 (object 0x000000076d2c8e10, a com.bazlur.ds.thread.diningphiloshoper.Chopstick),
which is held by "Philosopher 5"

Java stack information for the threads listed above:

"Philosopher 5":
at com.bazlur.ds.thread.diningphiloshoper.Philosopher.run(Philosopher.java:26)
- waiting to lock <0x000000076d2c8dd0> (a com.bazlur.ds.thread.diningphiloshoper.Chopstick)
- locked <0x000000076d2c8e10> (a com.bazlur.ds.thread.diningphiloshoper.Chopstick)
at java.lang.Thread.run(Thread.java:745)
"Philosopher 1":
at com.bazlur.ds.thread.diningphiloshoper.Philosopher.run(Philosopher.java:26)
- waiting to lock <0x000000076d2c8de0> (a com.bazlur.ds.thread.diningphiloshoper.Chopstick)
- locked <0x000000076d2c8dd0> (a com.bazlur.ds.thread.diningphiloshoper.Chopstick)
at java.lang.Thread.run(Thread.java:745)
"Philosopher 2":
at com.bazlur.ds.thread.diningphiloshoper.Philosopher.run(Philosopher.java:26)
- waiting to lock <0x000000076d2c8df0> (a com.bazlur.ds.thread.diningphiloshoper.Chopstick)
- locked <0x000000076d2c8de0> (a com.bazlur.ds.thread.diningphiloshoper.Chopstick)
at java.lang.Thread.run(Thread.java:745)
"Philosopher 3":
at com.bazlur.ds.thread.diningphiloshoper.Philosopher.run(Philosopher.java:26)
- waiting to lock <0x000000076d2c8e00> (a com.bazlur.ds.thread.diningphiloshoper.Chopstick)
- locked <0x000000076d2c8df0> (a com.bazlur.ds.thread.diningphiloshoper.Chopstick)
at java.lang.Thread.run(Thread.java:745)
"Philosopher 4":
at com.bazlur.ds.thread.diningphiloshoper.Philosopher.run(Philosopher.java:26)
- waiting to lock <0x000000076d2c8e10> (a com.bazlur.ds.thread.diningphiloshoper.Chopstick)
- locked <0x000000076d2c8e00> (a com.bazlur.ds.thread.diningphiloshoper.Chopstick)
at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment