Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.