1e41f4b71Sopenharmony_ci# Sendable对象简介
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci在传统JS引擎上,对象的并发通信开销的优化方式只有一种,就是把实现下沉到Native侧,通过[Transferable对象](transferabled-object.md)的转移或共享方式降低并发通信开销。而开发者仍然还有大量对象并发通信的诉求,这个问题在业界的JS引擎实现上并没有得到解决。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciArkTS提供了Sendable对象类型,在并发通信时支持通过引用传递来解决上述问题。
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciSendable对象为可共享的,其跨线程前后指向同一个JS对象,如果其包含了JS或者Native内容,均可以直接共享,如果底层是Native实现的,则需要考虑线程安全性。通信过程如下图所示:
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci![sendable](figures/sendable.png)
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci与其它ArkTS对象不一样的是,符合Sendable协议的数据对象在运行时必须是类型固定的对象。
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci当多个并发实例尝试同时更新Sendable数据时,会发生数据竞争,例如[ArkTS共享容器](arkts-collections-introduction.md)的多线程操作。因此,ArkTS提供了[异步锁](arkts-async-lock-introduction.md)的机制来避免不同并发实例间的数据竞争。同时,还可以通过[对象冻结接口](sendable-freeze.md)冻结对象,将其变为只读对象,就可以不用考虑数据的竞争问题。
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciSendable对象提供了并发实例间高效的通信效率,即引用传递的能力,一般适用于开发者自定义大对象需要线程间通信的场景,例如子线程读取数据库的数据返回主线程。
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## 基础概念
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci### Sendable协议
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ciSendable协议定义了ArkTS的可共享对象体系及其规格约束。符合Sendable协议的数据(以下简称Sendable对象)可以在ArkTS并发实例间传递。
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci默认情况下,Sendable数据在ArkTS并发实例间(包括主线程、TaskPool、Worker线程)传递的行为是引用传递。同时,ArkTS也支持Sendable数据在ArkTS并发实例间拷贝传递。
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci### ISendable
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci在ArkTS语言基础库[@arkts.lang](../reference/apis-arkts/js-apis-arkts-lang.md)中引入了interface ISendable,没有任何必须的方法或属性。ISendable是所有Sendable类型(除了null和undefined)的父类型。ISendable主要用在开发者自定义Sendable数据结构的场景中。类装饰器[@Sendable装饰器](#sendable装饰器)是implement ISendable的语法糖。
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci### Sendable class
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci> **说明:**
32e41f4b71Sopenharmony_ci>
33e41f4b71Sopenharmony_ci> 从API version 11开始,支持使用\@Sendable装饰器校验Sendable class。
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ciSendable class需同时满足以下两个规则:
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci1. 当且仅当被标注了[@Sendable装饰器](#sendable装饰器)。
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci2. 需满足Sendable约束,详情可查[Sendable使用规则](sendable-constraints.md)。
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci### Sendable function
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci> **说明:**
44e41f4b71Sopenharmony_ci>
45e41f4b71Sopenharmony_ci> - 从API version 12开始,支持使用\@Sendable装饰器校验Sendable function。
46e41f4b71Sopenharmony_ci>
47e41f4b71Sopenharmony_ci> - 开发者如需在API12上使用Sendable function,需在工程中配置"compatibleSdkVersionStage": "beta3",否则其Sendable特性将不生效。参考[build-profile.json5配置文件说明](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-hvigor-build-profile-V5)。
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ciSendable function需同时满足以下两个规则:
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci1. 当且仅当被标注了[@Sendable装饰器](#sendable装饰器)。
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci2. 需满足Sendable约束,详情可查[Sendable使用规则](sendable-constraints.md)。
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci### Sendable interface
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ciSendable interface需同时满足以下两个规则:
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci1. 当且仅当是[ISendable](#isendable)或者继承了ISendable。
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci2. 需满足Sendable约束,详情可查[Sendable使用规则](sendable-constraints.md)。
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci### Sendable支持的数据类型
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci- 所有的ArkTS基本数据类型:boolean, number, string, bigint, null, undefined。
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci- ArkTS语言标准库中定义的[容器类型数据](arkts-collections-introduction.md)(须显式引入[@arkts.collections](../reference/apis-arkts/js-apis-arkts-collections.md))。
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci- ArkTS语言标准库中定义的[异步锁对象](arkts-async-lock-introduction.md)(须显式引入[@arkts.utils](../reference/apis-arkts/js-apis-arkts-utils.md))。
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci- 继承了[ISendable](#isendable)的interface。
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci- 标注了[@Sendable装饰器](#sendable装饰器)的class。
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci- 标注了[@Sendable装饰器](#sendable装饰器)的function。
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci- 接入Sendable的系统对象。
78e41f4b71Sopenharmony_ci  - [共享用户首选项](../reference/apis-arkdata/js-apis-data-sendablePreferences.md)
79e41f4b71Sopenharmony_ci  - [可共享的色彩管理](../reference/apis-arkgraphics2d/js-apis-sendableColorSpaceManager.md)
80e41f4b71Sopenharmony_ci  - [基于Sendable对象的图片处理](../reference/apis-image-kit/js-apis-sendableImage.md)
81e41f4b71Sopenharmony_ci  - [资源管理](../reference/apis-localization-kit/js-apis-sendable-resource-manager.md)
82e41f4b71Sopenharmony_ci  - [SendableContext对象管理](../reference/apis-ability-kit/js-apis-app-ability-sendableContextManager.md)
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci- 元素均为Sendable类型的union type数据。
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci> **说明:**
87e41f4b71Sopenharmony_ci>
88e41f4b71Sopenharmony_ci> - JS内置对象在并发实例间的传递遵循结构化克隆算法,跨线程行为是拷贝传递。因此JS内置对象的实例不是Sendable类型。
89e41f4b71Sopenharmony_ci>
90e41f4b71Sopenharmony_ci> - 对象字面量、数组字面量在并发实例间的传递遵循结构化克隆算法,跨线程行为是拷贝传递。因此,对象字面量和数组字面量不是Sendable类型。
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci## Sendable的实现原理
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci为了实现[Sendable数据](#sendable支持的数据类型)在不同并发实例间的引用传递,Sendable共享对象会分配在共享堆中,以实现跨并发实例的内存共享。
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci共享堆(SharedHeap)是进程级别的堆空间,与虚拟机本地堆(LocalHeap)不同的是,LocalHeap只能被单个并发实例访问,而SharedHeap可以被所有线程访问。一个Sendable共享对象的跨线程行为是引用传递。因此,Sendable可能被多个并发实例引用,判断Sendable共享对象是否存活,取决于所有并发实例的对象是否存在对此Sendable共享对象的引用。
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci**SharedHeap与LocalHeap关系图**
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci![zh-cn_image_0000002001521153](figures/zh-cn_image_0000002001521153.png)
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci各个并发实例间的LocalHeap是隔离的,SharedHeap是进程级别的堆,可以被所有的并发实例引用。但是SharedHeap不能引用LocalHeap中的对象。
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci## \@Sendable装饰器
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci声明并校验Sendable class以及Sendable function。
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci| \@Sendable装饰器 | 说明 |
111e41f4b71Sopenharmony_ci| -------- | -------- |
112e41f4b71Sopenharmony_ci| 装饰器参数 | 无。 |
113e41f4b71Sopenharmony_ci| 使用场景限制 | 仅支持在Stage模型的工程中使用。仅支持在.ets文件中使用。 |
114e41f4b71Sopenharmony_ci| 装饰的函数类型限制 | 仅支持装饰普通function和Async function类型。 |
115e41f4b71Sopenharmony_ci| 装饰的类继承关系限制 | Sendable class只能继承Sendable class,普通Class不可以继承Sendable class。 |
116e41f4b71Sopenharmony_ci| 装饰的对象内的属性类型限制 | 1. 支持string、number、boolean、bigint、null、undefined、Sendable class、collections.Arraycollections.Mapcollections.SetArkTSUtils.locks.AsyncLock。<br/>2. 禁止使用闭包变量。<br/>3. 不支持通过\#定义私有属性,需用private。<br/>4. 不支持计算属性。 |
117e41f4b71Sopenharmony_ci| 装饰的对象内的属性的其他限制 | 成员属性必须显式初始化。成员属性不能跟感叹号。 |
118e41f4b71Sopenharmony_ci| 装饰的函数或类对象内的方法参数限制 | 允许使用local变量、入参和通过import引入的变量。禁止使用闭包变量,定义在顶层的Sendable class和Sendable function除外。 |
119e41f4b71Sopenharmony_ci| Sendable Class及Sendable Function的限制 | 不支持增加属性、不支持删除属性、允许修改属性,修改前后属性的类型必须一致、不支持修改方法。 |
120e41f4b71Sopenharmony_ci| 适用场景 | 1. 在TaskPool或Worker中使用类方法/Sendable函数。<br/>2. 传输对象数据量较大的使用场景。 |
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci**装饰器修饰Class使用示例:**
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci```ts
125e41f4b71Sopenharmony_ci@Sendable
126e41f4b71Sopenharmony_ciclass SendableTestClass {
127e41f4b71Sopenharmony_ci  desc: string = "sendable: this is SendableTestClass ";
128e41f4b71Sopenharmony_ci  num: number = 5;
129e41f4b71Sopenharmony_ci  printName() {
130e41f4b71Sopenharmony_ci    console.info("sendable: SendableTestClass desc is: " + this.desc);
131e41f4b71Sopenharmony_ci  }
132e41f4b71Sopenharmony_ci  get getNum(): number {
133e41f4b71Sopenharmony_ci    return this.num;
134e41f4b71Sopenharmony_ci  }
135e41f4b71Sopenharmony_ci}
136e41f4b71Sopenharmony_ci```
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci**装饰器修饰Function使用示例:**
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci```ts
141e41f4b71Sopenharmony_ci@Sendable
142e41f4b71Sopenharmony_citype SendableFuncType = () => void;
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci@Sendable
145e41f4b71Sopenharmony_ciclass TopLevelSendableClass {
146e41f4b71Sopenharmony_ci  num: number = 1;
147e41f4b71Sopenharmony_ci  PrintNum() {
148e41f4b71Sopenharmony_ci    console.info("Top level sendable class");
149e41f4b71Sopenharmony_ci  }
150e41f4b71Sopenharmony_ci}
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci@Sendable
153e41f4b71Sopenharmony_cifunction TopLevelSendableFunction() {
154e41f4b71Sopenharmony_ci  console.info("Top level sendable function");
155e41f4b71Sopenharmony_ci}
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci@Sendable
158e41f4b71Sopenharmony_cifunction SendableTestFunction() {
159e41f4b71Sopenharmony_ci  const topClass = new TopLevelSendableClass(); // 顶层sendable class
160e41f4b71Sopenharmony_ci  topClass.PrintNum();
161e41f4b71Sopenharmony_ci  TopLevelSendableFunction(); // 顶层sendable function
162e41f4b71Sopenharmony_ci  console.info("Sendable test function");
163e41f4b71Sopenharmony_ci}
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci@Sendable
166e41f4b71Sopenharmony_ciclass SendableTestClass {
167e41f4b71Sopenharmony_ci  constructor(func: SendableFuncType) {
168e41f4b71Sopenharmony_ci    this.callback = func;
169e41f4b71Sopenharmony_ci  }
170e41f4b71Sopenharmony_ci  callback: SendableFuncType; // 顶层sendable function
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci  CallSendableFunc() {
173e41f4b71Sopenharmony_ci    SendableTestFunction(); // 顶层sendable function
174e41f4b71Sopenharmony_ci  }
175e41f4b71Sopenharmony_ci}
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_cilet sendableClass = new SendableTestClass(SendableTestFunction);
178e41f4b71Sopenharmony_cisendableClass.callback();
179e41f4b71Sopenharmony_cisendableClass.CallSendableFunc();
180e41f4b71Sopenharmony_ci```
181