~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/jcsp-demos/wotNoChickens/callChannel/CallPhil.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    //////////////////////////////////////////////////////////////////////
 
2
    //                                                                  //
 
3
    //  JCSP ("CSP for Java") Libraries                                 //
 
4
    //  Copyright (C) 1996-2008 Peter Welch and Paul Austin.            //
 
5
    //                2001-2004 Quickstone Technologies Limited.        //
 
6
    //                                                                  //
 
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       //
 
11
    //  version.                                                        //
 
12
    //                                                                  //
 
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     //
 
17
    //  details.                                                        //
 
18
    //                                                                  //
 
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.                                     //
 
23
    //                                                                  //
 
24
    //  Author contact: P.H.Welch@kent.ac.uk                             //
 
25
    //                                                                  //
 
26
    //                                                                  //
 
27
    //////////////////////////////////////////////////////////////////////
 
28
 
 
29
 
 
30
import org.jcsp.lang.*;
 
31
 
 
32
/**
 
33
 * @author P.H. Welch
 
34
 */
 
35
class CallPhil implements CSProcess {
 
36
 
 
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
 
39
  //indefinitely.
 
40
  //
 
41
  //Except, that is, for Philosopher 0 ...  who refuses to think and just keeps
 
42
  //going to the Canteen.
 
43
  //
 
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.
 
47
 
 
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;
 
53
 
 
54
  public CallPhil (String id, CallCanteen.Service service,
 
55
                   int thinkTime, int eatTime, boolean greedy) {
 
56
    this.id = id;
 
57
    this.service = service;
 
58
    this.thinkTime = thinkTime;
 
59
    this.eatTime = eatTime;
 
60
    this.greedy = greedy;
 
61
  }
 
62
 
 
63
  public void run () {
 
64
    final CSTimer tim = new CSTimer ();
 
65
    int nEaten = 0;
 
66
    while (true) {
 
67
      // everyone, unless greedy, has a little think
 
68
      if (! greedy) {
 
69
        System.out.println ("   Phil " + id + " : thinking ... ");
 
70
        tim.sleep (thinkTime);   // thinking
 
71
      }
 
72
      // want chicken
 
73
      System.out.println ("   Phil " + id + " : gotta eat ... ");
 
74
      service.takeChicken (id);
 
75
      nEaten++;
 
76
      System.out.println ("   Phil " + id + " : mmm ... that's good [" + nEaten + " so far]");
 
77
      tim.sleep (eatTime);       // eating
 
78
    }
 
79
  }
 
80
 
 
81
}