#鸿蒙通关秘籍#如何在鸿蒙系统中通过转移方式传递ArrayBuffer对象?

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
CTP金枝玉叶

在鸿蒙系统中,默认通过转移的方式传递ArrayBuffer对象。转移方式可以提高性能,传递后原线程无法使用传输到子线程的ArrayBuffer。在代码中通过传递truecreateImageTask函数中的第二个参数来确认传递方式为转移即可。

typescript // Index.ets import { taskpool } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit';

@Concurrent function adjustImageValue(arrayBuffer: ArrayBuffer): ArrayBuffer { return arrayBuffer; }

function createImageTask(arrayBuffer: ArrayBuffer, isParamsByTransfer: boolean): taskpool.Task { let task: taskpool.Task = new taskpool.Task(adjustImageValue, arrayBuffer); return task; }

@Entry @Component struct Index { @State message: string = 'Hello World';

build() { RelativeContainer() { Text(this.message) .id('HelloWorld') .fontSize(50) .fontWeight(FontWeight.Bold) .alignRules({ center: { anchor: 'container', align: VerticalAlign.Center }, middle: { anchor: 'container', align: HorizontalAlign.Center } }) .onClick(() => { let taskNum = 4; let arrayBuffer = new ArrayBuffer(1024 * 1024); let taskPoolGroup = new taskpool.TaskGroup(); for (let i: number = 0; i < taskNum; i++) { let arrayBufferSlice: ArrayBuffer = arrayBuffer.slice(arrayBuffer.byteLength / taskNum * i, arrayBuffer.byteLength / taskNum * (i + 1)); taskPoolGroup.addTask(createImageTask(arrayBufferSlice, true)); } taskpool.execute(taskPoolGroup).then((data) => {}).catch((e: BusinessError) => { console.error(e.message); }) }) } .height('100%') .width('100%') } }

分享
微博
QQ
微信
回复
2天前
相关问题