<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Supple software comments on The five philosophers</title>
    <link>http://supplesoftware.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Supple software comments</description>
    <item>
      <title>"The five philosophers" by petrovg</title>
      <description>&lt;p&gt;Here&amp;#8217;s my little implementation of the five philosophers problem.&lt;/p&gt;

&lt;p&gt;The chopstick:
&lt;code&gt;&lt;/p&gt;

&lt;pre&gt;
package philosophers;

public class Chopstick {

    private int number;

    public Chopstick(int number) {
        this.number = number;
    }
    public int getNumber() {
        return number;
    }
}
&lt;/pre&gt;

&lt;p&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The philosopher:
&lt;code&gt;&lt;/p&gt;

&lt;pre&gt;
package philosophers;

public class Philosopher implements Runnable {

    private int number;

    private Chopstick leftChopstick;

    private Chopstick rightChopstick;

    public Philosopher(int number, Chopstick leftChopstick,
            Chopstick rightChopstick) {
        this.number = number;
        this.leftChopstick = leftChopstick;
        this.rightChopstick = rightChopstick;
    }

    private void eat(int time) {
        synchronized (leftChopstick) {
            System.out.println("Philosopher " + this.number
                    + " took chopstick " + leftChopstick.getNumber()
                    + " from the left.");
            synchronized (rightChopstick) {
                System.out.println("Philosopher " + this.number
                        + " took chopstick " + rightChopstick.getNumber()
                        + " from the right.");
                try {
                    System.out.println("Philosopher " + this.number
                            + " is eating...");
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    private void think(int time) {
        try {
            System.out.println("Philosopher " + this.number + " is thinking...");
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public void run() {
        while (true) {
            think(1);
            eat(1);
        }
    }

}
&lt;/pre&gt;

&lt;p&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And, bringing them all together, the Table:
&lt;code&gt;&lt;/p&gt;

&lt;pre&gt;
package philosophers;

public class Table {

    /**
     * Chopstick 0 is on the left of phylosopher 0
     */
    private Chopstick[] chopsticks;

    private Philosopher[] philosophers;

    public void build(int numberOfSeats) {
        for (int i = 0; i &lt; numberOfSeats; i++) {
            chopsticks[i] = new Chopstick(i);
        }
        for (int i = 0; i &lt; numberOfSeats; i++) {
            philosophers[i] = new Philosopher(i, chopsticks[i],
                    chopsticks[i &lt; numberOfSeats - 1 ? i + 1 : 0]);
        }
    }

    public Table(int numberOfSeats) {
        chopsticks = new Chopstick[numberOfSeats];
        philosophers = new Philosopher[numberOfSeats];
        build(numberOfSeats);
    }

    public void start() {
        System.out.println("Chopstick 0 is on the left of philosopher 0");
        for (Philosopher philosopher : philosophers) {
            new Thread(philosopher).start();
        }
    }

    public static void main (String[] args) {
        new Table(5).start();
    }
}
&lt;/pre&gt;

&lt;p&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Deadlocks pretty quickly with the numbers as they are here.&lt;/p&gt;

</description>
      <pubDate>Tue, 20 Nov 2007 01:54:00 PST</pubDate>
      <guid>&lt;a href="/articles/2007/11/20/the-five-philosophers"&gt;The five philosophers&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2007/11/20/the-five-philosophers"&gt;The five philosophers&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
