-
Notifications
You must be signed in to change notification settings - Fork 1
/
MetricAggregation.java
95 lines (72 loc) · 2.77 KB
/
MetricAggregation.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
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
package com.jyotindersingh;
import java.util.Random;
public class MetricAggregation {
public static void main(String[] args) {
Metrics metrics = new Metrics();
BusinessLogic businessLogicThread1 = new BusinessLogic(metrics);
BusinessLogic businessLogicThread2 = new BusinessLogic(metrics);
MetricsPrinter metricsPrinter = new MetricsPrinter(metrics);
businessLogicThread1.start();
businessLogicThread2.start();
metricsPrinter.start();
}
// MetricsPrinter runs in parallel to our BusinessLogic,
// and capture the average time the business logic is taking us and print it to the screen
public static class MetricsPrinter extends Thread {
private Metrics metrics;
public MetricsPrinter(Metrics metrics) {
this.metrics = metrics;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
double currentAverage = metrics.getAverage();
// Since getAVerage is not synchronized, we can be a 100% sure
// that Metrics printer will not slow down the business logic.
System.out.println("Current Average is " + currentAverage);
}
}
}
public static class BusinessLogic extends Thread {
private Metrics metrics;
private Random random = new Random();
public BusinessLogic(Metrics metrics) {
this.metrics = metrics;
}
@Override
public void run() {
while (true) {
long start = System.currentTimeMillis();
try {
Thread.sleep(random.nextInt(10));
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
metrics.addSample(end - start);
}
}
}
public static class Metrics {
// Count and average are variables that are going to be shared by multiple
// threads because multiple threads share the metrics object.
private long count = 0;
private volatile double average = 0.0;
public synchronized void addSample(long sample) {
double currentSum = average * count;
count++;
average = (currentSum + sample) / count;
}
// Assignments and reads from primitive types are atomic
// However average is a double, which is not thread safe,
// hence we declared it as volatile above.
public double getAverage() {
return average;
}
}
}