Created
August 11, 2012 14:37
-
-
Save jadamcrain/3324954 to your computer and use it in GitHub Desktop.
Oracle Java 7 update 5 UUID race condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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