KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Active BoundedBuffer
35  */

36 public class BoundedBuffer implements org.objectweb.proactive.RunActive {
37
38   private String JavaDoc buffer[]; // The buffer containing the datas
39
private int size; // The buffer's capacity
40
private int count; // The number of used cells
41
private int in; // The next cell to be read
42
private int out; // The next cell to be written
43
private ActiveDisplay display;
44
45
46   /**
47    * The mandatory no-args constructor.
48    */

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

56   public BoundedBuffer(int size, ActiveDisplay display) {
57     buffer = new String JavaDoc[size];
58     count = 0;
59     in = 0;
60     out = 0;
61     this.size = size;
62     this.display = display;
63   }
64
65
66   /**
67    * Put data in the buffer
68    * Note that this method doesn't contain _any_ synchronization code
69    */

70   public String JavaDoc put(String JavaDoc str) {
71     buffer[in] = str;
72     count++;
73     display.update(in, str);
74     in = (in + 1) % size;
75     display.setIn(in);
76     return "ok";
77   }
78
79
80   /**
81    * Get data from the buffer
82    * Note that this method doesn't contain _any_ synchronization code
83    */

84   public String JavaDoc get() {
85     String JavaDoc str;
86     str = buffer[out];
87     buffer[out] = null;
88     display.update(out, null);
89     out = (out + 1) % size;
90     display.setOut(out);
91     count--;
92     return str;
93   }
94
95
96   /**
97    * The only synchronization method
98    */

99   public void runActivity(org.objectweb.proactive.Body body) {
100     org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
101     while (body.isActive()) {
102       if (count == 0) {
103         // if the buffer is empty
104
service.blockingServeOldest("put"); // Serve the first buffer.put call
105
} else if (count == size) {
106         // if the buffer is full
107
service.blockingServeOldest("get"); // Serve the first buffer.get call
108
} else {
109         // if the buffer is neither empty nor full
110
service.blockingServeOldest(); // Serve the first buffer.xxx call
111
}
112     }
113   }
114 }
115
116
Popular Tags