Skip to content

Commit ff078f8

Browse files
committed
Drop the timer in Ultimately for an Executor
Change the behaviour of Ultimately to spawn a single daemon thread for handling dispatching of futures.
1 parent 2222aa3 commit ff078f8

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

.jvmopts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
-XX:+CMSClassUnloadingEnabled
66
-XX:+UseConcMarkSweepGC
77
-XX:+CMSIncrementalMode
8-
-XX:NewRatio=8 -XX:MaxPermSize=512M
8+
-XX:NewRatio=8
9+
-XX:MaxPermSize=512M

scalatest/src/main/scala/org/scalatest/concurrent/Ultimately.scala

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.scalactic.source
2525
import org.scalatest.exceptions.StackDepthException
2626
import scala.concurrent.{Future, Promise, ExecutionContext}
2727
import scala.util.{Success, Failure}
28+
import java.util.concurrent.{ Executors, TimeUnit, ThreadFactory }
2829

2930
/**
3031
* Trait that provides the <code>ultimately</code> construct, which periodically retries executing
@@ -280,6 +281,20 @@ import scala.util.{Success, Failure}
280281
*/
281282
trait Ultimately extends PatienceConfiguration {
282283

284+
285+
private[this] lazy val scheduler = {
286+
val threadFactory = new ThreadFactory {
287+
val inner = Executors.defaultThreadFactory()
288+
def newThread(runnable: Runnable) = {
289+
val thread = inner.newThread(runnable)
290+
thread.setDaemon(true)
291+
thread
292+
}
293+
}
294+
295+
Executors.newSingleThreadScheduledExecutor(threadFactory)
296+
}
297+
283298
/**
284299
* Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum
285300
* amount of time has passed, sleeping a configured interval between attempts.
@@ -427,7 +442,7 @@ trait Ultimately extends PatienceConfiguration {
427442
val promise = Promise[T]
428443

429444
val task =
430-
new TimerTask {
445+
new Runnable {
431446
override def run(): Unit = {
432447
val newFut = tryTryAgain(attempt + 1)
433448
newFut onComplete {
@@ -437,8 +452,7 @@ trait Ultimately extends PatienceConfiguration {
437452
}
438453
}
439454

440-
val timer = new Timer
441-
timer.schedule(task, chillTime)
455+
scheduler.schedule(task, chillTime, TimeUnit.MILLISECONDS)
442456
promise.future
443457
}
444458
else { // Timed out so return a failed Future

0 commit comments

Comments
 (0)