public
Created

Oracle Java 7 update 5 UUID race condition

  • Download Gist
UUIDTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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());
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.