Swan Lake Beta3 is here.
import ballerina/io;

class EvenNumber {
    int i = 1;

    // `isolated` method.
    isolated function generate() returns int {

        lock {
            // Uses `self` to access mutable field `i`
            // within a `lock` statement.
            return self.i * 2;

        }
    }
}

public function main() {
    EvenNumber e = new;
    int c = e.generate();
    io:println(c);
}

Isolated Methods

Object methods can be isolated. An isolated method is same as an isolated function with self treated as a parameter. An isolated method call is concurrency-safe if both the object is safe and the arguments are safe. This is not quite enough for service concurrency: when a listener makes calls to a remote or resource method,

  • it can ensure the safety of arguments it passes
  • it has no way to ensure the safety of the object itself (since the object may have fields)

import ballerina/io;
class EvenNumber {
    int i = 1;
    isolated function generate() returns int {

isolated method.

        lock {
            return self.i * 2;

Uses self to access mutable field i within a lock statement.

        }
    }
}
public function main() {
    EvenNumber e = new;
    int c = e.generate();
    io:println(c);
}
bal run isolated_methods.bal
2
import ballerina/io;

class EvenNumber {
    int i = 1;

    // `isolated` method.
    isolated function generate() returns int {

        lock {
            // Uses `self` to access mutable field `i`
            // within a `lock` statement.
            return self.i * 2;

        }
    }
}

public function main() {
    EvenNumber e = new;
    int c = e.generate();
    io:println(c);
}

Isolated Methods

Object methods can be isolated. An isolated method is same as an isolated function with self treated as a parameter. An isolated method call is concurrency-safe if both the object is safe and the arguments are safe. This is not quite enough for service concurrency: when a listener makes calls to a remote or resource method,

  • it can ensure the safety of arguments it passes
  • it has no way to ensure the safety of the object itself (since the object may have fields)

import ballerina/io;
class EvenNumber {
    int i = 1;
    isolated function generate() returns int {

isolated method.

        lock {
            return self.i * 2;

Uses self to access mutable field i within a lock statement.

        }
    }
}
public function main() {
    EvenNumber e = new;
    int c = e.generate();
    io:println(c);
}
bal run isolated_methods.bal
2
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