annotate src/alice/test/dpp/codesegment/TakeForkCodeSegment.java @ 20:6018e62cb198 dead lock dpp

modified
author one
date Sun, 09 Dec 2012 23:09:04 +0900
parents 7e91e919a7de
children fd370b35fac1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
1 package alice.test.dpp.codesegment;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
2
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
3 import alice.codesegment.CodeSegment;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
4 import alice.datasegment.CommandType;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
5 import alice.datasegment.Receiver;
20
6018e62cb198 modified
one
parents: 15
diff changeset
6 import alice.test.dpp.datasegment.Fork;
6018e62cb198 modified
one
parents: 15
diff changeset
7 import alice.test.dpp.datasegment.Philosophy;
5
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
8
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
9 public class TakeForkCodeSegment extends CodeSegment {
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
10
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
11 Receiver arg1 = ids.create(CommandType.TAKE);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
12 Receiver arg2 = ids.create(CommandType.TAKE);
14
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
13 Receiver arg3 = ids.create(CommandType.TAKE);
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
14
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
15 public TakeForkCodeSegment(String pName, String rFork, String lFork) {
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
16 arg1.setKey(pName);
15
7e91e919a7de modified
one
parents: 14
diff changeset
17 // dead lock
14
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
18 arg2.setKey(rFork);
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
19 arg3.setKey(lFork);
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
20 }
5
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
21
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
22 @Override
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
23 public void run() {
14
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
24 Philosophy phil = arg1.asClass(Philosophy.class);
5
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
25 Fork rightFork = arg2.asClass(Fork.class);
14
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
26 Fork leftFork = arg3.asClass(Fork.class);
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
27 System.out.println(phil.getName() + " is trying to take the fork.");
15
7e91e919a7de modified
one
parents: 14
diff changeset
28
14
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
29 if (leftFork.getFork(phil.getName()) && rightFork.getFork(phil.getName()) ) {
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
30 phil.setState(Philosophy.State.EAT);
5
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
31 } else {
14
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
32 leftFork.onTheTable(phil.getName());
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
33 rightFork.onTheTable(phil.getName());
5
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
34 }
15
7e91e919a7de modified
one
parents: 14
diff changeset
35
14
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
36 new ScheduleCodeSegment(phil.getName());
33727db069c2 add ScheduleCodeSegment. modified CodeSegment
one
parents: 12
diff changeset
37 ods.update("local", phil.getName(), phil);
5
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
38 ods.update("local", leftFork.getForkName(), leftFork);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
39 ods.update("local", rightFork.getForkName(), rightFork);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
40 }
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
41 }