Skip to content

Instantly share code, notes, and snippets.

@jadamcrain
Created August 11, 2012 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadamcrain/3324954 to your computer and use it in GitHub Desktop.
Save jadamcrain/3324954 to your computer and use it in GitHub Desktop.
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