Permalink
Browse files

Use ThreadLocalRandom in StringHelpers.randomString on Java 7.

On Java <7, we use the default SecureRandom, which locks on access.
ThreadLocalRandom will not lock as it is thread-local (!).
  • Loading branch information...
1 parent 22250b5 commit 229348d26a22f5565cb4b1ebd1d290115cf68f67 @Shadowfiend Shadowfiend committed with nafg Oct 11, 2012
Showing with 19 additions and 2 deletions.
  1. +19 −2 core/util/src/main/scala/net/liftweb/util/StringHelpers.scala
@@ -19,6 +19,7 @@ package util
import java.security.SecureRandom
import java.util.regex._
+import java.util.Random
import java.lang.Character._
import java.lang.{StringBuilder => GoodSB}
import scala.xml.NodeSeq
@@ -32,7 +33,23 @@ object StringHelpers extends StringHelpers
trait StringHelpers {
/** random numbers generator */
- private val _random = new SecureRandom
+ private lazy val _slowRandom = new SecureRandom
+ private lazy val _currentTlrMethod = {
+ try {
+ val tlr = Class.forName("java.util.concurrent.ThreadLocalRandom")
+ Full(tlr.getMethod("current"))
+ } catch {
+ case e =>
+ Failure("ThreadLocalRandom is not available.", Full(e), Empty)
+ }
+ }
+ private def withRng[T](block: (Random)=>T) = {
+ _currentTlrMethod.map { meth =>
+ block(meth.invoke(null).asInstanceOf[Random])
+ } openOr {
+ _slowRandom.synchronized(block(_slowRandom))
+ }
+ }
/**
* If str is surrounded by quotes it return the content between the quotes
@@ -176,7 +193,7 @@ trait StringHelpers {
if (pos >= size) sb
else {
val randNum = if ((pos % 6) == 0) {
- _random.synchronized(_random.nextInt)
+ withRng(_.nextInt)
} else {
lastRand
}

0 comments on commit 229348d

Please sign in to comment.