1 31 package org.objectweb.proactive.examples.boundedbuffer; 32 33 36 public class BoundedBuffer implements org.objectweb.proactive.RunActive { 37 38 private String buffer[]; private int size; private int count; private int in; private int out; private ActiveDisplay display; 44 45 46 49 public BoundedBuffer() { 50 } 51 52 53 56 public BoundedBuffer(int size, ActiveDisplay display) { 57 buffer = new String [size]; 58 count = 0; 59 in = 0; 60 out = 0; 61 this.size = size; 62 this.display = display; 63 } 64 65 66 70 public String put(String 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 84 public String get() { 85 String 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 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 service.blockingServeOldest("put"); } else if (count == size) { 106 service.blockingServeOldest("get"); } else { 109 service.blockingServeOldest(); } 112 } 113 } 114 } 115 116 | Popular Tags |