-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriceService.java
More file actions
36 lines (28 loc) · 1.05 KB
/
PriceService.java
File metadata and controls
36 lines (28 loc) · 1.05 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
package externalLegacyCodeNotUnderOurControl;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import static externalLegacyCodeNotUnderOurControl.PrintlnWithThreadname.println;
// DON'T CHANGE... it's legacy code and out of our control
public class PriceService {
private static final int DEFAULT_DELAY_SECONDS = 1;
private final int delay_seconds;
public PriceService() {
this(DEFAULT_DELAY_SECONDS);
}
public PriceService(int delay_seconds) {
this.delay_seconds = delay_seconds;
}
public int getPrice() {
println("Calculating price delayed by "+ delay_seconds+ " s");
try {
TimeUnit.SECONDS.sleep(delay_seconds);
int result = ThreadLocalRandom.current().nextInt(1, 100);
println("The price is " + result);
return result;
} catch (InterruptedException e) {
println("PriceService#getPrice() was interrupted.");
Thread.currentThread().interrupt();
return -1;
}
}
}