网站到期查询备案,c 做网站session用法,邯郸形象网站建设,合肥做网站哪家公司好【高心星出品】 文章目录 多线程Worker和Sendable的使用方法开发步骤运行结果 多线程Worker和Sendable的使用方法
Worker在HarmonyOS中提供了一种多线程的实现方式#xff0c;它允许开发者在后台线程中执行长耗时任务#xff0c;从而避免阻塞主线程并提高应用的响应性。
S…【高心星出品】 文章目录 多线程Worker和Sendable的使用方法开发步骤运行结果 多线程Worker和Sendable的使用方法
Worker在HarmonyOS中提供了一种多线程的实现方式它允许开发者在后台线程中执行长耗时任务从而避免阻塞主线程并提高应用的响应性。
Sendable 注解主要用于标记那些需要在多线程环境中共享的数据对象或函数。被 Sendable 标记的对象或函数可以在不同的线程之间高效地传输数据这主要得益于 ArkTS 的序列化和反序列化机制。
开发步骤
【案例需求】 接下来要实现一个案例创建两个子线程一个子线程负责数据求和一个子线程负责数据相减UI线程提供共享数据给这两个子线程子线程运行结果返回给UI线程。 创建两个worker。 在ets/下创建一个workers目录在该目录下创建worker。这两个worker负责接受UI线程传过来的数据并且负责子线程运行实体并将结果发送给UI线程。 addworker的代码
import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from kit.ArkTS;
import { Temp } from ../pages/Index;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 event message data*/
// 线程运行实体
function add(temp:Temp){let atemp.alet btemp.btemp.a5return ab
}
// 子线程接受UI线程信息并处理
workerPort.onmessage async (event: MessageEvents) {if(event){// 模拟线程睡眠2sawait new Promise((resolve:(v:number)void){setTimeout((){resolve(10)},2000)})// 解析获取ui线程发送的数据let tevent.data as Temp// 子线程向UI线程发送消息workerPort.postMessage(add(t))}
};/*** 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 event message data*/
workerPort.onmessageerror (event: MessageEvents) {
};/*** Defines the event handler to be called when an exception occurs during worker execution.* The event handler is executed in the worker thread.** param event error message*/
workerPort.onerror (event: ErrorEvent) {
};jianworker的代码
import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from kit.ArkTS;
import { Temp } from ../pages/Index;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 event message data*/
// 线程运行实体
function jian(t:Temp){let at.alet bt.bt.a-5return Math.abs(a-b)
}
// 子线程接受UI线程的信息 并运行
workerPort.onmessage async (event: MessageEvents) {if(event){// 模拟线程睡眠2sawait new Promise((resolve:(v:string)void){setTimeout((){resolve(a)},2000)})let tevent.data as Temp// 向UI线程发送消息workerPort.postMessage(jian(t))}
};/*** 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 event message data*/
workerPort.onmessageerror (event: MessageEvents) {
};/*** Defines the event handler to be called when an exception occurs during worker execution.* The event handler is executed in the worker thread.** param event error message*/
workerPort.onerror (event: ErrorEvent) {
};Index.ets代码
import { MessageEvents, worker } from kit.ArkTS;Sendable
export class Temp {a: numberb: numberconstructor(a: number, b: number) {this.a a;this.b b;}
}
Entry
Component
struct Index {State message: string Hello World;// UI线程和其他两个子线程共享的数据private temp new Temp(10, 20)//创建了两个线程private addthread new worker.ThreadWorker(entry/ets/workers/addworker.ets)private jianthread new worker.ThreadWorker(entry/ets/workers/jianworker.ets)aboutToAppear(): void {// UI线程中接受两个子线程发送的信息this.addthread.onmessage (event: MessageEvents) {console.log(gxxt add , event.data as number)console.log(gxxt 当前的temp值 ,JSON.stringify(this.temp))}this.jianthread.onmessage (event: MessageEvents) {console.log(gxxt jian , event.data as number)console.log(gxxt 当前的temp值 ,JSON.stringify(this.temp))}}build() {Column({ space: 20 }) {Button(加线程).width(60%).onClick(() {this.addthread.postMessageWithSharedSendable(this.temp)})Button(减线程).width(60%).onClick(() {this.jianthread.postMessageWithSharedSendable(this.temp)})}.height(100%).width(100%).justifyContent(FlexAlign.Center)}
}项目通过点击两个按钮启动两个线程并将共享数据temp发送给两个子线程两个子线程分别执行相加和相减同时还更新共享数据的原始值通过观察运算结果和共享数据的变化我们能掌握worker的开发方式。
运行结果
02-28 09:10:59.798 3292-3292 A03d00/JSAPP com.gxx.workdemo I gxxt add运算结果 30
02-28 09:10:59.799 3292-3292 A03d00/JSAPP com.gxx.workdemo I gxxt 当前的temp值 {a:15,b:20}
02-28 09:11:04.877 3292-3292 A03d00/JSAPP com.gxx.workdemo I gxxt jian运算结果 5
02-28 09:11:04.877 3292-3292 A03d00/JSAPP com.gxx.workdemo I gxxt 当前的temp值 {a:10,b:20}刚一开始进行运算的时候add线程面对的a为10b为20计算结果为30add线程同时aa5操作所以此时UI线程得到的a为15然后jian线程运行结果为|15-20|jian线程同时aa-5,所以此时UI线程得到的a为10. 文章转载自: http://www.morning.pqxjq.cn.gov.cn.pqxjq.cn http://www.morning.yrmgh.cn.gov.cn.yrmgh.cn http://www.morning.wdwfm.cn.gov.cn.wdwfm.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.tfpbm.cn.gov.cn.tfpbm.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.zkbxx.cn.gov.cn.zkbxx.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.rpzqk.cn.gov.cn.rpzqk.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.lgtcg.cn.gov.cn.lgtcg.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.rbktw.cn.gov.cn.rbktw.cn http://www.morning.taipinghl.cn.gov.cn.taipinghl.cn http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.qcbhb.cn.gov.cn.qcbhb.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.nbrdx.cn.gov.cn.nbrdx.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.mxmdd.cn.gov.cn.mxmdd.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.gbsfs.com.gov.cn.gbsfs.com http://www.morning.ykmg.cn.gov.cn.ykmg.cn http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.fykqh.cn.gov.cn.fykqh.cn http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.kycwt.cn.gov.cn.kycwt.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.flhnd.cn.gov.cn.flhnd.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.tralution.cn.gov.cn.tralution.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.ykswq.cn.gov.cn.ykswq.cn http://www.morning.ylsxk.cn.gov.cn.ylsxk.cn http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.fthcq.cn.gov.cn.fthcq.cn http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.hjrjr.cn.gov.cn.hjrjr.cn http://www.morning.ffwrq.cn.gov.cn.ffwrq.cn http://www.morning.mmhyx.cn.gov.cn.mmhyx.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.xqspn.cn.gov.cn.xqspn.cn http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.kcwkt.cn.gov.cn.kcwkt.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.wcjgg.cn.gov.cn.wcjgg.cn http://www.morning.ybgyz.cn.gov.cn.ybgyz.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.xswrb.cn.gov.cn.xswrb.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.pumali.com.gov.cn.pumali.com http://www.morning.demoux.com.gov.cn.demoux.com http://www.morning.twpq.cn.gov.cn.twpq.cn http://www.morning.ktfnj.cn.gov.cn.ktfnj.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.fkcjs.cn.gov.cn.fkcjs.cn http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn http://www.morning.cbczs.cn.gov.cn.cbczs.cn