Back to Examples

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` 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);
}

Executing the above code gives the output below.

$ bal run isolated_methods.bal2
PreviousIsolated variables
NextIsolated objects