1 31 package org.objectweb.proactive.examples.philosophers; 32 33 import org.objectweb.proactive.ObjectForSynchronousCall; 34 35 38 public class Table implements org.objectweb.proactive.RunActive { 39 40 43 protected boolean[] forks; 44 47 protected DinnerLayout layout; 48 49 50 53 public Table() { 54 } 55 56 57 61 public Table(DinnerLayout layout) { 62 this.layout = layout; 63 forks = new boolean[5]; 64 } 65 66 67 72 public void putForks(int id) { 73 int nextId = (id+1) % forks.length; 75 forks[id] = forks[nextId] = false; 76 layout.updateFork(id,3); 77 layout.updateFork(nextId,3); 78 layout.update(id, 0); 79 } 80 81 82 88 public ObjectForSynchronousCall getForks(int id) { 89 int nextId = (id+1) % forks.length; 90 forks[id] = forks[nextId] = true; 91 layout.update(id, 2); 92 layout.updateFork(id,4); 93 layout.updateFork(nextId,4); 94 return new ObjectForSynchronousCall(); 95 } 96 97 98 101 private boolean mayEat(int id) { 102 int nextId = (id+1) % forks.length; 103 return (forks[id] == false && forks[nextId] == false); } 105 106 107 111 public void runActivity(org.objectweb.proactive.Body body) { 112 org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body); 113 GetForkRequestFilter getForkRequestFilter = new GetForkRequestFilter(); 114 while (body.isActive()) { 115 service.serveAll(getForkRequestFilter); 116 service.serveOldest("putForks"); 117 try { 118 Thread.sleep(600); 119 } catch (InterruptedException e) {} 120 } 121 } 122 123 124 128 private class GetForkRequestFilter implements org.objectweb.proactive.core.body.request.RequestFilter { 129 130 public GetForkRequestFilter() { 131 } 132 133 public boolean acceptRequest(org.objectweb.proactive.core.body.request.Request request) { 134 if (! request.getMethodName().equals("getForks")) return false; 135 int place = ((Integer )request.getParameter(0)).intValue(); 136 if (mayEat(place)) { 137 return true; 139 } else { 140 return false; 141 } 142 } 143 } 145 } | Popular Tags |