Skip to content

Commit

Permalink
Use interruptGuard in VThreadScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
m8nmueller committed Jan 1, 2024
1 parent 7df96bd commit b5e5b1d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions jvm/src/main/scala/async/VThreadSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gears.async
import scala.annotation.unchecked.uncheckedVariance
import java.util.concurrent.locks.ReentrantLock
import scala.concurrent.duration.FiniteDuration
import java.util.concurrent.atomic.AtomicBoolean

object VThreadScheduler extends Scheduler:
private val VTFactory = Thread
Expand All @@ -13,11 +14,14 @@ object VThreadScheduler extends Scheduler:
override def execute(body: Runnable): Unit = VTFactory.newThread(body)

override def schedule(delay: FiniteDuration, body: Runnable): Cancellable =
val interruptGuard = AtomicBoolean(true) // to avoid interrupting the body

val th = VTFactory.newThread: () =>
Thread.sleep(delay.toMillis)
execute(body)
if interruptGuard.getAndSet(false) then body.run()
th.start()
() => th.interrupt()

() => if interruptGuard.getAndSet(false) then th.interrupt()

object VThreadSupport extends AsyncSupport:

Expand Down

0 comments on commit b5e5b1d

Please sign in to comment.