time-based UUIDs
This commit is contained in:
parent
9089d84d2b
commit
e14feb5351
|
@ -25,6 +25,21 @@
|
|||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.f4b6a3</groupId>
|
||||
<artifactId>uuid-creator</artifactId>
|
||||
<version>5.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.uuid</groupId>
|
||||
<artifactId>java-uuid-generator</artifactId>
|
||||
<version>4.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.f4b6a3</groupId>
|
||||
<artifactId>tsid-creator</artifactId>
|
||||
<version>5.2.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -143,4 +158,4 @@
|
|||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -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