Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Oracle Java 7 update 5 UUID race condition
import java.util.List;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.UUID;
class UUIDGenThread extends Thread {
private final Set<UUID> set;
private final int numUUIDS;
public UUIDGenThread(Set<UUID> set, int numUUIDS) {
this.set = set;
this.numUUIDS = numUUIDS;
public void run() {
for(int i=0; i<numUUIDS; ++i) {
UUID uuid = UUID.randomUUID();
if(!set.add(uuid)) System.out.println("Collision: " + uuid);
public class UUIDTest {
public static void main(String[] args) throws InterruptedException {
Set<UUID> set = new ConcurrentSkipListSet<UUID>();
List<Thread> threads = new LinkedList<Thread>();
for(int i=0; i<128; ++i) {
threads.add(new UUIDGenThread(set, 100000));
for(Thread t: threads) t.start();
for(Thread t: threads) t.join();
System.out.println("Set size: " + set.size());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.