Skip to content

Instantly share code, notes, and snippets.

@jadamcrain
Created August 10, 2012 23:38
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/3318973 to your computer and use it in GitHub Desktop.
Save jadamcrain/3318973 to your computer and use it in GitHub Desktop.
Problem with Java 7 UUID.random()
package com.automatak.archivist.server.uuid
import org.scalatest.FunSuite
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import com.automatak.commons.testing._
import java.util.UUID
@RunWith(classOf[JUnitRunner])
class MutlithreadedUniquenessOfUUID extends FunSuite with ShouldMatchers {
test("Warm up the generator") {
1000000.times(UUID.randomUUID())
}
test("Highly concurrently generated UUIDS are still unique") {
val uuidPerThread = 100000
val numThreads = 64
val totalUUID = uuidPerThread * numThreads
val set = collection.mutable.Set.empty[UUID]
val threads = numThreads create {
onAnotherThread {
uuidPerThread times {
val uuid = UUID.randomUUID()
set.synchronized {
if (set.contains(uuid)) println(uuid)
else set += uuid
}
}
}
}
val (ns, _) = time(threads.foreach(_.join()))
set.size should equal(totalUUID)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment