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
//////////////////////////////////////////////////////////////////////
31
//| This program shows a use of the ALT mechanism. It is a re-implementation
32
//| of the Starving Philosophers example but now has the Canteen programmed
33
//| properly as an active process. Here is the College:
37
//| :) :) :) :) :) ___________ ________
39
//| ---------------------<->--| Canteen |------<------| Cook |
40
//| service |_________| supply |______|
44
//| This time, although Philsopher 0 is just as greedy, no one starves.
47
import org.jcsp.lang.*;
50
* This Canteen is an active object -- a pure SERVER process for its `service'
51
* and `supply' CALL channels. The service channel is any-1 since, hopefully,
52
* there will be many customers. The supply channel may be 1-1 (one chef) or
61
// call interfaces and channels
63
public static interface Service {
64
public int takeChicken (String philId);
67
public static interface Supply {
68
public int freshChickens (String chefId, int value);
71
// fields and constructors
73
private final Service service; // called by the philosophers
74
private final Supply supply; // called by the chefs
75
private final int serviceTime; // how long a philosopher spends in the canteen
76
private final int supplyTime; // how long the chef spends in the canteen
77
private final int maxChickens; // maximum number of chickens in the canteen
78
private final CSTimer tim; // for delays
80
public StarveCanteen (int serviceTime, int supplyTime, int maxChickens) {
81
this.service = new Service () {
82
public int takeChicken (String philId) {
83
return doTakeChicken (philId);
86
this.supply = new Supply () {
87
public int freshChickens (String chefId, int value) {
88
return doFreshChickens (chefId, value);
91
this.serviceTime = serviceTime;
92
this.supplyTime = supplyTime;
93
this.maxChickens = maxChickens;
94
this.tim = new CSTimer ();
97
public Service getService () {
101
public Supply getSupply () {
105
private int nChickens = 0;
106
private int nSupplied = 0;
108
private synchronized int doTakeChicken (String philId) {
109
System.out.println (" Canteen -> " + philId + " : one chicken ordered ... "
110
+ nChickens + " left");
112
tim.sleep (serviceTime); // this takes serviceTime to deliver
115
System.out.println (" Canteen -> " + philId + " : one chicken coming down ... "
116
+ nChickens + " left [" + nSupplied + " supplied]");
123
private synchronized int doFreshChickens (String chefId, int value) {
124
System.out.println (" Canteen <- " + chefId
125
+ " : ouch ... make room ... this dish is very hot ...");
126
tim.sleep (supplyTime); // this takes supplyTime to put down
128
int sendBack = nChickens - maxChickens;
130
nChickens = maxChickens;
131
System.out.println (" Canteen <- " + chefId
132
+ " : full up ... sending back " + sendBack);
136
System.out.println (" Canteen <- " + chefId + " : more chickens ... "
137
+ nChickens + " now available");