How do I run tasks in Fibers once the Fiber.schedule methods are gone?
Arkadiusz Gasiński
jigga at jigga.pl
Tue Jul 23 21:47:00 UTC 2019
Hi,
So my dummy implementation of Jetty's ThreadPool is:
public class FiberThreadPool implements ThreadPool {
private final ExecutorService scheduler =
Executors.newFixedThreadPool(10);
private final FiberScope poolScope = FiberScope.open();
@Override
public void join() throws InterruptedException {
poolScope.close();
scheduler.shutdownNow();
scheduler.awaitTermination(10, TimeUnit.SECONDS);
}
@Override
public int getThreads() {
return 0;
}
@Override
public int getIdleThreads() {
return 0;
}
@Override
public boolean isLowOnThreads() {
return false;
}
@Override
public void execute(Runnable command) {
poolScope.schedule(command);
}
}
I assume that Jetty maintains single instance of ThreadPool implementation
per running server and thus having FiberScope tied to the ThreadPool's
instance should do the job. Well, I actually checked and it does.
It'd be nice if there was a way to check if FiberScope is still open before
we submit new task to it.
Also was wondering if there are any plans to extend the ExecutorService API
so that we could request our tasks to be executed in the context of Fibers
rather than Threads. Something like:
<T> Fiber<T> submit2(Callable<T> task);
After submitting the task we'd get the reference to the Fiber instance
(which we could adapt to CompletableFuture if needed). And the FiberScope
in which this Fiber would be running would be limited by the scope of the
ExecutorService, i.e. closing ExecutorService would automatically close
underlying FiberScope. Not sure if that makes sense at all, just an idea.
Regards,
Arek
On Sun, Jul 21, 2019 at 3:24 PM Alan Bateman <Alan.Bateman at oracle.com>
wrote:
> On 20/07/2019 15:56, Arkadiusz Gasiński wrote:
>
> Awesome, thanks!
>
> Also, given these changes to Fiber's API, what would be the recommended
> way to rewrite Mark's code <https://youtu.be/Csc2JRs6470?t=1598> he wrote
> for his loom rest demo he did at Devoxx last year?
>
> I assume that FiberScope.background().schedule() would be the simplest way
> to refactor his ThreadPool.execute(Runnable) method, but perhaps
> maintaining a dedicated FiberScope in the ThreadPool would be a better idea
> (though FiberScope.isClosed() method would be useful to not schedule new
> tasks if the scope has been closed)?
>
> For the Jetty "replace thread pool with fibers" example then
> FiberScope.background().schedule(...) should work fine. A FiberScope can
> only be closed by its owner so it's not possible to close the
> background/primordial scope. I don't think anyone has looked at changes to
> Jetty to get it work with structured concurrency, that could be an
> interesting experiment.
>
> -Alan.
>
More information about the loom-dev
mailing list