Skip to content
Create a gist now

Instantly share code, notes, and snippets.

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
class MutlithreadedUniquenessOfUUID extends FunSuite with ShouldMatchers {
test("Warm up the generator") {
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
Something went wrong with that request. Please try again.