KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > boundedbuffer > ConsumerProducer


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.boundedbuffer;
32
33 /**
34  * Simple Active consumer
35  */

36 public abstract class ConsumerProducer implements org.objectweb.proactive.RunActive {
37
38   protected String JavaDoc name;
39   protected ConsumerProducerListener listener;
40   protected BoundedBuffer buffer;
41   protected boolean isActive;
42   protected boolean isSuspended;
43   protected boolean requestChange;
44
45
46   /**
47    * The mandatory empty, no-arg constructor.
48    */

49   public ConsumerProducer() {
50   }
51
52
53   /**
54    * The effective constructor
55    */

56   public ConsumerProducer(String JavaDoc name, ConsumerProducerListener listener, BoundedBuffer buffer) {
57     this.name = name;
58     this.buffer = buffer;
59     this.listener = listener;
60     requestChange = false;
61     isSuspended = true;
62     isActive = true;
63   }
64
65
66   /**
67    * Toggle the consumer or producer 's activity
68    */

69   public void toggle() {
70     requestChange = true;
71   }
72
73
74   /**
75    * Kill the consumer/producer
76    */

77   public void done() {
78     isActive = false;
79   }
80
81
82   /**
83    * The only synchronization method
84    */

85   public void runActivity(org.objectweb.proactive.Body body) {
86     org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
87     while (isActive) {
88       // Allow the display to toggle or kill the consumer
89
service.serveOldest();
90       boolean wasSuspended = isSuspended;
91       if (requestChange) {
92         isSuspended = !isSuspended;
93         requestChange = false;
94       }
95       // Try to get some datas
96
doStuff(wasSuspended);
97       long l = 500 + (long)(Math.random() * 1000);
98       //listener.displayMessage(name+" now sleeping for "+l+" ms");
99
try {
100         Thread.sleep(l);
101       } catch (InterruptedException JavaDoc e) {}
102     }
103   }
104
105
106   protected abstract void doStuff(boolean wasSuspended);
107 }
Popular Tags