import ballerina/io;function main (string[] args) {
    fork {
        worker w1 {
            int i = 23;
            string s = "Colombo";
            io:println("[w1] i: " + i + " s: " + s);
            i, s -> fork;
        }        worker w2 {
            float f = 10.344;
            io:println("[w2] f: " + f);
            f -> fork;
        }
    } join (some 1) (map results) {
        if (results["w1"] != null) {
            any[] resW1 =check <any[]>results["w1"];
            int iW1 =check <int>resW1[0];
            string sW1 = <string>resW1[1];
            io:println("[join-block] iW1: " + iW1 + " sW1: " + sW1);
        }
        if (results["w2"] != null) {
            any[] resW2 =check <any[]>results["w2"];
            float fW2 =check <float>resW2[0];
            io:println("[join-block] fW2: " + fW2);
        }
    }
}

Fork/Join Condition Some

A fork-join statement splits the current execution to multiple workers, do their work in parallel, and join the executions together to process their results. The fork block allows you to start any number of parallel workers at once. In this example, you can make the join block wait for a specific number of workers to finish executing using the 'some (int)' condition.

import ballerina/io;
function main (string[] args) {
    fork {
        worker w1 {
            int i = 23;
            string s = "Colombo";
            io:println("[w1] i: " + i + " s: " + s);

Declare the fork-join statement.

            i, s -> fork;
        }

Reply to the join block from worker w1.

        worker w2 {
            float f = 10.344;
            io:println("[w2] f: " + f);
            f -> fork;
        }
    } join (some 1) (map results) {

Reply to the join block from worker w2.

Here we use ‘some 1’ as the join condition, which means that the join block needs to wait for any one of the workers to finish executing. When the join condition has been satisfied, the results ‘map’ is updated with the value returned by the worker.

        if (results["w1"] != null) {
            any[] resW1 =check <any[]>results["w1"];
            int iW1 =check <int>resW1[0];
            string sW1 = <string>resW1[1];
            io:println("[join-block] iW1: " + iW1 + " sW1: " + sW1);
        }

Checks if the worker that finished executing is worker ‘w1’.

        if (results["w2"] != null) {
            any[] resW2 =check <any[]>results["w2"];
            float fW2 =check <float>resW2[0];
            io:println("[join-block] fW2: " + fW2);
        }
    }
}

Checks if the worker that finished executing is worker ‘w2’.

$ ballerina run fork-join-condition-some.bal
[w2] f: 10.344
[w1] i: 23 s: Colombo
[join-block] fW2: 10.344

The worker that has finished executing is ‘w2’.