【HarmonyOS NEXT】鸿蒙多线程Sendable开发

非共享模块在同一线程内只加载一次,在不同线程间会加载多次,单例类也会创建多次,导致数据不共享,在不同的线程内都会产生新的模块对象

基础概念

Sendable协议

Sendable协议定义了ArkTS的可共享对象体系及其规格约束。符合Sendable协议的数据(以下简称Sendable数据)可以在ArkTS并发实例间传递。

默认情况下,Sendable数据在ArkTS并发实例间(包括主线程、TaskPool&Worker工作线程)传递的行为是引用传递。同时,ArkTS支持Sendable数据在ArkTS并发实例间的拷贝传递。

当多个并发实例尝试同时更新可变Sendable数据时,会发生数据竞争。ArkTS提供了异步锁的机制来避免不同并发实例间的数据竞争。

示例:

import { taskpool, worker } from '@kit.ArkTS';

@Sendable
class A {}

let a: A = new A();

@Concurrent
function foo(a: A) {}
let task: taskpool.Task = new taskpool.Task(foo, a)

let w = new worker.ThreadWorker("entry/ets/workers/Worker.ets")

// 1. TaskPool 共享传输实现方式
taskpool.execute(task).then(() => {})

// 2. Worker 共享传输实现方式
w.postMessageWithSharedSendable(a)

// 3. TaskPool 拷贝传输实现方式
task.setCloneList([a])
taskpool.execute(task).then(() => {})

// 4. Worker 拷贝传输实现方式
w.postMessage(a)

Sendable class

Sendable class需同时满足以下两个规则:

  1. 当且仅当被标注了@Sendable装饰器。
  2. 需满足Sendable约束,详情可查Sendable使用规则。

Sendable interface

Sendable interface需同时满足以下两个规则:

  1. 当且仅当是ISendable或者继承了ISendable。
  2. 需满足Sendable约束,详情可查Sendable使用规则。

Sendable支持的数据类型

  • 所有的ArkTS基本数据类型:boolean, number, string, bigint, null, undefined。
  • ArkTS语言标准库中定义的容器类型数据(须显式引入@arkts.collections)。
  • ArkTS语言标准库中定义的AsyncLock对象(须显式引入@arkts.utils)。
  • 继承了ISendable的interface。
  • 标注了@Sendable装饰器的class。
  • 接入Sendable的系统对象类型(详见Sendable系统对象)。
  • 元素均为Sendable类型的union type数据。

说明:

  • JS内置对象在并发实例间的传递遵循结构化克隆算法,语义为拷贝传递。因此JS内置对象的实例不是Sendable类型。

  • 对象字面量、数组字面量在并发实例间的传递遵循结构化克隆算法,语义为拷贝传递。因此,对象字面量和数组字面量不是Sendable类型。

  • ArkTS容器集与原生API行为差异具体参考行为差异汇总。

ISendable

在ArkTS语言基础库@arkts.lang中引入interface ISendable {},没有任何必须的方法或属性。ISendable是所有Sendable类型(除了null和undefined)的父类型。ISendable主要用在开发者自定义Sendable数据结构的场景中。类装饰器@Sendable是implement ISendable的语法糖。

@Sendable装饰器:声明并校验Sendable class

说明:

从API version 11开始,该装饰器支持在ArkTS卡片中使用。

装饰器说明

@Sendable类装饰器说明
装饰器参数无。
使用场景限制仅支持在Stage模型的工程中使用。仅支持在.ets文件中使用。
装饰的类继承关系限制Sendable class只能继承Sendable class,普通Class不可以继承Sendable class。
装饰的对象内的属性类型限制1. 支持string、number、boolean、bigint、null、undefined、Sendable class、collections.Array、collections.Map、collections.Set。
2. 禁止使用闭包变量。
3. 不支持#定义私有属性,需用private。
4. 不支持计算属性。
装饰的对象内的属性的其他限制成员属性必须显式初始化。成员属性不能跟感叹号。
装饰的对象内的方法参数限制允许使用local变量、入参和通过import引入的变量。禁止使用闭包变量。
Sendable Class的限制不支持增加属性、不支持删除属性、允许修改属性,修改前后属性的类型必须一致、不支持修改方法。
适用场景1. 在TaskPool或Worker中使用类方法。
2. 传输对象数据量较大的使用场景。

装饰器使用示例

@Sendable
class SendableTestClass {
  desc: string = "sendable: this is SendableTestClass ";
  num: number = 5;
  printName() {
    console.info("sendable: SendableTestClass desc is: " + this.desc);
  }
  get getNum(): number {
    return this.num;
  }
}

Sendable使用规则

1. Sendable class只能继承自Sendable class

说明:

这里的class不包括变量。Sendable class不能继承自变量。

正例:

@Sendable
class A {
  constructor() {
  }
}

@Sendable
class B extends A {
  constructor() {
    super()
  }
}

反例:

class A {
  constructor() {
  }
}

@Sendable
class B extends A {
  constructor() {
    super()
  }
}

2. 非Sendable class只能继承自非Sendable class

正例:

class A {
  constructor() {
  }
}

class B extends A {
  constructor() {
    super()
  }
}

反例:

@Sendable
class A {
  constructor() {
  }
}

class B extends A {
  constructor() {
    super()
  }
}

3. 非Sendable class只能实现非Sendable interface

正例:

interface I {};

class B implements I {};

反例:

import { lang } from '@kit.ArkTS';

type ISendable = lang.ISendable;

interface I extends ISendable {};

class B implements I {};

4. Sendable class/interface成员变量必须是Sendable支持的数据类型

正例:

@Sendable
class A {
  constructor() {
  }
  a: number = 0;
}

反例:

@Sendable
class A {
  constructor() {
  }
  b: Array<number> = [1, 2, 3] // 需使用collections.Array
}

5. Sendable class/interface的成员变量不支持使用!断言

正例:

@Sendable
class A {
  constructor() {
  }
  a: number = 0;
}

反例:

@Sendable
class A {
  constructor() {
  }
  a!: number;
}

6. Sendable class/interface的成员变量不支持使用计算属性名

正例:

@Sendable
class A {
    num1: number = 1;
    num2: number = 2;
    add(): number {
      return this.num1 + this.num2;
    }
}

反例:

enum B {
    b1 = "bbb"
}
@Sendable
class A {
    ["aaa"]: number = 1; // ["aaa"] is allowed in other classes in ets files
    [B.b1]: number = 2; // [B.b1] is allowed in other classes in ets files
}

7. 泛型类中的Sendable class,collections.Array,collections.Map,collections.Set的模板类型必须是Sendable类型

正例:

import { collections } from '@kit.ArkTS';

try {
  let arr1: collections.Array<number> = new collections.Array<number>();
  let num: number = 1;
  arr1.push(num)
} catch (e) {
  console.error(`taskpool execute: Code: ${e.code}, message: ${e.message}`);
}

反例:

import { collections } from '@kit.ArkTS';

try {
  let arr1: collections.Array<Array<number>> = new collections.Array<Array<number>>();
  let arr2: Array<number> = new Array<number>()
  arr2.push(1)
  arr1.push(arr2)
} catch (e) {
  console.error(`taskpool execute: Code: ${e.code}, message: ${e.message}`);
}

8. Sendable class的内部不允许使用当前模块内上下文环境中定义的变量

由于Sendable对象在不同并发实例间的上下文环境不同,如果直接访问会有非预期行为。不支持Sendable对象使用当前模块内上下文环境中定义的变量,如果违反,编译阶段会报错。

说明:

从API version 12开始,sendable class的内部支持使用top level的sendable class对象。

正例:

import { lang } from '@kit.ArkTS';

type ISendable = lang.ISendable;

interface I extends ISendable {}

@Sendable
class B implements I {
  static o: number = 1;
  static bar(): B {
    return new B();
  }
}

@Sendable
class C {
  v: I = new B();
  u: number = B.o;

  foo() {
    return B.bar();
  }
}

反例:

import { lang } from '@kit.ArkTS';

type ISendable = lang.ISendable;

interface I extends ISendable {}

@Sendable
class B implements I {}

function bar(): B {
  return new B();
}

let b = new B();

{
  @Sendable
  class A implements I {}

  @Sendable
  class C {
    u: I = bar(); // bar不是sendable class对象,编译报错
    v: I = new A(); // A不是定义在top level中,编译报错

    foo() {
      return b; // b不是sendable class对象,而是sendable class的实例,编译报错
    }
  }
}

9. Sendable class中不能使用除了@Sendable的其它装饰器

如果类装饰器定义在ts文件中,产生修改类的布局的行为,那么会造成运行时的错误。

正例:

@Sendable
class A {
  num: number = 1;
}

反例:

@Sendable
@Observed
class C {
  num: number = 1;
}

10. 不能使用对象字面量/数组字面量初始化Sendable类型

Sendable数据类型只能通过Sendable类型的new表达式创建。

正例:

import { collections } from '@kit.ArkTS';

let arr1: collections.Array<number> = new collections.Array<number>(1, 2, 3); // 是Sendable类型

反例:

import { collections } from '@kit.ArkTS';

let arr2: collections.Array<number> = [1, 2, 3]; // 不是Sendable类型,编译报错
let arr3: number[] = [1, 2, 3]; // 不是Sendable类型,正例,不报错
let arr4: number[] = new collections.Array<number>(1, 2, 3); // 编译报错

11. 非Sendable类型不可以as成Sendable类型

说明:

Sendable类型在不违反Sendable规则的前提下需要和非Sendable类型行为兼容,因此Sendable类型可以as成非Sendable类型。

正例:

class A {
  state: number = 0;
}

@Sendable
class SendableA {
  state: number = 0;
}

let a1: A = new SendableA() as A;

反例:

class A {
  state: number = 0;
}

@Sendable
class SendableA {
  state: number = 0;
}

let a2: SendableA = new A() as SendableA;

与TS/JS交互的规则

ArkTS通用规则(目前只针对Sendable对象)

规则
Sendable对象传入TS/JS的接口中,禁止操作其对象布局(增、删属性,改变属性类型)。
Sendable对象设置到TS/JS的对象上,TS中获取到这个Sendable对象后,禁止操作其对象布局(增、删属性,改变属性类型)。
Sendable对象放入TS/JS的容器中,TS中获取到这个Sendable对象后,禁止操作其对象布局(增、删属性,改变属性类型)。

说明:

此处改变属性类型不包括Sendable对象类型的改变,比如从Sendable class A 变为Sendable class B。

NAPI规则(目前只针对Sendable对象)

规则
禁止删除属性,不能使用的接口有:napi_delete_property。
禁止新增属性,不能使用的接口有:napi_set_property、napi_set_named_property、napi_define_properties。
禁止修改属性类型,不能使用的接口有:napi_set_property、napi_set_named_property、napi_define_properties。
不支持Symbol相关接口和类型,不能使用的接口有:napi_create_symbol、napi_is_symbol_object、napi_symbol。

使用场景

Sendable对象可以在不同并发实例间通过引用传递。通过引用传递方式传输对象相比序列化方式更加高效,同时不丢失class上携带的成员方法,因此,Sendable主要可以解决两个场景的问题: 1. 跨并发实例传输大数据(例如可能达到100KB以上) 2. 跨并发实例传递带方法的class实例对象

跨并发实例传输大数据场景开发指导

由于跨并发实例序列化的开销随着数据量线性增长,因此当传输数据量较大时(100KB数据大约1ms传输耗时),跨并发实例的拷贝开销大,影响并行化的性能。引用传递方式传输对象可提升性能。

示例:

// index.ets
import { taskpool } from '@kit.ArkTS';
import { testTypeA, testTypeB, Test } from './sendable'

// 在并发函数中模拟数据处理
@Concurrent
async function taskFunc(obj: Test) {
  console.info("test task res1 is: " + obj.data1.name + " res2 is: " + obj.data2.name);
}

async function test() {
  // 使用taskpool传递数据
  let a: testTypeA = new testTypeA("testTypeA");
  let b: testTypeB = new testTypeB("testTypeB");
  let obj: Test = new Test(a, b);
  let task: taskpool.Task = new taskpool.Task(taskFunc, obj);
  await taskpool.execute(task);
}

test();
// sendable.ets
// 将数据量较大的数据在Sendable class中组装
@Sendable
export class testTypeA {
  name: string = "A";
  constructor(name: string) {
    this.name = name;
  }
}

@Sendable
export class testTypeB {
  name: string = "B";
  constructor(name: string) {
    this.name = name;
  }
}

@Sendable
export class Test {
  data1: testTypeA;
  data2: testTypeB;
  constructor(arg1: testTypeA, arg2: testTypeB) {
    this.data1 = arg1;
    this.data2 = arg2;
  }
}

跨并发实例传递带方法的class实例对象

由于序列化传输实例对象时会丢失方法,在必须调用实例方法的场景中,需使用引用传递方式进行开发。在数据处理过程中有需要解析的数据,可使用ASON工具进行数据解析。

示例:

// Index.ets
import { taskpool, ArkTSUtils } from '@kit.ArkTS'
import { SendableTestClass, ISendable } from './sendable'

// 在并发函数中模拟数据处理
@Concurrent
async function taskFunc(sendableObj: SendableTestClass) {
  console.info("SendableTestClass: name is: " + sendableObj.printName() + ", age is: " + sendableObj.printAge() + ", sex is: " + sendableObj.printSex());
  sendableObj.setAge(28);
  console.info("SendableTestClass: age is: " + sendableObj.printAge());

  // 解析sendableObj.arr数据生成JSON字符串
  let str = ArkTSUtils.ASON.stringify(sendableObj.arr);
  console.info("SendableTestClass: str is: " + str);

  // 解析该数据并生成ISendable数据
  let jsonStr = '{"name": "Alexa", "age": 23, "sex": "female"}';
  let obj = ArkTSUtils.ASON.parse(jsonStr) as ISendable;
  console.info("SendableTestClass: type is: " + typeof obj);
  console.info("SendableTestClass: name is: " + (obj as object)?.["name"]); // 输出: 'Alexa'
  console.info("SendableTestClass: age is: " + (obj as object)?.["age"]); // 输出: 23
  console.info("SendableTestClass: sex is: " + (obj as object)?.["sex"]); // 输出: 'female'
}
async function test() {
  // 使用taskpool传递数据
  let obj: SendableTestClass = new SendableTestClass();
  let task: taskpool.Task = new taskpool.Task(taskFunc, obj);
  await taskpool.execute(task);
}

test();
// sendable.ets
// 定义模拟类Test,模仿开发过程中需传递带方法的class
import { lang, collections  } from '@kit.ArkTS'

export type ISendable = lang.ISendable;

@Sendable
export class SendableTestClass {
  name: string = 'John';
  age: number = 20;
  sex: string = "man";
  arr: collections.Array<number> = new collections.Array<number>(1, 2, 3);
  constructor() {
  }
  setAge(age: number) : void {
    this.age = age;
  }

  printName(): string {
    return this.name;
  }

  printAge(): number {
    return this.age;
  }

  printSex(): string {
    return this.sex;
  }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/766353.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

synchronized用法解析

锁的意义&#xff1a; 比如我跟我老弟要用电脑&#xff0c;我想学java&#xff0c;他想拿电脑打LOL&#xff0c;如果我敲java代码敲的正嗨皮&#xff0c;他突然把电脑抢了过去&#xff0c;代码还没保存&#xff0c;就被他拿去打LOL了&#xff0c;很✓8&#xff0c;那么如何解决…

【Arduino】XIAOFEIYU实验ESP32实验热敏电阻(图文)

今天XIAOFEIYU来实验一下ESP32使用热敏电阻传感器。 热敏电阻具有测试灵敏&#xff0c;测试范围大的特点&#xff0c;具有广泛的使用范围。常温器件适用于-55℃&#xff5e;315℃&#xff0c;高温器件适用温度高于315℃&#xff08;目前最高可达到2000℃&#xff09;&#xff…

[图解]SysML和EA建模住宅安全系统-07-to be块定义图

1 00:00:00,180 --> 00:00:06,820 我们来看&#xff0c;这是之前的那张图&#xff0c;现有的 2 00:00:08,290 --> 00:00:09,160 我们怎么做 3 00:00:09,170 --> 00:00:11,280 你看&#xff0c;我们之前 4 00:00:11,290 --> 00:00:15,600 在现状&#xff0c;as i…

Android AlertDialog对话框

目录 AlertDialog对话框普通对话框单选框多选框自定义框 AlertDialog对话框 部分节选自博主编《Android应用开发项目式教程》&#xff08;机械工业出版社&#xff09;2024.6 在Android中&#xff0c;AlertDialog弹出对话框用于显示一些重要信息或者需要用户交互的内容。 弹出…

GeoServer改造Springboot源码十(样式管理设计)

GeoServer改造Springboot源码一&#xff08;公共部分&#xff09; GeoServer改造Springboot源码二&#xff08;数据源管理设计&#xff09; GeoServer改造Springboot源码三&#xff08;数据源管理代码&#xff09; GeoServer改造Springboot源码四&#xff08;图层管理设计&a…

【知识】DGL中graph默认的稀疏矩阵格式和coo格式不对的坑

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;欢迎[点赞、收藏、关注]哦~ 目录 先给结论 源码解读 代码验证 网上没找到相关的讨论&#xff0c;因此只能从源码上一步步查。 先给结论 对于自己使用dgl.graph接口创建的图&am…

20240702在vmware17.5虚拟机中让ubuntu22.04使用主机的代理上网

20240702在vmware17.5虚拟机中让ubuntu22.04使用主机的代理上网 2024/7/2 14:41 百度&#xff1a;vmware 虚拟机 使用主机代理 上网 https://blog.csdn.net/nomoremorphine/article/details/138738065?utm_mediumdistribute.pc_relevant.none-task-blog-2~default~baidujs_ba…

Elasticsearch集群部署(上)

目录 前言 一. 环境准备 二. 实施部署 三. 安装配置head监控插件 &#xff08;只在第一台es部署&#xff09; 四. Kibana部署&#xff08;当前还是在第一台es部署&#xff09; 五. 安装配置Nginx反向代理 六. Logstash部署与测试 前言 1. Elasticsearch&#xff1a; 是…

什么是文档透明加密|好用的文档透明加密软件有哪些?

在当今日益数字化和信息化的时代&#xff0c;数据安全问题愈发受到企业和个人的关注。文档作为信息的重要载体&#xff0c;其安全性不言而喻。为了保障文档的机密性和完整性&#xff0c;文档透明加密技术应运而生。本文将对文档透明加密进行详细介绍&#xff0c;并探讨一些好用…

多模态融合 + 慢病精准预测

多模态融合 慢病精准预测 慢病预测算法拆解子解法1&#xff1a;多模态数据集成子解法2&#xff1a;实时数据处理与更新子解法3&#xff1a;采用大型语言多模态模型&#xff08;LLMMs&#xff09;进行深度学习分析 慢病预测更多模态 论文&#xff1a;https://arxiv.org/pdf/2406…

Python中爬虫编程的常见问题及解决方案

Python中爬虫编程的常见问题及解决方案 引言&#xff1a; 随着互联网的发展&#xff0c;网络数据的重要性日益突出。爬虫编程成为大数据分析、网络安全等领域中必备的技能。然而&#xff0c;爬虫编程不仅需要良好的编程基础&#xff0c;还需要面对着各种常见的问题。本文将介绍…

【js + ckeditor】插入base64格式的图片

一、需求说明 直接把图片转成base64插入到富文本 二、需求分析 1、富文本图片格式处理位置 在ckeidtor的目录下有个plugins文件夹&#xff0c;在plugins下新建一个文件夹&#xff08;自己命名&#xff0c;如simpleupload&#xff09;&#xff0c;进入simpleupload文件夹&…

用MySQL+node+vue做一个学生信息管理系统(四):制作增加、删除、修改的组件和对应的路由

1.下载依赖&#xff1a; npm install vue-router 在src目录下新建一个文件夹router&#xff0c;在router文件夹下新建一个文件router.js文件,在component目录下新建增加删除和修改的组件&#xff0c;引入router.js当中 此时的init组件为主页面&#xff08;&#xff08;二、三&…

ROS2仿真工具-gazebo

gazebo独立于ROS2&#xff0c;就像插件一样&#xff0c;需要安装。 1.安装 sudo apt install gazebo sudo apt install ros-humble-gazebo-* 2.运行测试demo gazebo /opt/ros/humble/share/gazebo_plugins/worlds/gazebo_ros_diff_drive_demo.world 查看所有话题 ros2 top…

6月份上海二手房卖疯了,暴涨四成,反价房东被抛弃

6月份刚刚结束&#xff0c;北京、上海两大城市的房市成交情况纷纷出炉&#xff0c;从成交量来看上海房市明显比北京火热许多&#xff0c;同时与其他城市类似&#xff0c;消费者偏向于二手房。 6月份上海二手房往前高达2.6万套&#xff0c;环比增加超四成&#xff0c;创下2021年…

Windows下Visual Studio 中配置第一个CUDA工程

今天整NVIDIA 的CUDA 安装和第一个CUDA 代码&#xff0c;顺便添加一个有CUDA工程的空框架。 &#xff08;1&#xff09;首先确认自己的CUDA 已经安装成功 >>cmd 进入命令窗&#xff0c;在窗口输入查看cuda 是否安装成功&#xff0c;能查到CUDA的版本号&#xff0c;表示安…

在CenteOs7上安装mysql8.0(Super详细版)

在CenteOs7上安装mysql8.0 为什么用Mysql8.0&#xff1f;如何下载下载地址需要提前准备下载步骤 服务器上安装如何上传到服务器&#xff1f;通过wget下载到服务器并解压 开始安装非必须安装如果全部安装执行顺序 安装完后&#xff0c;启动mysql使用“systemctl”检测mysqld服务…

基于YOLOv10深度学习的CT扫描图像肾结石智能检测系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标检测

《博主简介》 小伙伴们好&#xff0c;我是阿旭。专注于人工智能、AIGC、python、计算机视觉相关分享研究。 ✌更多学习资源&#xff0c;可关注公-仲-hao:【阿旭算法与机器学习】&#xff0c;共同学习交流~ &#x1f44d;感谢小伙伴们点赞、关注&#xff01; 《------往期经典推…

《企业实战分享 · 常用运维中间件》

&#x1f4e2; 大家好&#xff0c;我是 【战神刘玉栋】&#xff0c;有10多年的研发经验&#xff0c;致力于前后端技术栈的知识沉淀和传播。 &#x1f497; &#x1f33b; 近期刚转战 CSDN&#xff0c;会严格把控文章质量&#xff0c;绝不滥竽充数&#xff0c;如需交流&#xff…

《昇思25天学习打卡营第6天|网络构建》

文章目录 前言&#xff1a;今日所学&#xff1a;1. 定义模型类2. 模型层3. 模型参数 前言&#xff1a; 在第六节中我们学习了网络构建&#xff0c;了解了神经网络模型是由神经网络层和Tensor操作构成&#xff0c;我们使用的mindspore.nn中提供了常见的升级网络层的实现&#x…