Swan Lake Beta3 is here.
import ballerina/io;

public function main() {
    // By default, named workers are multitasked cooperatively, not preemptively.
    // Each named worker has a "strand" (logical thread of control) and execution
    // switches between strands only at specific "yield" points.
    worker A {
        io:println("In worker A");
    }

    // An annotation can be used to make a strand run on a separate thread.
    @strand {
        thread: "any"
    }

    worker B {
        io:println("In worker B");
    }

    io:println("In function worker");
}

Strands

By default, named workers are multitasked cooperatively, not preemptively. Each named worker has a “strand” (logical thread of control) and execution switches between strands only at specific “yield” points such as doing a wait or when a library function invokes a system call that would block. This avoids the need for users to lock variables that are accessed from multiple named workers. An annotation can be used to make a strand run on a separate thread.

import ballerina/io;
public function main() {
    worker A {
        io:println("In worker A");
    }

By default, named workers are multitasked cooperatively, not preemptively. Each named worker has a “strand” (logical thread of control) and execution switches between strands only at specific “yield” points.

    @strand {
        thread: "any"
    }

An annotation can be used to make a strand run on a separate thread.

    worker B {
        io:println("In worker B");
    }
    io:println("In function worker");
}
bal run strands.bal
In worker B
In function worker
In worker A
import ballerina/io;

public function main() {
    // By default, named workers are multitasked cooperatively, not preemptively.
    // Each named worker has a "strand" (logical thread of control) and execution
    // switches between strands only at specific "yield" points.
    worker A {
        io:println("In worker A");
    }

    // An annotation can be used to make a strand run on a separate thread.
    @strand {
        thread: "any"
    }

    worker B {
        io:println("In worker B");
    }

    io:println("In function worker");
}

Strands

By default, named workers are multitasked cooperatively, not preemptively. Each named worker has a “strand” (logical thread of control) and execution switches between strands only at specific “yield” points such as doing a wait or when a library function invokes a system call that would block. This avoids the need for users to lock variables that are accessed from multiple named workers. An annotation can be used to make a strand run on a separate thread.

import ballerina/io;
public function main() {
    worker A {
        io:println("In worker A");
    }

By default, named workers are multitasked cooperatively, not preemptively. Each named worker has a “strand” (logical thread of control) and execution switches between strands only at specific “yield” points.

    @strand {
        thread: "any"
    }

An annotation can be used to make a strand run on a separate thread.

    worker B {
        io:println("In worker B");
    }
    io:println("In function worker");
}
bal run strands.bal
In worker B
In function worker
In worker A
Subscribe to the newsletter

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