Nafaz M N M
Nafaz M N M

Reputation: 1688

Threads and Reactive Programming in Ballerina

I have just started to follow ballerina by reading https://ballerina.io/ documentation. when I try to run Hello World Parallel then I have no idea how internally it works?

 import ballerina/io;
    public function main() {
        @strand {thread: "any"}
        worker w1 {
            io:println("Hello, World! #m");
        }
    
        @strand {thread: "any"}
        worker w2 {
            io:println("Hello, World! #n");
        }
        @strand {thread: "any"}
        worker w3 {
            io:println("Hello, World! #k");
        }
    }
  1. When I run this code how many threads are running behind that 4 or less than 4?
  2. Threads are very expensive in java, when we create a thread at the same time OS-level thread is created by the JVM and a lot of memory will be consumed. Does the ballerina also follow the same way (Native Thread Model)?
  3. Can we use lightweight threads in ballerina? Like which is going to be introduced by Project Loom(fibers)
  4. Does the ballerina fully supported for reactive programming?
  5. Does the ballerina provide packages to connect with MongoDB, Redis, and Cassandra without blocking? I mean reactive way

Upvotes: 4

Views: 202

Answers (1)

Dhananjaya
Dhananjaya

Reputation: 1615

  1. Ballerina run on a thread pool configured on VM startup. The thread count is configurable with environment variable BALLERINA_MAX_POOL_SIZE, when this is not explicitly provided it will default to thread count = (logical cpu count X 2).

  2. Ballerina runtime scheduler work on pre-configured thread pool and it does not have a one to one correspondence with native threads, the flow of control (what usually called thread) is called a strand and many strands will be mapped to a single OS thread. This is very similar to green threads, user level threads, or Project Loom fibers.

  3. All you get is lightweight threads.

  4. As far as I understand reactive programming was developed as a way to tame the complexities coming with programming complex asynchronous systems, similar to reactive programming, Ballerina support another way to write asynchronous systems with async await style (similar to C#) https://ballerina.io/learn/by-example/async.html

  5. Most of the Ballerina libraries are non-blocking (does not block the OS thread), this means the Ballerina program using that library looks like the code written using straight forward blocking code, but under the hood they are multiplexed by Ballerina schedulear. So if you mean the reactive programming paradigm, then no, but if you mean the non-blockingness and simplicity (without call backs etc,) then yes.

Upvotes: 7

Related Questions