@@ -25,6 +25,7 @@ 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 }
28
29
29
30
/**
30
31
* Trait that provides the <code>ultimately</code> construct, which periodically retries executing
@@ -280,6 +281,20 @@ import scala.util.{Success, Failure}
280
281
*/
281
282
trait Ultimately extends PatienceConfiguration {
282
283
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
+
283
298
/**
284
299
* Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum
285
300
* amount of time has passed, sleeping a configured interval between attempts.
@@ -427,7 +442,7 @@ trait Ultimately extends PatienceConfiguration {
427
442
val promise = Promise [T ]
428
443
429
444
val task =
430
- new TimerTask {
445
+ new Runnable {
431
446
override def run (): Unit = {
432
447
val newFut = tryTryAgain(attempt + 1 )
433
448
newFut onComplete {
@@ -437,8 +452,7 @@ trait Ultimately extends PatienceConfiguration {
437
452
}
438
453
}
439
454
440
- val timer = new Timer
441
- timer.schedule(task, chillTime)
455
+ scheduler.schedule(task, chillTime, TimeUnit .MILLISECONDS )
442
456
promise.future
443
457
}
444
458
else { // Timed out so return a failed Future
0 commit comments