comparison src/alice/test/dpp/codesegment/ThinkCodeSegment.java @ 12:bfaaebb50a2e

modified Fork.java
author one
date Sun, 25 Nov 2012 18:49:29 +0900
parents 2794091a7058
children 888388e47fe5
comparison
equal deleted inserted replaced
11:37b981eb4533 12:bfaaebb50a2e
1 package alice.test.dpp.codesegment; 1 package alice.test.dpp.codesegment;
2 2
3 import alice.codesegment.CodeSegment; 3 import alice.codesegment.CodeSegment;
4 import alice.datasegment.CommandType;
5 import alice.datasegment.Receiver;
6 import alice.test.dpp.model.Fork;
4 import alice.test.dpp.model.Philosophy; 7 import alice.test.dpp.model.Philosophy;
5 8
6 public class ThinkCodeSegment extends CodeSegment { 9 public class ThinkCodeSegment extends CodeSegment {
7 10
8 Philosophy phil; 11 Philosophy phil;
12 Receiver arg1 = ids.create(CommandType.TAKE);
13 Receiver arg2 = ids.create(CommandType.TAKE);
9 14
10 public ThinkCodeSegment(Philosophy p) { 15 public ThinkCodeSegment(Philosophy p) {
11 phil = p; 16 phil = p;
17 arg1.setKey(phil.getLeftFork());
18 arg2.setKey(phil.getRightFork());
12 } 19 }
13 20
14 @Override 21 @Override
15 public void run() { 22 public void run() {
23 System.out.println(phil.getName() + " thinking");
24 Fork leftFork = arg1.asClass(Fork.class);
25 Fork rightFork = arg2.asClass(Fork.class);
16 double rand = Math.random(); 26 double rand = Math.random();
17 if (rand < 0.5) { 27 if (rand < 0.5) {
18 try { 28 try {
19 System.out.println(phil + " is thinking.");
20 Thread.sleep(10); 29 Thread.sleep(10);
21 new ThinkCodeSegment(phil); 30 new ThinkCodeSegment(phil);
22 } catch (InterruptedException e) { 31 } catch (InterruptedException e) {
23 e.printStackTrace(); 32 e.printStackTrace();
24 } 33 }
25 } else { 34 } else {
26 new TakeForkCodeSegment(phil); 35 new TakeForkCodeSegment(phil);
27 } 36 }
37
38 ods.update("local", leftFork.getForkName(), leftFork);
39 ods.update("local", rightFork.getForkName(), rightFork);
28 } 40 }
29 41
30 } 42 }