-
Notifications
You must be signed in to change notification settings - Fork 3
/
WeatherCenter.java
45 lines (36 loc) · 1.05 KB
/
WeatherCenter.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
package completableFuture;
import net.jcip.annotations.ThreadSafe;
import java.util.Random;
/**
* Created by kennylbj on 2017/4/30.
* {@code WeatherCenter} offers a synchronous API for getting
* temperature and moisture at a certain location.
* These query operations may take some time to execute.
*/
@ThreadSafe
final class WeatherCenter {
private static final int RANGE = 40;
private static final Random random = new Random(System.nanoTime());
private final String name;
WeatherCenter(String name) {
this.name = name;
}
int getTemperature(String location) {
delayFixDuration(1);
return (location.hashCode() + name.hashCode()) % RANGE;
}
double getMoisture(String location) {
delayFixDuration(1);
return random.nextDouble();
}
String getName() {
return name;
}
private void delayFixDuration(int seconds) {
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}