-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityCompete.java
More file actions
137 lines (110 loc) · 3 KB
/
Copy pathPriorityCompete.java
File metadata and controls
137 lines (110 loc) · 3 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
package threadbook.ch06;
public class PriorityCompete extends Object {
private volatile int count;
private boolean yield;
private Thread internalThread;
private volatile boolean noStopRequested;
public PriorityCompete(
String name,
int priority,
boolean yield
) {
count = 0;
this.yield = yield;
noStopRequested = true;
Runnable r = new Runnable() {
public void run() {
try {
runWork();
} catch ( Exception x ) {
// in case ANY exception slips through
x.printStackTrace();
}
}
};
internalThread = new Thread(r, name);
internalThread.setPriority(priority);
}
private void runWork() {
Thread.yield();
while ( noStopRequested ) {
if ( yield ) {
Thread.yield();
}
count++;
for ( int i = 0; i < 1000; i++ ) {
double x = i * Math.PI / Math.E;
}
}
}
public void startRequest() {
internalThread.start();
}
public void stopRequest() {
noStopRequested = false;
}
public int getCount() {
return count;
}
public String getNameAndPriority() {
return internalThread.getName() +
": priority=" + internalThread.getPriority();
}
private static void runSet(boolean yield) {
PriorityCompete[] pc = new PriorityCompete[3];
pc[0] = new PriorityCompete("PC0", 3, yield);
pc[1] = new PriorityCompete("PC1", 6, yield);
pc[2] = new PriorityCompete("PC2", 6, yield);
// let the dust settle for a bit before starting them up
try { Thread.sleep(1000); }
catch ( InterruptedException x ) { }
for ( int i = 0; i < pc.length; i++ ) {
pc[i].startRequest();
}
long startTime = System.currentTimeMillis();
try { Thread.sleep(10000); }
catch ( InterruptedException x ) { }
for ( int i = 0; i < pc.length; i++ ) {
pc[i].stopRequest();
}
long stopTime = System.currentTimeMillis();
// let things settle down again
try { Thread.sleep(1000); }
catch ( InterruptedException x ) { }
int totalCount = 0;
for ( int i = 0; i < pc.length; i++ ) {
totalCount += pc[i].getCount();
}
System.out.println("totalCount=" + totalCount +
", count/ms=" + roundTo(((double) totalCount) /
(stopTime - startTime), 3));
for ( int i = 0; i < pc.length; i++ ) {
double perc = roundTo(100.0 * pc[i].getCount() /
totalCount, 2);
System.out.println(pc[i].getNameAndPriority() +
", " + perc + "%, count=" + pc[i].getCount());
}
}
public static double roundTo(double val, int places) {
double factor = Math.pow(10, places);
return ( (int) ( ( val * factor ) + 0.5 ) ) / factor;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
System.out.println(
"Run without using yield()");
System.out.println(
"=========================");
runSet(false);
System.out.println();
System.out.println("Run using yield()");
System.out.println("=================");
runSet(true);
}
};
Thread t = new Thread(r, "PriorityCompete");
t.setPriority(Thread.MAX_PRIORITY - 1);
t.start();
}
}