-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFiveStates.java
More file actions
165 lines (146 loc) · 3.41 KB
/
Copy pathFiveStates.java
File metadata and controls
165 lines (146 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/**
* FiveStates.
* @author Liu, xinwei
*
*/
public class FiveStates {
private Scanner in = new Scanner(System.in);
/** New */
private Queue<State> newQueue = new LinkedList<State>();
/** Ready*/
private Queue<State> ready = new LinkedList<State>();
/** Running*/
private Queue<State> running = new LinkedList<State>();
/** Blocked*/
private Queue<State> blocked = new LinkedList<State>();
/** Exit */
private Queue<State> exit = new LinkedList<State>();
/**
* create a state.
*/
public void create() {
System.out.println("please input demand:");
State state = new State(in.next());
newQueue.add(state);
System.out.println("demand<" + state.getName() + ">has been created.");
}
/**
* new to ready.
*/
public void admit() {
State state = newQueue.poll();
judge(ready, state);
}
/**
* Ready to Running.
*/
public void dispatch() {
if (running.size() > 0) {
System.out.println("there is a state running!");
return;
}
State state = ready.poll();
judge(running, state);
}
/**
* Running to Ready.
*/
public void timeout() {
State state = running.poll();
judge(ready, state);
//dispatch automatic.
dispatch();
}
/**
* Running to Blocked.
*/
public void eventWait() {
State state = running.poll();
judge(blocked, state);
//dispatch automatic.
dispatch();
}
/**
* Blocked to Ready.
*/
public void eventOccurs() {
State state = blocked.poll();
judge(ready, state);
if (running.size() == 0 && ready.size() > 0) {
dispatch();
}
}
/**
* running to exit.
*/
public void release() {
State state = running.poll();
judge(exit, state);
//dispatch automatic.
dispatch();
}
/**
* Judge.
* @param queue
* @param state
*/
public void judge(Queue<State> queue, State state) {
if (state != null) {
queue.add(state);
}
}
/**
* show all states.
*/
public void showStates() {
System.out.println("The states Now:");
System.out.println("New" + newQueue.toString());
System.out.println("Ready" + ready.toString());
System.out.println("Running" + running.toString());
System.out.println("Blocked" + blocked.toString());
System.out.println("Exit" + exit.toString());
}
/**
* show the window.
*/
public void showWindow() {
System.out.println("***********************************FiveStates***********************************************");
System.out.println("1.Create 2.Admit 3.Dispatch 4.TimeOut 5.EventOccurs 6.EventWait 7.release 0.Exit");
System.out.println("********************************************************************************************");
}
/**
* controllor.
*/
public void control() {
showWindow();
String temp;
while (!"0".equals((temp = in.next()))) {
switch (temp) {
case "1": create(); break;
case "2": admit(); break;
case "3": dispatch(); break;
case "4": timeout(); break;
case "5": eventWait(); break;
case "6": eventOccurs(); break;
case "7": release(); break;
case "0": break;
default : {
showWindow();
System.out.println("Your demand is wrong, please input again:");
continue;
}
}
showStates();
showWindow();
}
System.out.println("Thanks for use.");
}
public static void main(String[] args) {
FiveStates fiveStates = new FiveStates();
fiveStates.control();
fiveStates.in.close();
}
}