鸿蒙ArkTS实战开发:多线程并发-Worker
1.Worker简介Worker主要作用是为应用程序提供一个多线程的运行环境,可满足应用程序在实行过程中与主线程分离,在背景线程中运行一个脚本操纵耗时操纵,极大制止类似于计算密集型或高耽误的使命壅闭主线程的运行。
创建Worker的线程称为宿主线程(不一定是主线程,工作线程也支持创建Worker子线程),Worker自身的线程称为Worker子线程(或Actor线程、工作线程)。每个Worker子线程与宿主线程拥有独立的实例,包含底子设施、对象、代码段等。Worker子线程和宿主线程之间的通讯是基于消息传递的,Worker通过序列化机制与宿主线程之间相互通讯,完成下令及数据交互。
2.Worker注意事项
[*] 创建Worker时,传入的Worker.ts路径在不同版本有不同的规则,详情请参见文件路径注意事项。
[*] Worker创建后需要手动管理生命周期,且最多同时运行的Worker子线程数目为8个,详情请参见生命周期注意事项。
[*] Ability范例的Module支持使用Worker,Library范例的Module不支持使用Worker。
[*] 创建Worker不支持使用其他Module的Worker.ts文件,即不支持跨模块调用Worker。
[*] 由于不同线程中上下文对象是不同的,因此Worker线程只能使用线程安全的库,比方UI相关的非线程安全库不能使用。
[*] 序列化传输的数据量巨细限制为16MB。
[*] 使用Worker模块时,需要在主线程中注册onerror接口,否则当worker线程出现非常时会发生jscrash问题。
[*] 不支持跨HAP使用Worker线程文件。
3.生命周期注意事项
Worker的创建和销毁耗费性能,发起开发者合理管理已创建的Worker并重复使用。Worker空闲时也会不绝运行,因此当不需要Worker时,可以调用terminate()接口或parentPort.close()方法主动销毁Worker。若Worker处于已销毁或正在销毁等非运行状态时,调用其功能接口,会抛出相应的错误。
Worker存在数目限制,支持最多同时存在8个Worker。
在API version 8及之前的版本,当Worker数目超出限制时,会抛出“Too many workers, the number of workers exceeds the maximum.”错误。从API version 9开始,当Worker数目超出限制时,会抛出“Worker initialization failure, the number of workers exceeds the maximum.”错误。
4.示例
DevEco Studio 4.0 Release中,通过File->New->Worker,创建MyWorker https://img-blog.csdnimg.cn/direct/8510a6b57b894fb38d1a63d18fca230f.png
完成创建后,wokers目录与pages目录同级,所以后续声明woker时注意路径。
https://img-blog.csdnimg.cn/direct/f11ce6089f214910a957e5b096dd14f3.png
4.1 MyWorker.ts文件
import worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
/**
* Defines the event handler to be called when the worker thread receives a message sent by the host thread.
* The event handler is executed in the worker thread.
*
* @param e message data
*/
workerPort.onmessage = function (e: MessageEvents) {
let oldArr = e.data;
let newArr = strSort(oldArr);
workerPort.postMessage(newArr);
}
/**
* Defines the event handler to be called when the worker receives a message that cannot be deserialized.
* The event handler is executed in the worker thread.
*
* @param e message data
*/
workerPort.onmessageerror = function (e: MessageEvents) {
console.log("onmessageerror worker::error = " + e.data);
}
/**
* Defines the event handler to be called when an exception occurs during worker execution.
* The event handler is executed in the worker thread.
*
* @param e error message
*/
workerPort.onerror = function (e: ErrorEvent) {
console.log("onerror worker::error ");
}
function strSort(inPutArr): string {
let newArr = inPutArr.sort();
for (let index = 0; index < newArr.length; index++) {
const element = newArr;
console.info(`ConcurrencyPage newArr js ${element}`)
}
return newArr;
} 4.2.工作线程相关变量界说
//注意ThreadWorker的路径
myWorker: worker.ThreadWorker = new worker.ThreadWorker("entry/ets/workers/MyWorker.ts");
jsWorkerInPutArr: string[] = [];
@State jsWorkerOutPutStr: string = ''
@State jsWorkerInPutStr: string = 'I am,go home,go,thank you,help me,wellcome,get out,I need your help,bye bye,good night'
4.3.工作线程调度
工作线程根据输入的字符串数组,发送到工作线程中处置惩罚。处置惩罚完成后,使用jsWorkerOutPutStr接收处置惩罚效果。
async executeWorkerFunc(inPutArr: string[]): Promise<void> {
if (!this.jsWorkerInPutStr.length) {
this.jsWorkerOutPutStr = "No input for the string to be sorted.\n";
return;
}
for (let index = 0; index < inPutArr.length; index++) {
console.log(`${this.TAG}, jsWorkerInPutStr:${inPutArr}`)
}
this.myWorker.postMessage(inPutArr);
let strFlag = false;
let outPutStr = '';
this.myWorker.onmessage = (e) => {
outPutStr = e.data.toString();
console.log(`${this.TAG}, onmessage outPutStr:${outPutStr}`)
strFlag = true;
}
while (!strFlag) {
await this.promiseCase();
}
this.jsWorkerOutPutStr = outPutStr;
}
async promiseCase(): Promise<Object | void> {
let p: Promise<Object | void> = new Promise<Object>((resolve: Function, reject: Function) => {
setTimeout(() => {
resolve(1)
}, 100)
}).then(undefined, () => {
})
return p;
} 4.4.UI线程中的调用
Text('Worker主要作用是为应用程序提供一个多线程的运行环境,可满足应用程序在执行过程中与主线程分离,在后台线程中运行一个脚本操作耗时操作,极大避免类似于计算密集型或高延迟的任务阻塞主线程的运行')
.width('80%')
Text('Worker创建后需要手动管理生命周期,且最多同时运行的Worker子线程数量为8个,详情请参见生命周期注意事项。\nAbility类型的Module支持使用Worker,Library类型的Module不支持使用Worker。\n创建Worker不支持使用其他Module的Worker.ts文件,即不支持跨模块调用Worker。\n由于不同线程中上下文对象是不同的,因此Worker线程只能使用线程安全的库,例如UI相关的非线程安全库不能使用。\n序列化传输的数据量大小限制为16MB。\n使用Worker模块时,需要在主线程中注册onerror接口,否则当worker线程出现异常时会发生jscrash问题。')
.width('80%')
Text(this.jsWorkerInPutStr)
.width('80%').fontColor(Color.Green)
Text(`Worker 字符串排序后:${this.jsWorkerOutPutStr}`)
.width('80%')
Button('Worker分割字符串')
.width('80%')
.onClick(() => {
this.jsWorkerInPutArr = this.jsWorkerInPutStr.trim().split(',');
this.jsWorkerOutPutStr = '';
this.executeWorkerFunc(this.jsWorkerInPutArr);
}) 5.效果
https://img-blog.csdnimg.cn/direct/083c5c2fa7e24f68854cb82babe0a844.png
最后
如果你想快速提升鸿蒙技能,那么可以直接领取这份包含了:【OpenHarmony多媒体技能、Stage模子、ArkUI多端部署、分布式应用开发、音频、视频、WebGL、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战】等技能知识点。
鸿蒙Next全套VIP学习资料←点击领取!(安全链接,放心点击)
1.鸿蒙焦点技能学习门路
https://img-blog.csdnimg.cn/direct/f223303f59cb48f2b3d7faeda7e431f7.png
2.大厂口试必问口试题
https://img-blog.csdnimg.cn/direct/6a8d1476de8b4606adef673206366f64.png
3.鸿蒙南向开发技能
https://img-blog.csdnimg.cn/direct/21bf9d9da77840fc9748768d594d232f.png
4.鸿蒙APP开发必备
https://img-blog.csdnimg.cn/direct/339886a24c544fe4a2291e51c0250a77.png
5.HarmonyOS Next 最新全套视频教程
https://img-blog.csdnimg.cn/direct/c84524dea615446ebe7622a7c6326124.png
6.鸿蒙生态应用开发白皮书V2.0PDF
https://img-blog.csdnimg.cn/direct/8939d548a4e14cf79861d11af83ee11b.png
这份全套完整版的学习资料已经全部打包好,朋侪们如果需要可以点击→鸿蒙Next全套VIP学习资料免费领取(安全链接,放心点击)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]