-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPU.java
41 lines (35 loc) · 896 Bytes
/
CPU.java
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
/**
* "Virtual" CPU
*
* This virtual CPU also maintains system time.
*
* @author Greg Gagne - March 2016
*/
public class CPU
{
// the multiplier for simulating executing the task
public final static int MULTIPLIER = 10;
// notion of time by the CPU
private static int time = 0;
/**
* Run the specified task for the specified slice of time.
*/
public static void run(Task task, int slice) {
System.out.println("Will run " + task);
// simulate executing a task by sleeping for the time slice
try {
Thread.sleep(slice * MULTIPLIER);
}
catch (InterruptedException ie) {
System.err.println(ie);
}
// update the current time
time += slice;
}
/**
* Returns the current notion of time
*/
public static int getTime() {
return time;
}
}