diff --git a/core-java-modules/core-java-uuid/pom.xml b/core-java-modules/core-java-uuid/pom.xml index b97db174b4..c0e93c1d32 100644 --- a/core-java-modules/core-java-uuid/pom.xml +++ b/core-java-modules/core-java-uuid/pom.xml @@ -24,6 +24,21 @@ log4j-over-slf4j ${org.slf4j.version} + + com.github.f4b6a3 + uuid-creator + 5.2.0 + + + com.fasterxml.uuid + java-uuid-generator + 4.1.0 + + + com.github.f4b6a3 + tsid-creator + 5.2.3 + @@ -142,4 +157,4 @@ 3.0.0-M1 - \ No newline at end of file + diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java new file mode 100644 index 0000000000..20b2c127bd --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java @@ -0,0 +1,42 @@ +package com.baeldung.timebaseduuid; + +import com.fasterxml.uuid.Generators; + +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +public class JavaUUIDCreatorBenchmark { + +public static void main(String[] args) throws InterruptedException { + + int threadCount = 128; + int iterationCount = 100_000; + Map uuidMap = new ConcurrentHashMap<>(); + AtomicLong collisionCount = new AtomicLong(); + long startNanos = System.nanoTime(); + CountDownLatch endLatch = new CountDownLatch(threadCount); + + for (long i = 0; i < threadCount; i++) { + 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/JavaUUIDCreatorExample.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorExample.java new file mode 100644 index 0000000000..b59d7e236a --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorExample.java @@ -0,0 +1,13 @@ +package com.baeldung.timebaseduuid; + +import com.fasterxml.uuid.Generators; + +public class JavaUUIDCreatorExample { + + public static void main(String[] args) { + System.out.println("UUID Version 1: " + Generators.timeBasedGenerator().generate()); + System.out.println("UUID Version 6: " + Generators.timeBasedReorderedGenerator().generate()); + System.out.println("UUID Version 7: " + Generators.timeBasedEpochGenerator().generate()); + + } +} diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java new file mode 100644 index 0000000000..d93cd73a25 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java @@ -0,0 +1,42 @@ +package com.baeldung.timebaseduuid; + +import com.github.f4b6a3.uuid.UuidCreator; + +import java.util.Map; +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 UUIDCreatorBenchmark { + + public static void main(String[] args) throws InterruptedException { + + int threadCount = 128; + int iterationCount = 100_000; + Map uuidMap = new ConcurrentHashMap<>(); + AtomicLong collisionCount = new AtomicLong(); + long startNanos = System.nanoTime(); + CountDownLatch endLatch = new CountDownLatch(threadCount); + + for (long i = 0; i < threadCount; i++) { + 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"); + } +} diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java new file mode 100644 index 0000000000..fad2f55c93 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java @@ -0,0 +1,13 @@ +package com.baeldung.timebaseduuid; + +import com.github.f4b6a3.uuid.UuidCreator; + +public class UUIDCreatorExample { + + public static void main(String[] args) { + System.out.println("UUID Version 1: " + UuidCreator.getTimeBased()); + System.out.println("UUID Version 6: " + UuidCreator.getTimeOrdered()); + System.out.println("UUID Version 7: " + UuidCreator.getTimeOrderedEpoch()); + } +} +