-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.java
More file actions
42 lines (41 loc) · 1.06 KB
/
Thread.java
File metadata and controls
42 lines (41 loc) · 1.06 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
import java.util.Random;
public abstract class Thread {
Method currentMethod;
private int threadId;
//static final int nthreads = 10;
public Thread(int id) {
threadId = id;
}
/* public int getNewThreadId() {
return threadId++;
}*/
public int getThreadId() {
return threadId;
}
public abstract Method nextMethod();
public abstract String getState();
public void step(int line) {
if (currentMethod == null) {
currentMethod = nextMethod();
currentMethod.log(line);
} else {
currentMethod.step(line);
}
if (currentMethod.isCompleted()) {
currentMethod = null;
}
}
public boolean betweenMethods() {
return currentMethod == null;
}
static void iterate(Thread[] threads) {
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < threads.length; j++) {
threads[j].step(threads.length * i + j);
if (threads[j].betweenMethods()) {
System.out.println(threads[j].getState());
}
}
}
}
}