forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerOrder.java
More file actions
42 lines (29 loc) · 1.14 KB
/
CustomerOrder.java
File metadata and controls
42 lines (29 loc) · 1.14 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
package modern.challenge;
import java.util.Random;
import java.util.logging.Logger;
public class CustomerOrder implements Runnable {
private static final Logger logger = Logger.getLogger(CustomerOrder.class.getName());
private static final Random rnd = new Random();
private static final ThreadLocal<Order> customerOrder = new ThreadLocal<>();
private final int customerId;
public CustomerOrder(int customerId) {
this.customerId = customerId;
}
@Override
public void run() {
logger.info(() -> "Given customer id: " + customerId
+ " | " + customerOrder.get()
+ " | " + Thread.currentThread().getName());
customerOrder.set(new Order(customerId));
try {
Thread.sleep(rnd.nextInt(2000));
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
logger.severe(() -> "Exception: " + ex);
}
logger.info(() -> "Given customer id: " + customerId
+ " | " + customerOrder.get()
+ " | " + Thread.currentThread().getName());
customerOrder.remove();
}
}