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
ballerina 学习十七 多线程编程的更多相关文章
- Linux学习 :多线程编程
1.Linux进程与线程() 进程:通过fork创建子进程与创建线程之间是有区别的:fork创建出该进程的一份拷贝,创建时额外申请了新的内存空间以及存储代码段.数据段.BSS段.堆.栈空间, ...
- 深入学习c++--多线程编程(一)
1. 简介 2. 线程使用 2.1 demo #include <iostream> #include <thread> #include <future> usi ...
- APUE学习之多线程编程(三):线程属性、同步属性
一.线程属性 可以使用pthread_attr_t结构修改线程默认属性,并这些属性和创建的线程练习起来,可以使用pthread_att_init函数初始化pthread_attr_t结构,调 ...
- APUE学习之多线程编程(二):线程同步
为了保证临界资源的安全性和可靠性,线程不得不使用锁,同一时间只允许一个或几个线程访问变量.常用的锁有互斥量,读写锁,条件变量 一.互斥量 互斥量是用pthrea ...
- APUE学习之多线程编程(一):线程的创建和销毁
一.线程标识 和每个进程都有一个进程ID一样,每个线程也有一个线程ID,线程ID是以pthread_t数据类型来表示的,在Linux中,用无符号长整型表示pthread_t,Solaris ...
- Linux程序设计学习笔记----多线程编程线程同步机制之相互排斥量(锁)与读写锁
相互排斥锁通信机制 基本原理 相互排斥锁以排他方式防止共享数据被并发訪问,相互排斥锁是一个二元变量,状态为开(0)和关(1),将某个共享资源与某个相互排斥锁逻辑上绑定之后,对该资源的訪问操作例如以下: ...
- 深入学习c++--多线程编程(三)thread的两种死法
1. 生成了一个线程,需要告诉编译器是否管理 必须告诉编译器是不管理还是管理,否则直接down了 #include <iostream> #include <thread> # ...
- 深入学习c++--多线程编程(二)【当线程间需要共享非const资源】
1. 遇到的问题 #include <iostream> #include <thread> #include <chrono> #include <futu ...
- 吴裕雄--天生自然 JAVA开发学习:多线程编程
class RunnableDemo implements Runnable { private Thread t; private String threadName; RunnableDemo( ...
随机推荐
- OpenCV_火焰检测——完整代码
转:http://blog.csdn.net/xiao_lxl/article/details/43307993 火焰检测小程序 前几天,偶然看到了An Early Fire-Detection Me ...
- openwrt下使用wget出现Failed to allocate uclient context
一.场景重现 root@OpenWrt:/# wget www.baidu.com Downloading 'www.baidu.com' Failed to allocate uclient con ...
- HDU 6156 Palindrome Function(数位DP)题解
思路: 数位dp的操作是dfs+记忆化,我们dp开四维:位置,长度,进制,是否回文.然后每次暴搜记录下每个位置的数字是什么,搜到对称轴另一边需要检查是否符合回文. 终于把友谊赛的题目都补完了...没做 ...
- 使用webuploader实现文件的断点续传
webuploader是百度Fex团队开发的一款上传插件,对于现代浏览器采用了HTML5的方式进行上传,二为了兼容IE浏览器则采用了Flash的方式作为补充. 首先,这个插件在全局仅暴露一个WebUp ...
- 上海仪电Azure Stack技术深入浅出系列1:谈Azure Stack在私有云/混合云生态中的定位
2.2 Azure Stack Azure Stack到2017年7月才提供GA版本,但目前还是可以通过技术预览版了解该技术.Azure Stack本质上是核心Azure服务的一个私有实例. Micr ...
- php多站点配置以及Forbidden You don't have permission to access / on this server问题解决
php多站点配置以及Forbidden You don't have permission to access / on this server问题解决 一.总结 一句话总结:我的问题是php的版本问 ...
- 这些HTML、CSS知识点,面试和平时开发都需要 No10-No11(知识点:表格操作、代码编写规则)
系列知识点汇总 1.基础篇 这些HTML.CSS知识点,面试和平时开发都需要 No1-No4(知识点:HTML.CSS.盒子模型.内容布局) 这些HTML.CSS知识点,面试和平时开发都需要 No5- ...
- Getting 'The AWS Access Key Id you provided does not exist in our records' error with Amazon MWS
I upgraded from one version of Amazon MWS (marketplace web service) version https://mws.amazonservic ...
- HDU 1969 精度二分
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...