Skip to content

Commit

Permalink
Merge pull request lampepfl#32 from He-Pin/vtAvoidRescheduling
Browse files Browse the repository at this point in the history
Add VTFactory for the group naming.
  • Loading branch information
natsukagami authored Dec 26, 2023
2 parents f9f063f + b2d1731 commit 68b1c73
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions jvm/src/main/scala/async/VThreadSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import java.util.concurrent.locks.ReentrantLock
import scala.concurrent.duration.FiniteDuration

object VThreadScheduler extends Scheduler:
override def execute(body: Runnable): Unit = Thread.startVirtualThread(body)
private val VTFactory = Thread
.ofVirtual()
.name("gears.async.VThread-", 0L)
.factory()

override def execute(body: Runnable): Unit = VTFactory.newThread(body)

override def schedule(delay: FiniteDuration, body: Runnable): Cancellable =
val th = Thread.startVirtualThread: () =>
val th = VTFactory.newThread: () =>
Thread.sleep(delay.toMillis)
execute(body)
th.start()
() => th.interrupt()

object VThreadSupport extends AsyncSupport:
Expand Down

0 comments on commit 68b1c73

Please sign in to comment.