KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > philosophers > Philosopher


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.philosophers;
32
33 public class Philosopher implements org.objectweb.proactive.RunActive {
34
35   /**
36    * The philosopher's ID
37    */

38   protected int id;
39   /**
40    * set to false if the philosopher is interface-driven
41    */

42   protected boolean autopilot;
43   /**
44    * True if the philosopher has both forks
45    */

46   protected boolean hasBothForks;
47   /**
48    * The table's stub
49    */

50   protected Table table;
51   /**
52    * A reference to the layout\
53    */

54   protected DinnerLayout layout;
55
56   /**
57    * No arg constructor
58    */

59   public Philosopher() {
60   }
61
62
63   /**
64    * Real constructor
65    */

66   public Philosopher(int id, Table table, DinnerLayout layout) {
67     this.id = id;
68     this.table = table;
69     this.layout = layout;
70     autopilot = false;
71     hasBothForks = false;
72   }
73
74
75   /**
76    * getForks
77    */

78   public void getForks() {
79     layout.update(id, 1); // set to waiting
80
// do a synchronous call
81
table.getForks(id);
82     hasBothForks = true;
83   }
84
85
86   /**
87    * putForks
88    */

89   public void putForks() {
90     table.putForks(id);
91     hasBothForks = false;
92   }
93
94
95   /**
96    *
97    */

98   public void toggle() {
99     autopilot = !autopilot;
100   }
101
102
103   /**
104    * The live method
105    */

106   public void runActivity(org.objectweb.proactive.Body body) {
107     org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
108     while (body.isActive()) {
109       // Check wether the philosopher is UI-driven or not
110
if (autopilot) {
111         if (hasBothForks) {
112           // drop the forks
113
putForks();
114         } else {
115           // Let's take the forks
116
getForks();
117         }
118         service.serveOldest("toggle"); // Serve any toggle
119
try {
120           Thread.sleep((int)(Math.random() * (double)2000) + 1000);
121         } catch (InterruptedException JavaDoc e) {}
122       } else {
123         // manual mode
124
service.serveOldest();
125       }
126       try {
127         Thread.sleep(50);
128       } catch (InterruptedException JavaDoc e) {}
129     }
130   }
131 }
132
133 //:~Philosopher
134
Popular Tags