This commit is contained in:
alexandru.borza 2023-03-24 22:44:22 +02:00
parent a3e8cddc8f
commit 6dcdba8b3f
3 changed files with 1 additions and 84 deletions

View File

@ -1,42 +0,0 @@
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");
}
}

View File

@ -1,41 +0,0 @@
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");
}
}

View File

@ -22,7 +22,7 @@ public class UUIDCreatorBenchmark {
CountDownLatch endLatch = new CountDownLatch(threadCount);
for (long i = 0; i < threadCount; i++) {
long threadId = i;
final long threadId = i;
new Thread(() -> {
for (long j = 0; j < iterationCount; j++) {
UUID uuid = UuidCreator.getTimeBased();