public

Problem with Java 7 UUID.random()

  • Download Gist
MultithreadedUniquenessOfUUID.scala
Scala
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
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)
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.