1 31 package org.objectweb.proactive.examples.boundedbuffer; 32 33 36 public abstract class ConsumerProducer implements org.objectweb.proactive.RunActive { 37 38 protected String name; 39 protected ConsumerProducerListener listener; 40 protected BoundedBuffer buffer; 41 protected boolean isActive; 42 protected boolean isSuspended; 43 protected boolean requestChange; 44 45 46 49 public ConsumerProducer() { 50 } 51 52 53 56 public ConsumerProducer(String 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 69 public void toggle() { 70 requestChange = true; 71 } 72 73 74 77 public void done() { 78 isActive = false; 79 } 80 81 82 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 service.serveOldest(); 90 boolean wasSuspended = isSuspended; 91 if (requestChange) { 92 isSuspended = !isSuspended; 93 requestChange = false; 94 } 95 doStuff(wasSuspended); 97 long l = 500 + (long)(Math.random() * 1000); 98 try { 100 Thread.sleep(l); 101 } catch (InterruptedException e) {} 102 } 103 } 104 105 106 protected abstract void doStuff(boolean wasSuspended); 107 } | Popular Tags |