-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBusyThread.java
More file actions
36 lines (33 loc) · 1.06 KB
/
Copy pathBusyThread.java
File metadata and controls
36 lines (33 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
public class BusyThread extends Thread{
private double load;
private long duration;
/**
* Constructor which creates the thread
* @param name Name of this thread
* @param load Load % that this thread should generate
* @param duration Duration that this thread should generate the load for
*/
public BusyThread(String name, double load, long duration) {
super(name);
this.load = load;
this.duration = duration;
}
/**
* Generates the load when run
*/
@Override
public void run() {
long startTime = System.currentTimeMillis();
try {
// Loop for the given duration
while (System.currentTimeMillis() - startTime < duration) {
// Every 100ms, sleep for the percentage of unladen time
if (System.currentTimeMillis() % 100 == 0) {
Thread.sleep((long) Math.floor((1 - load) * 100));
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}