# 普通对象 普通对象跨线程时通过拷贝形式传递,两个线程的对象内容一致,但是指向各自线程的隔离内存区间,被分配在各自线程的虚拟机本地堆(LocalHeap)。例如Ecmascript262规范定义的Object、Array、Map等对象是通过这种方式实现跨并发实例通信的。通信过程如下图所示: ![deep_copy](figures/deep_copy.png) ## 使用示例 此处提供了一个传递普通对象的简单示例,具体实现如下: ```ts // Test.ets // 自定义class TestA export class TestA { constructor(name: string) { this.name = name; } name: string = 'ClassA'; } ``` ```ts // 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%') } } ```