time-based UUIDs
This commit is contained in:
parent
04d697ecb2
commit
a3e8cddc8f
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.timebaseduuid;
|
||||
|
||||
import com.fasterxml.uuid.Generators;
|
||||
import com.github.f4b6a3.uuid.UuidCreator;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public class JavaUUIDCreator {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
int threadCount = 128;
|
||||
int iterationCount = 100_000;
|
||||
ConcurrentMap<UUID, Long> uuidMap = new ConcurrentHashMap<>();
|
||||
AtomicLong collisionCount = new AtomicLong();
|
||||
long startNanos = System.nanoTime();
|
||||
CountDownLatch endLatch = new CountDownLatch(threadCount);
|
||||
|
||||
for (long i = 0; i < threadCount; i++) {
|
||||
final long threadId = i;
|
||||
new Thread(() -> {
|
||||
for (long j = 0; j < iterationCount; j++) {
|
||||
UUID uuid = Generators.timeBasedGenerator().generate();
|
||||
Long existingUUID = uuidMap.put(uuid, (threadId * iterationCount) + j);
|
||||
if(existingUUID != null) {
|
||||
collisionCount.incrementAndGet();
|
||||
}
|
||||
}
|
||||
endLatch.countDown();
|
||||
}).start();
|
||||
}
|
||||
|
||||
endLatch.await();
|
||||
System.out.println(threadCount * iterationCount + " UUIDs generated, " + collisionCount + " collisions in "
|
||||
+ TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos) + "ms");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.timebaseduuid;
|
||||
|
||||
import com.github.f4b6a3.uuid.UuidCreator;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public class UUIDCreator {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
int threadCount = 128;
|
||||
int iterationCount = 100_000;
|
||||
ConcurrentMap<UUID, Long> uuidMap = new ConcurrentHashMap<>();
|
||||
AtomicLong collisionCount = new AtomicLong();
|
||||
long startNanos = System.nanoTime();
|
||||
CountDownLatch endLatch = new CountDownLatch(threadCount);
|
||||
|
||||
for (long i = 0; i < threadCount; i++) {
|
||||
final long threadId = i;
|
||||
new Thread(() -> {
|
||||
for (long j = 0; j < iterationCount; j++) {
|
||||
UUID uuid = UuidCreator.getTimeBased();
|
||||
Long existingUUID = uuidMap.put(uuid, (threadId * iterationCount) + j);
|
||||
if(existingUUID != null) {
|
||||
collisionCount.incrementAndGet();
|
||||
}
|
||||
}
|
||||
endLatch.countDown();
|
||||
}).start();
|
||||
}
|
||||
|
||||
endLatch.await();
|
||||
System.out.println(threadCount * iterationCount + " UUIDs generated, " + collisionCount + " collisions in "
|
||||
+ TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos) + "ms");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue