Skip to content

Instantly share code, notes, and snippets.

@daohoangson
Created December 6, 2013 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daohoangson/7819090 to your computer and use it in GitHub Desktop.
Save daohoangson/7819090 to your computer and use it in GitHub Desktop.
public class Chopstick {
int cid, used;
boolean output;
/*
* Constructor
* This chopstick is the chopstick number `cid`
*/
public Chopstick(int cid) {
this.used = -1;
this.cid = cid;
this.output = false;
}
/*
* pick method
* The Philosopher number `pid` is requesting me
*/
public synchronized void pick(int pid) {
// check if nobody is using me
if (used != -1) {
// this is not the case, you have to wait
try {
wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
// okie, your turn
used = pid;
if (output) System.out.println("Chopstick #" + (cid + 1) + " is picked by #" + (used + 1));
}
/*
* release method
* The Philosopher finished using me
*/
public synchronized void release(int pid) {
if (output) System.out.println("Chopstick #" + (cid + 1) + " is releasded by #" + (used + 1));
used = -1;
// notify who is waiting
notify();
}
}
public class DiningPhilosophers {
public static void main(String[] args) {
/*
* Let's create a table with 5 chopsticks
* And then assign 5 philosophers to it
*/
int n = 5;
Table table = new Table(n);
for (int i = 0; i < n; i++) {
new Philosopher(table,i,n);
}
}
}
public class Philosopher implements Runnable {
private Table table;
private int pid;
private int total;
private int eat_count;
private int timeout;
/*
* Constructor
* Assign our Philosopher to a `table`
* He will be the guest number `pid`
* The total number of guests are `total`
*/
public Philosopher(Table table, int pid, int total) {
this.table = table;
this.pid = pid;
this.total = total;
this.eat_count = 0;
this.timeout = 5000;
new Thread(this, "Philosopher " + pid).start();
}
/*
* eat method
* Everybody has to eat at some point in life
*/
private void eat() {
eat_count++;
System.out.println("Philosopher #" + (pid + 1) + " is eating (#" + eat_count + ").");
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
System.out.println(e);
}
}
/*
* think method
* A philosopher should love thinking
*/
private void think() {
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
System.out.println(e);
}
}
public void run() {
while (true) {
// shortcuts
Chopstick c1 = table.chopsticks[pid];
Chopstick c2 = table.chopsticks[(pid + 1) % total];
// pick (or wait) for 2 chopsticks
c1.pick(pid);
c2.pick(pid);
// great, let's eat
eat();
// be fair, release the 2 chopsticks
c1.release(pid);
c2.release(pid);
// hmm, to be or not to be?
think();
}
}
}
public class Table {
public Chopstick chopsticks[];
/*
* Constructor
* Create a new table with `n` chopsticks
*/
public Table(int n) {
chopsticks = new Chopstick[n];
for (int i = 0; i < n; i++)
chopsticks[i] = new Chopstick(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment