2016-11-23 09:05:26 -08:00
|
|
|
// concurrent/FixedDiningPhilosophers.java
|
2015-12-15 11:47:04 -08:00
|
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Dining philosophers without deadlock
|
2016-07-28 13:42:03 -06:00
|
|
|
// {java FixedDiningPhilosophers 5 5 timeout}
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.concurrent.*;
|
2016-12-21 11:06:49 -08:00
|
|
|
import onjava.Nap;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
public class FixedDiningPhilosophers {
|
2016-01-25 18:05:55 -08:00
|
|
|
public static void
|
2016-12-21 11:06:49 -08:00
|
|
|
main(String[] args) {
|
2015-06-15 17:47:35 -07:00
|
|
|
int ponder = 5;
|
|
|
|
if(args.length > 0)
|
|
|
|
ponder = Integer.parseInt(args[0]);
|
|
|
|
int size = 5;
|
|
|
|
if(args.length > 1)
|
|
|
|
size = Integer.parseInt(args[1]);
|
2016-01-25 18:05:55 -08:00
|
|
|
ExecutorService es = Executors.newCachedThreadPool();
|
2015-06-15 17:47:35 -07:00
|
|
|
Chopstick[] sticks = new Chopstick[size];
|
|
|
|
for(int i = 0; i < size; i++)
|
|
|
|
sticks[i] = new Chopstick();
|
|
|
|
for(int i = 0; i < size; i++)
|
|
|
|
if(i < (size-1))
|
2016-01-25 18:05:55 -08:00
|
|
|
es.execute(new Philosopher(
|
2015-06-15 17:47:35 -07:00
|
|
|
sticks[i], sticks[i+1], i, ponder));
|
|
|
|
else
|
2016-01-25 18:05:55 -08:00
|
|
|
es.execute(new Philosopher(
|
2015-06-15 17:47:35 -07:00
|
|
|
sticks[0], sticks[i], i, ponder));
|
|
|
|
if(args.length == 3 && args[2].equals("timeout"))
|
2016-12-21 11:06:49 -08:00
|
|
|
new Nap(5000);
|
2015-06-15 17:47:35 -07:00
|
|
|
else {
|
|
|
|
System.out.println("Press 'Enter' to quit");
|
2016-12-21 11:06:49 -08:00
|
|
|
try {
|
|
|
|
System.in.read();
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2016-01-25 18:05:55 -08:00
|
|
|
es.shutdownNow();
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
2016-07-20 06:32:39 -06:00
|
|
|
/* Output: (First and Last 10 Lines)
|
2016-07-27 11:12:11 -06:00
|
|
|
Philosopher 1 thinking
|
2015-06-15 17:47:35 -07:00
|
|
|
Philosopher 4 thinking
|
|
|
|
Philosopher 2 thinking
|
2016-07-27 11:12:11 -06:00
|
|
|
Philosopher 0 thinking
|
|
|
|
Philosopher 3 thinking
|
|
|
|
Philosopher 1 grabbing right
|
2015-06-15 17:47:35 -07:00
|
|
|
Philosopher 2 grabbing right
|
2016-07-27 11:12:11 -06:00
|
|
|
Philosopher 4 grabbing right
|
|
|
|
Philosopher 4 grabbing left
|
|
|
|
Philosopher 4 eating
|
2016-07-22 14:45:35 -06:00
|
|
|
...________...________...________...________...
|
2016-07-27 11:12:11 -06:00
|
|
|
Philosopher 1 thinking
|
|
|
|
Philosopher 0 grabbing left
|
2016-07-22 14:45:35 -06:00
|
|
|
Philosopher 0 eating
|
2016-07-27 11:12:11 -06:00
|
|
|
Philosopher 2 eating
|
|
|
|
Philosopher 1 grabbing right
|
2016-07-22 14:45:35 -06:00
|
|
|
Philosopher 3 exiting via interrupt
|
|
|
|
Philosopher 1 exiting via interrupt
|
|
|
|
Philosopher 2 exiting via interrupt
|
2016-07-27 11:12:11 -06:00
|
|
|
Philosopher 0 exiting via interrupt
|
|
|
|
Philosopher 4 exiting via interrupt
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|