harmony 鸿蒙普通对象
普通对象
普通对象跨线程时通过拷贝形式传递,两个线程的对象内容一致,但是指向各自线程的隔离内存区间,被分配在各自线程的虚拟机本地堆(LocalHeap)。例如,Ecmascript262规范定义的Object、Array、Map等对象通过这种方式实现跨并发实例通信。通信过程如图所示:
说明:
普通类实例对象跨线程通过拷贝形式传递,只能传递数据,类实例上的方法会丢失。可以使用@Sendable装饰器标识为Sendable类,类实例对象跨线程传递后,可携带类方法。
使用示例
此处提供了一个传递普通对象的示例,具体实现如下:
// Test.ets
// 自定义class TestA
export class TestA {
constructor(name: string) {
this.name = name;
}
name: string = 'ClassA';
}
// Index.ets
import { taskpool } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
import { TestA } from './Test';
@Concurrent
async function test1(arg: TestA) {
console.info("TestA name is: " + arg.name);
}
@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(() => {
// 1. 创建Test实例objA
let objA = new TestA("TestA");
// 2. 创建任务task,将objA传递给该任务,objA非sendable对象,通过序列化传递给子线程
let task = new taskpool.Task(test1, objA);
// 3. 执行任务
taskpool.execute(task).then(() => {
console.info("taskpool: execute task success!");
}).catch((e:BusinessError) => {
console.error(`taskpool: execute task: Code: ${e.code}, message: ${e.message}`);
})
})
}
.height('100%')
.width('100%')
}
}
你可能感兴趣的鸿蒙文章
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦