threads
Dinning Philosophers deadlock example
This is an example of the Dining Philosophers’ problem. A short description of the problem shows that there are N philosphers sitting around a circular table eating and discussing philosphy. The problem is that each philosopher needs 2 forks to eat, and there are only N forks, one between each 2 philosophers. An algorithm is needed for the philosophers to follow that will ensure that none starves as long as each philosopher eventually stops eating, and such that the maximum number of philosophers can eat at once. The example consists of the following classes:
- The class
Spoon
represents the spoons that a Philosopher needs in order to eat. - The class Philosopher extends the Thread, and overrides its
run()
method and itstoString()
method. It consists of two methods,think()
andeat()
. In thethink()
method, the thread sleeps for a random number of milliseconds. In itseat()
method the thread holds in asynchronized
statement the left Spoon first and then the second Spoon that represent the left and the right spoon of thePhilosopher
. In its constructor the thread gets the two Spoon objects that represent the left and the right spoon and calls itsstart()
method to begin execution. In its overridenrun()
method the thread keeps callingthink()
andeat()
methods forever. - The
Timeout
class extends the Timer class and overrides itsrun()
method. It sets its superTimer(boolean isDaemon)
true, so that the associated thread should run as a daemon. It calls itsschedule(TimerTask task, long delay)
method with a new TimerTask and a specified delay. Is is used in the example in order to stop the running Java Virtual Machine after a specified time. - In order to run the example, we create a
Philosopher
array. We create the two Spoon objects, that are the left and right spoon. For each one of the philosopher array positions we create a new Philosopher to put into the array and we give the Philosopher a left and a right spoon. Then the right spoon is set to the left and a new rightSpoon
is created for the nextPhilosopher
. In order to avoid the deadlock we use the static booleangotoDeadLock
. When set to true a newPhilosopher
is created and gets the left and first spoon, that have values set above and in the while statement.
Let’s take a look at the code snippet that follows:
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | package com.javacodegeeks.snippets.core; import java.util.Random; import java.util.Timer; import java.util.TimerTask; class Spoon { private static int cnt = 0 ; private int num = cnt++; @Override public String toString() { return "Spoon " + num; } } class Philosopher extends Thread { private static Random rnd = new Random(); private static int cnt = 0 ; private int num = cnt++; private Spoon leftSpoon; private Spoon rightSpoon; static int waiting = 0 ; public Philosopher(Spoon left, Spoon right) { leftSpoon = left; rightSpoon = right; start(); } public void think() { System.out.println( this + " is thinking" ); if (waiting > 0 ) { try { sleep(rnd.nextInt(waiting)); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public void eat() { synchronized (leftSpoon) { System.out.println( this + " has " + this .leftSpoon + " Waiting for " + this .rightSpoon); synchronized (rightSpoon) { System.out.println( this + " eating" ); } } } @Override public String toString() { return "Philosopher " + num; } @Override public void run() { while ( true ) { think(); eat(); } } } public class DiningPhilosophers { private static boolean gotoDeadLock = true ; public static void main(String[] args) { Philosopher[] phil = new Philosopher[ 10 ]; Philosopher.waiting = 8 ; Spoon left = new Spoon(), right = new Spoon(), first = left; int i = 0 ; while (i < phil.length - 1 ) { phil[i++] = new Philosopher(left, right); left = right; right = new Spoon(); } if (gotoDeadLock) { phil[i] = new Philosopher(left, first); } else { phil[i] = new Philosopher(first, left); } if (args.length >= 4 ) { int delay = 3 ; if (delay != 0 ) { Timeout timeout = new Timeout(delay * 1000 , "Timed out" ); } } } } class Timeout extends Timer { public Timeout( int delay, final String msg) { super ( true ); schedule( new TimerTask() { @Override public void run() { System.out.println(msg); System.exit( 0 ); } }, delay); } } |
This was an example of the Dining Philosophers’ problem in Java.