/** * Creates a new fiber scheduler. * * @param name the scheuler's name. This name is used in naming the scheduler's threads. * @param executor an {@link Executor} used to schedule the fibers; * may be {@code null} if the {@link #execute(Runnable)} method is overriden. * @param monitorType the {@link MonitorType} type to use for the scheduler. * @param detailedInfo whether detailed information about the fibers is collected by the fibers monitor. */ public FiberExecutorScheduler(String name, Executor executor, MonitorType monitorType, boolean detailedInfo) { super(name, monitorType, detailedInfo); this.executor = executor; this.timer = new FiberTimedScheduler(this, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("FiberTimedScheduler-" + getName()).build(), getMonitor()); }
/** * Get the `FiberScheduler` for the current context. There should be only one instance per context. * @return the scheduler */ @Suspendable public static FiberScheduler getContextScheduler() { Context context = Vertx.currentContext(); if (context == null) { throw new IllegalStateException("Not in context"); } if (!context.isEventLoopContext()) { throw new IllegalStateException("Not on event loop"); } // We maintain one scheduler per context FiberScheduler scheduler = context.get(FIBER_SCHEDULER_CONTEXT_KEY); if (scheduler == null) { Thread eventLoop = Thread.currentThread(); scheduler = new FiberExecutorScheduler("vertx.contextScheduler", command -> { if (Thread.currentThread() != eventLoop) { context.runOnContext(v -> command.run()); } else { // Just run directly command.run(); } }); context.put(FIBER_SCHEDULER_CONTEXT_KEY, scheduler); } return scheduler; }