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/deliver |_________| supply |______|
44
//| This time, although Philsopher 0 is just as greedy, no one starves.
47
import org.jcsp.lang.*;
52
class Canteen implements CSProcess {
54
//The Canteen is an active object -- a pure SERVER process for its `supply'
55
//and `service'/`deliver' Channels, giving priority to the former.
57
//Philosphers eat chickens. They queue up at the Canteen on its `service'
58
//Channel. They only get served when chickens are available -- otherwise,
59
//they just have to wait. Once they have got `service', they are dispensed
60
//a chicken down the `deliver' Channel.
62
//The Chef cooks chickens. When a batch ready is ready, he/she queues up at
63
//the Canteen on its `supply' Channel. Setting down the batch takes around
64
//3 seconds and the Chef is made to hang about this has happened.
66
private final AltingChannelInputInt service; // shared from all Philosphers (any-1)
67
private final ChannelOutputInt deliver; // shared to all Philosphers (but only used 1-1)
68
private final AltingChannelInputInt supply; // from the Chef (1-1)
70
public Canteen (AltingChannelInputInt service, ChannelOutputInt deliver,
71
AltingChannelInputInt supply) {
72
this.service = service;
73
this.deliver = deliver;
79
final Alternative alt = new Alternative (new Guard[] {supply, service});
80
final boolean[] precondition = {true, false};
82
final int SERVICE = 1;
84
final CSTimer tim = new CSTimer ();
88
System.out.println (" Canteen : starting ... ");
90
precondition[SERVICE] = (nChickens > 0);
91
switch (alt.fairSelect (precondition)) {
93
int value = supply.read (); // new batch of chickens from the Chef
94
System.out.println (" Canteen : ouch ... make room ... this dish is very hot ... ");
95
tim.after (tim.read () + 3000); // this takes 3 seconds to put down
97
System.out.println (" Canteen : more chickens ... " +
98
nChickens + " now available ... ");
99
supply.read (); // let the Chef get back to cooking
102
service.read (); // Philosopher wants a chicken
103
System.out.println (" Canteen : one chicken coming down ... " +
104
(nChickens - 1) + " left ... ");
105
deliver.write (1); // serve one chicken