ballerina 学习十七 多线程编程

并发&&多线程开发对于日常的处理是比较重要的,ballerina 支持的模式有work fork/join async lock

基本workers

  • 参考代码
import ballerina/io;

function main(string… args) {
worker first {
io:println("first");
}
worker second {
io:println("second");

}
}
  • 输出结果
first
second

workers 数据通信

worker 之间的通信和大部分语言都是比较类似的消息,同时使用channel(类似golang),同发送,接收应该是成对出现

  • 参考代码
import ballerina/io;

function main (string… args) {
worker first {
string name ="from first message";
name -> second;
io:println("send message first-> second ");
}
worker second {
string name ;
name <- first;
io:println("recevied message first ",name);
}
}
  • 输出结果
recevied message first from first message
send message first-> second

基本fork/join

类似java 语言的多线程开发模型

  • 参考代码
import ballerina/io;

function main (string… args) {
fork {
worker first {
"dalongdemo first" -> fork;
}
worker second {
(1,"appdemo") -> fork;
}
} join (all) (map results) {
int index ;
string indexname;
string first = <string>results["first"];
io:println(first," result from first work");
(index, indexname) = check <(int, string)>results["second"];
io:println(index,indexname, " result from second work");
}
}
  • 输出结果
dalongdemo first result from first work
1appdemo result from second work

fork/join 条件

上面的例子我们使用的是all 实际上可以使用some

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

(i, s) -> fork;
}

worker w2 {
float f = 10.344;
io:println("[w2] f: ", f);
runtime:sleep(100);
f -> fork;
}

} join (some 1) (map results) {

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

if (results["w2"] != null) {
float fW2 = check <float>results["w2"];
io:println("[join-block] fW2: ", fW2);
}
}
}
  • 输出结果
[w1] i: 23 s: Colombo
[w2] f: 10.344
[join-block] iW1: 23 sW1: Colombo
[join-block] fW2: 10.344

fork/join 变量访问

实际上就是共享数据访问的处理

import ballerina/io;
function main(string… args) {
int i = 100;
string s = "WSO2";
map m = { "name": "dalong", "era": "demoapp" }; string name = <string>m["name"];
string era = <string>m["era"];
io:println("[default worker] before fork-join: value of name is [",
name, "] value of era is [", era, "]");
fork {
worker W1 {
i = 23;
m["name"] = "fengliang";
string n = "Colombo";
(i, n) -> fork;
}
worker W2 {
s = "Ballerina";
m["era"] = "rong";
s -> fork;
}
} join (all) (map results) {
int p;
string l;
(p, l) = check <(int, string)>results["W1"];
string q = <string>results["W2"];
io:println("[default worker] within join: " +
"value of integer variable from W1 is [", p, "]");
io:println("[default worker] within join: " +
"value of string variable from W1 is [", l, "]");
io:println("[default worker] within join: " +
"value of string variable from W2 is [", q, "]");
}
io:println("[default worker] after fork-join: " +
"value of integer variable is [", i, "] ",
"value of string variable is [", s, "]");
name = <string>m["name"];
era = <string>m["era"]; io:println("[default worker] after fork-join: " +
"value of name is [", name,
"] value of era is [", era, "]");
}
  • 输出结果
[default worker] before fork-join: value of name is [dalong] value of era is [demoapp]
[default worker] within join: value of integer variable from W1 is [23]
[default worker] within join: value of string variable from W1 is [Colombo]
[default worker] within join: value of string variable from W2 is [Ballerina]
[default worker] after fork-join: value of integer variable is [100] value of string variable is [WSO2]
[default worker] after fork-join: value of name is [fengliang] value of era is [rong]

aysnc

  • 参考代码
import ballerina/io;

function main (string… args) {
future<int> result = start add(1,3);
int waitresult = await result;
int waitresult2 = getresult(result);
io:println(waitresult," with await");
io:println(waitresult2," with await app");
}
function getresult (future<int> f) returns int{
return await f;
}

function add (int a ,int b) returns int{
return a+b;
}
  • 输出结果
4 with await
4 with await app

lock

类似多线程开发中的锁

  • 参考代码
import ballerina/io;
int counter;

function main(string… args) {
process();
io:println("final counter value - ", counter);
}
function process() {
worker w1 {
foreach i in [1..1000] {
lock {
counter = counter + 1;
}
}
}
worker w2 {
foreach i in [1..1000] {
lock {
counter = counter + 1;
}
}
}
worker w3 {
foreach i in [1..1000] {
lock {
counter = counter + 1;
}
}
}
worker w4 {
foreach i in [1..1000] {
lock {
counter = counter + 1;
}
}
}
}

  • 输出结果
final counter value - 4000

参考资料

https://ballerina.io/philosophy/
https://ballerina.io/learn/by-example/async.html
https://ballerina.io/learn/by-example/locks.html
https://ballerina.io/learn/by-example/workers.html
https://ballerina.io/learn/by-example/worker-interaction.html
https://ballerina.io/learn/by-example/fork-join.html

原文地址:https://www.cnblogs.com/rongfengliang/p/9114948.html