This commit is contained in:
alexandru.borza 2023-03-24 22:44:22 +02:00
parent e14feb5351
commit a93d82d800
5 changed files with 69 additions and 43 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

@ -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<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++) {
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

@ -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());
}
}

View File

@ -9,7 +9,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
public class UUIDCreator {
public class UUIDCreatorBenchmark {
public static void main(String[] args) throws InterruptedException {

View File

@ -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());
}
}