-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
129bcb6
commit 534ede9
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import ballerina/io; | ||
import ballerina/lang.runtime; | ||
|
||
public function main() { | ||
worker w1 { | ||
2 -> w3; | ||
} | ||
|
||
worker w2 { | ||
runtime:sleep(2); | ||
3 -> w3; | ||
} | ||
|
||
worker w3 returns int { | ||
// The value of the variable `result` is set as soon as the values from either | ||
// worker `w1` or `w2` is received. | ||
int result = <- w1 | w2; | ||
return result; | ||
} | ||
|
||
worker w4 returns error? { | ||
int value = 10; | ||
if value == 10 { | ||
return error("Error in worker 1"); | ||
} | ||
value -> w6; | ||
} | ||
|
||
worker w5 { | ||
runtime:sleep(2); | ||
3 -> w6; | ||
} | ||
|
||
worker w6 returns int|error? { | ||
// Alternate receive action waits until a message that is not an error is received | ||
// when error is not an expected static type. Since `w4` returns an error it | ||
// waits further and sets the value that is received from `w5`. | ||
int a = check <- w4 | w5; | ||
return a; | ||
} | ||
|
||
int valueW3 = wait w3; | ||
io:println(valueW3); | ||
|
||
int|error? valueW6 = wait w6; | ||
io:println(valueW6); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Alternate receive | ||
|
||
The alternate receive action can be used to receive values from several workers. | ||
|
||
::: code alternate_receive.bal ::: | ||
|
||
::: out alternate_receive.out ::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: This BBE demonstrates the alternate receive action | ||
keywords: ballerina, ballerina by example, bbe, worker, alternate receive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
$ bal run alternate_receive.bal | ||
2 | ||
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters