1
//////////////////////////////////////////////////////////////////////
3
// JCSP ("CSP for Java") Libraries //
4
// Copyright (C) 1996-2008 Peter Welch and Paul Austin. //
5
// 2001-2004 Quickstone Technologies Limited. //
7
// This library is free software; you can redistribute it and/or //
8
// modify it under the terms of the GNU Lesser General Public //
9
// License as published by the Free Software Foundation; either //
10
// version 2.1 of the License, or (at your option) any later //
13
// This library is distributed in the hope that it will be //
14
// useful, but WITHOUT ANY WARRANTY; without even the implied //
15
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
16
// PURPOSE. See the GNU Lesser General Public License for more //
19
// You should have received a copy of the GNU Lesser General //
20
// Public License along with this library; if not, write to the //
21
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, //
22
// Boston, MA 02111-1307, USA. //
24
// Author contact: P.H.Welch@kent.ac.uk //
27
//////////////////////////////////////////////////////////////////////
30
import org.jcsp.lang.*;
35
class CallPhil implements CSProcess {
37
//A Philosopher thinks for a while -- around 3 seconds -- and then goes to the
38
//Canteen for food, consuming what he gets straight away. This cycle continues
41
//Except, that is, for Philosopher 0 ... who refuses to think and just keeps
42
//going to the Canteen.
44
//For this Canteen, when there's no chicken, the Philosphers are just kept
45
//waiting in the service queue. The greedy Philosopher no longer loses his
46
//place through getting in before the food is cooked and doesn't starve.
48
private final String id;
49
private final CallCanteen.Service service;
50
private final int thinkTime;
51
private final int eatTime;
52
private final boolean greedy;
54
public CallPhil (String id, CallCanteen.Service service,
55
int thinkTime, int eatTime, boolean greedy) {
57
this.service = service;
58
this.thinkTime = thinkTime;
59
this.eatTime = eatTime;
64
final CSTimer tim = new CSTimer ();
67
// everyone, unless greedy, has a little think
69
System.out.println (" Phil " + id + " : thinking ... ");
70
tim.sleep (thinkTime); // thinking
73
System.out.println (" Phil " + id + " : gotta eat ... ");
74
service.takeChicken (id);
76
System.out.println (" Phil " + id + " : mmm ... that's good [" + nEaten + " so far]");
77
tim.sleep (eatTime); // eating