KickJava   Java API By Example, From Geeks To Geeks.

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


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

34
35 import org.apache.log4j.Logger;
36 import org.objectweb.proactive.Body;
37 import org.objectweb.proactive.RunActive;
38 import org.objectweb.proactive.Service;
39
40 /** This class sends increasing numbers to the first PrimeNumber for testing.
41  * NumberSource is migratable. pause() serves to pause sending of numbers
42  * but allows the containers to finish their waiting requests.
43  * slowDown() makes the source sleep a little.
44  * @author Jonathan Streit
45  */

46 public class NumberSource implements java.io.Serializable JavaDoc, RunActive, Slowable {
47     
48     static Logger logger = Logger.getLogger(NumberSource.class.getName());
49   private PrimeOutputListener outputListener;
50   private ActivePrimeContainer first;
51   private boolean pause;
52   private boolean sleep;
53   private long currentNumber;
54
55   /**
56    * Constructor for NumberSource.
57    */

58   public NumberSource() {
59     pause = true;
60     currentNumber = 5;
61   }
62
63   public void setFirst(ActivePrimeContainer first) {
64     this.first = first;
65   }
66
67     /** makes the source sleep a little to reduce charge on the first container. */
68   public void sleep(boolean sleep) {
69     this.sleep = sleep;
70   }
71   
72
73   /** Sends increasing numbers to the first PrimeNumber for testing. */
74   public void runActivity(Body b) {
75     Service service = new Service(b);
76     while (b.isActive()) {
77       while (service.hasRequestToServe()) service.serveOldest(); // serve requests (sleep)
78
if (pause || sleep) {
79         try {
80             Thread.sleep(100);
81         } catch (InterruptedException JavaDoc ex) {}
82       } else if (first != null) {
83         logger.info(" Testing numbers from "+currentNumber+" to "+(currentNumber+98));
84         for (int i = 0; i < 100; i ++) { // start several requests at a time in order to increase speed
85
first.tryModulo(currentNumber);
86           currentNumber += 2;
87         }
88       }
89     }
90   }
91
92   /** Pause temporarily */
93   public void pause(boolean p) {
94     pause = p;
95   }
96   
97 }
98
Popular Tags