diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java new file mode 100644 index 0000000000..39308c8982 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java @@ -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 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"); + } +} diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreator.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreator.java new file mode 100644 index 0000000000..8695a54bae --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreator.java @@ -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 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"); + } +}