鸿蒙多线程开发——sendable共享模块
蓝亭书序
2026-11-18 13:14:40 发布0 浏览 0 收藏
1、前 言
鸿蒙多线程开发——线程间数据通信对象03(sendable)
2、共享模块
共享模块是进程内只会加载一次的模块,使用"use shared"这一指令来标记一个模块是否为共享模块。
非共享模块在同一线程内只加载一次,在不同线程间会加载多次,在不同的线程内都会产生新的模块对象。因此可以使用共享模块来实现进程单例。
🔐 约束限制
- "use shared"需要与"use strict"一样写在ArkTS文件顶层,写在import语句之后其他语句之前。共享属性不存在传递性,即非共享模块A不会引入了共享模块B而使A变成共享。
- 共享模块只支持ets文件。
- 共享模块内不允许使用side-effects-import。共享模块可在线程间共享,共享后函数对依赖的非共享模块会在获取模块化变量值的时候懒加载,这种类型的import不涉及导出变量,所以不会被加载。
// 不允许使用side-effects-import
import "./sharedModule"- 共享模块导出的变量必须都是可共享对象。共享模块在并发实例间可共享,因此模块导出的所有对象都必须是可共享的。
- 共享模块中不允许直接导出模块。
// test.ets
export let num = 1;
export let str = 'aaa';
// 共享模块
'use shared'
export * from './test'; // 编译报错,不允许直接导出模块
export {num, str} from './test'; // 正确示例,导出对象合集- 共享模块可以引用共享模块或非共享模块。不限制共享模块的引用和被引用场景。
- napi_load_module、napi_load_module_with_info以及动态加载不支持加载共享模块。
3、使用示例
- 共享模块内导出Sendable对象。
// 共享模块sharedModule.ets
import { ArkTSUtils } from '@kit.ArkTS';
// 声明当前模块为共享模块,只能导出可Sendable数据
"use shared"
// 共享模块,SingletonA全局唯一
@Sendable
class SingletonA {
private count_: number = 0;
lock_: ArkTSUtils.locks.AsyncLock = new ArkTSUtils.locks.AsyncLock()
public async getCount(): Promise<number> {
return this.lock_.lockAsync(() => {
return this.count_;
})
}
public async increaseCount() {
await this.lock_.lockAsync(() => {
this.count_++;
})
}
}
export let singletonA = new SingletonA();2. 在多个线程中操作共享模块导出的对象。
import { ArkTSUtils, taskpool } from '@kit.ArkTS';
import { singletonA } from './sharedModule';
@Sendable
export class A {
private count_: number = 0;
lock_: ArkTSUtils.locks.AsyncLock = new ArkTSUtils.locks.AsyncLock();
public async getCount(): Promise<number> {
return this.lock_.lockAsync(() => {
return this.count_;
})
}
public async increaseCount() {
await this.lock_.lockAsync(() => {
this.count_++;
})
}
}
@Concurrent
async function increaseCount() {
await singletonA.increaseCount();
console.info("SharedModule: count is:" + await singletonA.getCount());
}
@Concurrent
async function printCount() {
console.info("SharedModule: count is:" + await singletonA.getCount());
}
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Row() {
Column() {
Button("MainThread print count")
.onClick(async () => {
await printCount();
})
Button("Taskpool print count")
.onClick(async () => {
await taskpool.execute(printCount);
})
Button("MainThread increase count")
.onClick(async () => {
await increaseCount();
})
Button("Taskpool increase count")
.onClick(async () => {
await taskpool.execute(increaseCount);
})
}
.width('100%')
}
.height('100%')
}
}
©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
HarmonyOS
标签
鸿蒙
线程
sendable
暂无评论数据
加载中...
发布
相关推荐
鸿蒙UI开发——上拉抽屉的更新与事件回调
蓝亭书序
0
0鸿蒙原生 Want 信息传递载体实战指南
在人间耕耘
0
0鸿蒙原生 Preferences C/C++ 数据持久化实战指南
在人间耕耘
0
0鸿蒙UI开发——实现一个上拉抽屉效果
蓝亭书序
0
0鸿蒙UI开发——Builder与LocalBuilder对比
蓝亭书序
0
0
蓝亭书序
我还没有写个人简介......
137
帖子
0
提问
236
粉丝
最新发布
鸿蒙多线程开发——Worker多线程
2026-11-04 14:22:24 发布鸿蒙多线程开发——TaskPool任务池
2026-11-04 14:21:14 发布热门推荐
23445 浏览
23030 浏览
20621 浏览
相关问题




京公网安备:11010502051901号