-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathAsyncJobMonitor.java
More file actions
85 lines (72 loc) · 2.38 KB
/
AsyncJobMonitor.java
File metadata and controls
85 lines (72 loc) · 2.38 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
/**
*
*/
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class AsyncJobMonitor {
private static final long INIT_TIME = System.currentTimeMillis();
private Queue<Job> queue = new ConcurrentLinkedQueue<>();
private Map<String, Long> endMap = new ConcurrentHashMap<>();
public void start(Job job) {
queue.add(job);
}
public void end(String id, long end) {
endMap.put(id, end);
cleanQueue();
}
private void cleanQueue() {
while (!queue.isEmpty() && endMap.containsKey(queue.peek().id)) {
Job curr = queue.poll();
curr.end = endMap.get(curr.id);
endMap.remove(curr.id);
printJob(curr);
}
}
private void printJob(Job job) {
System.out.println(String.format("now: %s, job: %s, start: %s, end: %s",
System.currentTimeMillis() - INIT_TIME,
job.id,
job.start - INIT_TIME,
job.end - INIT_TIME));
}
static class Job {
String id;
long start;
long end;
Job(String id, long start) {
this.id = id;
this.start = start;
}
}
public static void main(String[] args) {
AsyncJobMonitor monitor = new AsyncJobMonitor();
ExecutorService executorService = Executors.newFixedThreadPool(5);
String[] ids = new String[]{"a", "b", "c", "d", "e"};
long[] times = new long[]{5, 2, 5, 4, 1};
for (int i=0; i<5; i++) {
String id = ids[i];
long time = times[i];
executorService.execute(() -> {
Job job = new Job(id, System.currentTimeMillis());
monitor.start(job);
try {
TimeUnit.SECONDS.sleep(time);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
monitor.end(id, System.currentTimeMillis());
});
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
executorService.shutdown();
}
}