diff --git a/examples/strands/strands.bal b/examples/strands/strands.bal index 6967bdd0e8..6a9cd5c967 100644 --- a/examples/strands/strands.bal +++ b/examples/strands/strands.bal @@ -8,8 +8,11 @@ function userSpeakerService(string userName) { } public function main() { + + io:println("In function worker"); + // By default, named workers are multitasked cooperatively, not preemptively. - // Each named worker has a `strand` (a logical thread of control) and + // Each named worker has a strand (a logical thread of control) and // the execution switches between strands only at specific `yield` points. worker A { io:println("In worker A"); @@ -23,5 +26,9 @@ public function main() { io:println("Worker B end"); } - io:println("In function worker"); + // Explicitly wait for named workers to complete. + // This guarantees that the Ballerina program will not terminate till + // the workers have completed their execution. + wait A; + wait B; } diff --git a/examples/strands/strands.md b/examples/strands/strands.md index a114c85dbb..fd54d6bfe1 100644 --- a/examples/strands/strands.md +++ b/examples/strands/strands.md @@ -2,8 +2,6 @@ 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. - ::: code strands.bal ::: ::: out strands.out ::: \ No newline at end of file