-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05.DesignUniqueIDGeneratorService.java
More file actions
68 lines (60 loc) · 2.06 KB
/
05.DesignUniqueIDGeneratorService.java
File metadata and controls
68 lines (60 loc) · 2.06 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* -------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* --------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* ----------------------------------------------------------- */
import java.util.concurrent.locks.ReentrantLock;
class UniqueIDGenerator {
private final long machineId;
private long sequence = 0L;
private long lastTimestamp = -1L;
private final ReentrantLock lock = new ReentrantLock();
public UniqueIDGenerator(long machineId) {
this.machineId = machineId & 0x3FF; // 10 bits for machine ID
}
private long currentMillis() {
return System.currentTimeMillis();
}
private long waitNextMillis(long lastTs) {
long ts = currentMillis();
while (ts <= lastTs) {
ts = currentMillis();
}
return ts;
}
public long getId() {
lock.lock();
try {
long ts = currentMillis();
if (ts == lastTimestamp) {
sequence = (sequence + 1) & 0xFFF; // 12-bit sequence
if (sequence == 0) {
ts = waitNextMillis(lastTimestamp);
}
} else {
sequence = 0;
}
lastTimestamp = ts;
return (ts << 22) | (machineId << 12) | sequence;
} finally {
lock.unlock();
}
}
}
public class IDGeneratorDemo {
public static void main(String[] args) {
UniqueIDGenerator generator = new UniqueIDGenerator(42);
System.out.println("Generating 5 unique IDs:");
for (int i = 0; i < 5; i++) {
long uid = generator.getId();
System.out.println(uid);
}
}
}