KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > eratosthenes > ActivePrimeContainer


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

38
39 /**
40 * @author Jonathan Streit
41 *
42 * An active container for PrimeNumber objects. The class implements the
43 * PrimeNumber interface so that it can be assigned to the preceeding
44 * numbers' next-field. When the container has reached its maximum size,
45 * it demands creation of a new container.
46 * The container is migratable.
47 */

48 public class ActivePrimeContainer implements PrimeNumber, java.io.Serializable JavaDoc, Slowable, RunActive {
49
50   private PrimeNumber first;
51   private int size;
52   private int maxSize;
53   private boolean sleep;
54   private boolean isPreviousSleeping;
55   private ActivePrimeContainerCreator activePrimeContainerCreator;
56   private PrimeOutputListener outputListener;
57   private Slowable previous;
58
59   /**
60    * Constructor for ActivePrimeContainer.
61    */

62   public ActivePrimeContainer() {
63   }
64
65   /** @param creator the creator of new containers (when this one is full)
66    * @param listener the output listener used to print out new prime numbers
67    * @param maxSize the maximum size of this container before a new one is created
68    * @param n the first number to store in this container
69    * @param previous the container or source that sends requests to this container
70    * */

71   public ActivePrimeContainer(ActivePrimeContainerCreator creator, PrimeOutputListener listener,
72                                int maxSize, long n, Slowable previous) {
73     super();
74     this.maxSize = maxSize;
75     this.activePrimeContainerCreator = creator;
76     this.outputListener = listener;
77     this.first = newPrimeNumber(n);
78     this.previous = previous;
79   }
80
81   /** Asks this container to sleep in order to reduce the number of requests sent. */
82   public void sleep(boolean sleep) {
83     this.sleep = sleep;
84   }
85   
86   /** Serves requests and looks after the length of the request queue from time to time.
87    * Demands slowing down from previous container if the queue gets to long. */

88   public void runActivity(Body b) {
89     Service service = new Service(b);
90     while (b.isActive()) {
91       if (sleep) { // sleep in order to reduce charge on next container
92
try {
93           Thread.sleep(100);
94         } catch (InterruptedException JavaDoc e) {}
95         service.serveAll("sleep"); // serve all sleep requests: maybe we can wake up
96

97       } else {
98         for (int i = 0; i < 50; i ++) service.blockingServeOldest(); // serve requests
99
}
100       
101       int queueSize = b.getRequestQueue().size(); // test request queue length
102
if (!isPreviousSleeping && queueSize > 200) { // make previous sleep
103
previous.sleep(true);
104         isPreviousSleeping = true;
105       } else if (isPreviousSleeping && queueSize < 100) { // make previous wake up
106
previous.sleep(false);
107         isPreviousSleeping = false;
108       }
109     }
110   }
111
112   /** Asks the first PrimeNumber object in the container to try this number */
113   public void tryModulo(long n) {
114       first.tryModulo(n);
115   }
116
117   /** Returns the value of the first prime number. */
118   public long getValue() {
119     return first.getValue();
120   }
121
122   /** Creates a new PrimeNumber in this container or, when maximum size
123    * has been reached, demands creation of a new container. */

124   public PrimeNumber newPrimeNumber(long n) {
125     size ++;
126     if (size <= maxSize) {
127         outputListener.newPrimeNumberFound(n);
128         return new PrimeNumberImpl(this, n);
129     } else return activePrimeContainerCreator.newActivePrimeContainer(n,
130             (ActivePrimeContainer)ProActive.getStubOnThis());
131   }
132   
133 }
134
Popular Tags