-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashSaleService.java
More file actions
51 lines (42 loc) · 1.72 KB
/
Copy pathFlashSaleService.java
File metadata and controls
51 lines (42 loc) · 1.72 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
43
44
45
46
47
48
49
50
51
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import OutOfStockException;
import Product;
public class FlashSaleService {
@SuppressWarnings("unused")
private final Product product;
private final AtomicInteger stock;
// Set derived from ConcurrentHashMap for thread-safe uniqueness check
private final Set successfulUsers = ConcurrentHashMap.newKeySet();
public FlashSaleService(Product product, int initialStock) {
this.product = product;
this.stock = new AtomicInteger(initialStock);
}
public boolean purchase(String userId) throws OutOfStockException, DuplicatePurchaseException {
// 1. Check for duplicate purchase atomically
if (!successfulUsers.add(userId)) {
throw new DuplicatePurchaseException("User " + userId + " has already purchased this item!");
}
// 2. Decrement stock atomically only if stock > 0
while (true) {
int currentStock = stock.get();
if (currentStock <= 0) {
// Rollback user eligibility if stock is empty
successfulUsers.remove(userId);
throw new OutOfStockException("Sale ended! Item is out of stock.");
}
// Atomic Compare-And-Swap (CAS)
if (stock.compareAndSet(currentStock, currentStock - 1)) {
return true; // Purchase successful!
}
// If compareAndSet fails due to another thread modifying stock, loop retries automatically
}
}
public int getRemainingStock() {
return stock.get();
}
public int getTotalSuccessfulPurchases() {
return successfulUsers.size();
}
}