Swan Lake Beta2 is here.
import ballerina/io;

public function main() {
    // Code before any named workers is executed before named 
    // workers start.
    io:println("Initializing");
    final string greeting = "Hello";

    // A function can declare named workers, which run concurrently with the
    // function's default worker and other named workers.
    worker A {
        // Variables declared before all named workers and function 
        // parameters are accessible in named workers.
        io:println(greeting + " from worker A");

    }

    worker B {
        io:println(greeting + " from worker B");
    }

    io:println(greeting + " from function worker");
}

Named Workers

Normally all of a function’s code belongs to the function’s default worker, which has a single logical thread of control. A function can also declare named workers, which run concurrently with the function’s default worker and other named workers. Code before any named workers is executed before named workers start. Variables declared before all named workers and function parameters are accessible in named workers.

import ballerina/io;
public function main() {
    io:println("Initializing");
    final string greeting = "Hello";

Code before any named workers is executed before named workers start.

    worker A {

A function can declare named workers, which run concurrently with the function’s default worker and other named workers.

        io:println(greeting + " from worker A");

Variables declared before all named workers and function parameters are accessible in named workers.

    }
    worker B {
        io:println(greeting + " from worker B");
    }
    io:println(greeting + " from function worker");
}
bal run named_workers.bal
Initializing
Hello from function worker
Hello from worker B
Hello from worker A
import ballerina/io;

public function main() {
    // Code before any named workers is executed before named 
    // workers start.
    io:println("Initializing");
    final string greeting = "Hello";

    // A function can declare named workers, which run concurrently with the
    // function's default worker and other named workers.
    worker A {
        // Variables declared before all named workers and function 
        // parameters are accessible in named workers.
        io:println(greeting + " from worker A");

    }

    worker B {
        io:println(greeting + " from worker B");
    }

    io:println(greeting + " from function worker");
}

Named Workers

Normally all of a function’s code belongs to the function’s default worker, which has a single logical thread of control. A function can also declare named workers, which run concurrently with the function’s default worker and other named workers. Code before any named workers is executed before named workers start. Variables declared before all named workers and function parameters are accessible in named workers.

import ballerina/io;
public function main() {
    io:println("Initializing");
    final string greeting = "Hello";

Code before any named workers is executed before named workers start.

    worker A {

A function can declare named workers, which run concurrently with the function’s default worker and other named workers.

        io:println(greeting + " from worker A");

Variables declared before all named workers and function parameters are accessible in named workers.

    }
    worker B {
        io:println(greeting + " from worker B");
    }
    io:println(greeting + " from function worker");
}
bal run named_workers.bal
Initializing
Hello from function worker
Hello from worker B
Hello from worker A

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand