2

I am new to Quasar and want to know if there are any examples like below. Or even if someone could point me into the right direction on how to do this.

So what I need to do is read a json file. For each index create a new Fiber and pass some value from the json. Each fiber will create a execute a function based from a value from the json. But depending on the value, some fibers may take longer to do. Will wait max of 5 seconds to complete.

I assume all these fibers will run in parallel.

Is this even possible ? Any example would be great.

NoName2
  • 113
  • 1
  • 8

1 Answers1

4

I'm part of the Quasar team.

Fibers will run concurrently, the actual parallelism depends on how many CPU cores you run your program on and on the parallelism level of the fibers executor, which by default is a ForkJoinPool with a number of worker threads equal to the number of CPU cores.

Fibers are used just like threads (but much more lightweight): you spawn them, let them do their job and join them. It looks like in your case you don't need much more than that (even though Quasar offers Go-like channels, Erlang-like actors and dataflow as well, for both fibers and threads) so I suggest you take a look at the fiber docs, there are also plenty of examples but I especially suggest you start from the Gradle template.

You can pass the index/value as closure variables to an instance created from anonymous class or you could create a named sublass of Fiber and receive them as constructor arguments.

Remember to run the agent. If you need to build a single-artifact executable JAR including the agent I suggest you take a look at Capsule.

Also consider joining the Quasar-Pulsar user group.

circlespainter
  • 836
  • 5
  • 8
  • 1
    WARNING: fiber Fiber@10000007:fiber-10000007[task: ParkableForkJoinTask@169b456(Fiber@10000007), target: null, scheduler: co.paralleluniverse.fibers.FiberForkJoinScheduler@376cd890] is hogging the CPU or blocking a thread. This is the error I get when I try to call my class within a thread. – NoName2 Apr 25 '16 at 10:23
  • 1
    It's not an error but a warning, please have a look at the [docs](http://docs.paralleluniverse.co/quasar/#fibers) for more info ("runaway fibers" sub-section). – circlespainter Apr 25 '16 at 13:16