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 38 39 48 public class ActivePrimeContainer implements PrimeNumber, java.io.Serializable , 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 62 public ActivePrimeContainer() { 63 } 64 65 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 82 public void sleep(boolean sleep) { 83 this.sleep = sleep; 84 } 85 86 88 public void runActivity(Body b) { 89 Service service = new Service(b); 90 while (b.isActive()) { 91 if (sleep) { try { 93 Thread.sleep(100); 94 } catch (InterruptedException e) {} 95 service.serveAll("sleep"); 97 } else { 98 for (int i = 0; i < 50; i ++) service.blockingServeOldest(); } 100 101 int queueSize = b.getRequestQueue().size(); if (!isPreviousSleeping && queueSize > 200) { previous.sleep(true); 104 isPreviousSleeping = true; 105 } else if (isPreviousSleeping && queueSize < 100) { previous.sleep(false); 107 isPreviousSleeping = false; 108 } 109 } 110 } 111 112 113 public void tryModulo(long n) { 114 first.tryModulo(n); 115 } 116 117 118 public long getValue() { 119 return first.getValue(); 120 } 121 122 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 |