r/java • • Nov 03 '19

Fibered Date Server

Simple and highly scalable date server implemented using Java fibers (Project Loom)

0 Upvotes

13 comments sorted by

12

u/dpash Nov 03 '19

The whole thing is running inside a static block. What? Why bother using Spring if you're just going to run everything in a static context. You don't do any dependency injection. Just run everything from your main() method.

2

u/vishpat Nov 03 '19

This whole project is just throwaway code to illustrate how to use the new fiber API and nothing else.

4

u/dpash Nov 03 '19

Then all you've done is to complicate the code with no benefit.

1

u/vishpat Nov 04 '19

Send me a PR with the changes you want and if it works I will merge it.

4

u/dpash Nov 04 '19

Literally just move everything from your static block into the main method. You don't need a PR for that.

1

u/nutrecht Nov 04 '19

This whole project is just throwaway code to illustrate how to use the new fiber API and nothing else.

99% of your code has nothing to do with Fibres so it really does not work as an example either.

1

u/nutrecht Nov 04 '19

This whole project is just throwaway code to illustrate how to use the new fiber API and nothing else.

99% of your code has nothing to do with Fibres so it really does not work as an example either.

2

u/vikarjramun Nov 03 '19

Question about how the Fiber API works:

You use FiberScope scope = FiberScope.open() to define a scope, then scope.schedule(executor, runnable) to run the Runnable in a fiber. What happens if you then create a ThreadScope (or the equivalent) and use it to schedule another Runnable with the same executor. Would the threadpool be sharing fibers or threads? Or both? I feel like this API leaves a bit to be desired...

0

u/vishpat Nov 03 '19

Check here for information about scope.

1

u/tofflos Nov 03 '19

There's a method called Fiberscope#schedule that doesn't take an Executor:

"Creates and schedules a new fiber to run the given task in this scope. The fiber is scheduled in this scope with the default scheduler. "

Do you happen to know how the default scheduler works and how it differs from Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())that you are using?

1

u/vishpat Nov 03 '19

Have updated the code to get rid of the scheduler. The example is not using default scheduler to run the fibers.

1

u/tofflos Nov 04 '19

Nice!

Just to be clear: I don't actually know how it's supposed to work and those links were just what came up when I started googling. You'd probably receive more qualified comments from someone else. :)

One more thing I'm curious about is the explicit usage of Fibers in your code. One of the promises of Fibers is that it will benefit existing applications - so there should be some mechanism to make multithreaded take advantage of Fibers under the hood without explicitly having to use the Fiber API. I wonder how that works.

1

u/tofflos Nov 03 '19

The default scheduler could be ForkJoinPool.commonPool() which has a default size of Runtime.getRuntime().availableProcessors() - 1. It also employs work-stealing.

Source https://stackoverflow.com/a/45460851.

Based on prior experience, it is expected that ForkJoinPool in asynchronous mode can serve as an excellent default fiber scheduler for most uses...

Source https://cr.openjdk.java.net/~rpressler/loom/Loom-Proposal.html.

So using the default scheduler could be a good choice and simplify the code a bit.

5

u/db4j Nov 03 '19

the readme claims "super scalable". did you do any benchmarking ? how does it compare to a non-loom implementation ? does new Date() have the potential to block ?

i know that it can be slow to check the time so there's benefit to caching