-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounterThread.java
More file actions
31 lines (29 loc) · 816 Bytes
/
CounterThread.java
File metadata and controls
31 lines (29 loc) · 816 Bytes
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
import java.util.Random;
public class CounterThread extends Thread {
static Counter c = new Counter();
Random rand = new Random();
public CounterThread(int id) {
super(id);
}
public Method nextMethod() {
if(rand.nextInt(2) == 0) {
return new CounterGetAndIncrement(c, getThreadId());
} else {
return new Sleep(rand.nextInt(3), getThreadId());
}
}
public boolean betweenMethods() {
return currentMethod == null;
}
public String getState() {
return "counter=" + c.value;
}
public static void main(String args[]) {
CounterThread threads[] = new CounterThread[5];
for (int i = 0; i < threads.length; i++) {
threads[i] = new CounterThread(i);
}
iterate(threads);
//System.out.println(s);
}
}