1 31 package org.objectweb.proactive.examples.philosophers; 32 33 public class Philosopher implements org.objectweb.proactive.RunActive { 34 35 38 protected int id; 39 42 protected boolean autopilot; 43 46 protected boolean hasBothForks; 47 50 protected Table table; 51 54 protected DinnerLayout layout; 55 56 59 public Philosopher() { 60 } 61 62 63 66 public Philosopher(int id, Table table, DinnerLayout layout) { 67 this.id = id; 68 this.table = table; 69 this.layout = layout; 70 autopilot = false; 71 hasBothForks = false; 72 } 73 74 75 78 public void getForks() { 79 layout.update(id, 1); table.getForks(id); 82 hasBothForks = true; 83 } 84 85 86 89 public void putForks() { 90 table.putForks(id); 91 hasBothForks = false; 92 } 93 94 95 98 public void toggle() { 99 autopilot = !autopilot; 100 } 101 102 103 106 public void runActivity(org.objectweb.proactive.Body body) { 107 org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body); 108 while (body.isActive()) { 109 if (autopilot) { 111 if (hasBothForks) { 112 putForks(); 114 } else { 115 getForks(); 117 } 118 service.serveOldest("toggle"); try { 120 Thread.sleep((int)(Math.random() * (double)2000) + 1000); 121 } catch (InterruptedException e) {} 122 } else { 123 service.serveOldest(); 125 } 126 try { 127 Thread.sleep(50); 128 } catch (InterruptedException e) {} 129 } 130 } 131 } 132 133 | Popular Tags |