-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathFixedBoxing.java
38 lines (28 loc) · 960 Bytes
/
FixedBoxing.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
package memory;
import java.util.concurrent.locks.LockSupport;
public class FixedBoxing {
private static volatile double sensorValue = Double.NaN;
private static void readSensor() {
while(true) {
sensorValue = Math.random();
}
}
private static void processSensorValue(double value) {
if(!Double.isNaN(value)) {
// Be warned: may take more than one usec on some machines, especially Windows
LockSupport.parkNanos(1000);
}
}
public static void main(String[] args) {
int iterations = args.length > 0 ? Integer.parseInt(args[0]) : 1_000_000;
initSensor();
for(int i = 0; i < iterations; i ++) {
processSensorValue(sensorValue);
}
}
private static void initSensor() {
Thread sensorReader = new Thread(FixedBoxing::readSensor);
sensorReader.setDaemon(true);
sensorReader.start();
}
}