HarmonyOS ArkTS符合线程安全的单例代码示例

HarmonyOS
2024-12-27 14:35:41
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

参考demo如下:

TestData.ets

import { ArkTSUtils } from '@kit.ArkTS';
// 声明当前模块为共享模块,只能导出可Sendable数据
"use shared"
// 共享模块,TestData全局唯一
@Sendable
export class TestData {
  private keyHandle: string = '0';
  private count_: number = 0;
  lock_: ArkTSUtils.locks.AsyncLock = new ArkTSUtils.locks.AsyncLock()
  private static instance: TestData ;
  private constructor() {}
  public static getInstance(): TestData {
    if (TestData.instance == null) {
      TestData.instance = new TestData();
      console.debug('getInstance  new  ')
    }
    return TestData.instance ;
  }
  initData() {
    this.keyHandle = '100';
  }
  async getKeyHandle(): Promise<string> {
    return this.lock_.lockAsync(() => {
      return this.keyHandle;
    })
  }
  async setKeyHandle(keyHandle:string) {
    await this.lock_.lockAsync(() => {
      this.keyHandle = keyHandle;
    })
  }
  public async  getCount():Promise<number>  {
    return this.lock_.lockAsync(() => {
      return this.count_;
    })
  }
  public async increaseCount() {
    await this.lock_.lockAsync(() => {
      this.count_++;
    })
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.

Index.ets

import { TestData } from '../common/TestData';
import { taskpool } from '@kit.ArkTS';
@Concurrent
async function increaseCount() {
  TestData.getInstance().increaseCount();
  console.info("TestData: count is:" + await TestData.getInstance().getCount());
}
@Concurrent
async function printCount() {
  console.info("TestData: count is:" + await TestData.getInstance().getCount());
}
@Concurrent
async function increaseKeyHandle() {
  TestData.getInstance().setKeyHandle('test');
  console.info("TestData: count is:" + await TestData.getInstance().getKeyHandle());
}
@Concurrent
async function printKeyHandle() {
  console.info("TestData: KeyHandle is:" + await TestData.getInstance().getKeyHandle());
}
@Entry
@Component
struct Index {
  @Styles ButtonStyle(){
    .width(180).height(45).margin({ top: 20 })
  }
  build() {
    Row() {
      Column() {
        Button("MainThread print count").ButtonStyle()
          .onClick(async () => {
            await printCount()
          })
        Button("Taskpool print count").ButtonStyle()
          .onClick(async () => {
            await taskpool.execute(printCount);
          })
        Button("MainThread increase count").ButtonStyle()
          .onClick(async () => {
            await increaseCount()
          })
        Button("Taskpool increase count").ButtonStyle()
          .onClick(async () => {
            await taskpool.execute(increaseCount);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
分享
微博
QQ
微信
回复
2024-12-27 17:06:38


相关问题
HarmonyOS 怎么实现线程安全
709浏览 • 1回复 待解决
HarmonyOS TaskPool子线程问题
813浏览 • 1回复 待解决
ArkTS层实例化与介绍
3884浏览 • 1回复 待解决
HarmonyOS 问题
736浏览 • 1回复 待解决
HarmonyOS ArkTS线程安全问题
913浏览 • 1回复 待解决
ArkTs线程方案如何保证线程安全
3099浏览 • 2回复 待解决
HarmonyOS 关于问题
844浏览 • 1回复 待解决
HarmonyOS 对象如何实现
1069浏览 • 1回复 待解决
HarmonyOS 模式不生效
554浏览 • 1回复 待解决
HarmonyOS 如何创建WebView组件
483浏览 • 1回复 待解决
如何获取为undefined
1006浏览 • 1回复 待解决
HarmonyOS 模式拿不到类对象
970浏览 • 1回复 待解决
HarmonyOS静态库是否是
430浏览 • 1回复 待解决
HarmonyOS navigation有模式吗
490浏览 • 1回复 待解决
HarmonyOS 如何构建跨动态库
802浏览 • 1回复 待解决
实现模式下数据存储
1730浏览 • 1回复 待解决
HarmonyOS 路由跳转如何实现效果
563浏览 • 1回复 待解决