@@ -25,6 +25,8 @@ import org.scalactic.source
25
25
import org .scalatest .exceptions .StackDepthException
26
26
import scala .concurrent .{Future , Promise , ExecutionContext }
27
27
import scala .util .{Success , Failure }
28
+ import java .util .concurrent .{ Executors , TimeUnit , ThreadFactory }
29
+ import java .util .concurrent .ScheduledExecutorService
28
30
29
31
/**
30
32
* Trait that provides the <code>ultimately</code> construct, which periodically retries executing
@@ -280,6 +282,8 @@ import scala.util.{Success, Failure}
280
282
*/
281
283
trait Ultimately extends PatienceConfiguration {
282
284
285
+
286
+
283
287
/**
284
288
* Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum
285
289
* amount of time has passed, sleeping a configured interval between attempts.
@@ -427,7 +431,7 @@ trait Ultimately extends PatienceConfiguration {
427
431
val promise = Promise [T ]
428
432
429
433
val task =
430
- new TimerTask {
434
+ new Runnable {
431
435
override def run (): Unit = {
432
436
val newFut = tryTryAgain(attempt + 1 )
433
437
newFut onComplete {
@@ -437,8 +441,7 @@ trait Ultimately extends PatienceConfiguration {
437
441
}
438
442
}
439
443
440
- val timer = new Timer
441
- timer.schedule(task, chillTime)
444
+ Ultimately .scheduler.schedule(task, chillTime, TimeUnit .MILLISECONDS )
442
445
promise.future
443
446
}
444
447
else { // Timed out so return a failed Future
@@ -501,4 +504,17 @@ trait Ultimately extends PatienceConfiguration {
501
504
* ...
502
505
* </pre>
503
506
*/
504
- object Ultimately extends Ultimately
507
+ object Ultimately extends Ultimately {
508
+ private lazy val scheduler : ScheduledExecutorService = {
509
+ val threadFactory = new ThreadFactory {
510
+ val inner = Executors .defaultThreadFactory()
511
+ def newThread (runnable : Runnable ) = {
512
+ val thread = inner.newThread(runnable)
513
+ thread.setDaemon(true )
514
+ thread
515
+ }
516
+ }
517
+
518
+ Executors .newSingleThreadScheduledExecutor(threadFactory)
519
+ }
520
+ }
0 commit comments