import ballerina/io;
function main (string[] args) {    worker w1 {
        int i = 100;
        float k = 2.34;
        io:println("[w1 -> w2] i: " + i + " k: " + k);
        i, k -> w2;
        json j = {};
        j <- w2;
        io:println("[w1 <- w2] j: " + j.toString());
    }    worker w2 {
        int iw;
        float kw;
        iw, kw <- w1;
        io:println("[w2 <- w1] iw: " + iw + " kw: " + kw);
        json jw = {"name":"Ballerina"};
        io:println("[w2 -> w1] jw: " + jw.toString());
        jw -> w1;
    }
}

Worker Interaction

Workers interact with each other by sending messages. Messages are sent over a channel but the channels are anonymous.

import ballerina/io;
function main (string[] args) {

Workers interact with each other by sending and receiving messages. Ballerina checks the send/receive signatures of every pair of workers and validate them in order to avoid deadlocks.

    worker w1 {
        int i = 100;
        float k = 2.34;
        io:println("[w1 -> w2] i: " + i + " k: " + k);
        i, k -> w2;

Send messages to worker ‘w2’. This message contains two values of type int and float.

        json j = {};
        j <- w2;
        io:println("[w1 <- w2] j: " + j.toString());
    }

Receive a message from worker w2. This message contains a JSON typed value.

    worker w2 {
        int iw;
        float kw;
        iw, kw <- w1;
        io:println("[w2 <- w1] iw: " + iw + " kw: " + kw);

Receive a message from the default worker.

        json jw = {"name":"Ballerina"};
        io:println("[w2 -> w1] jw: " + jw.toString());
        jw -> w1;
    }
}

Send a message to the default worker.

$ ballerina run worker-interaction.bal
[w1 -> w2] i: 100 k: 2.34
[w2 <- w1] iw: 100 kw: 2.34
[w2 -> w1] jw: {"name":"Ballerina"}
[w1 <- w2] j: {"name":"Ballerina"}