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_++;
    })
  }

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%')
  }
}
}
分享
微博
QQ
微信
回复
2024-12-27 17:06:38
相关问题
HarmonyOS 怎么实现线程安全
575浏览 • 1回复 待解决
HarmonyOS TaskPool子线程问题
740浏览 • 1回复 待解决
ArkTS层实例化与介绍
3620浏览 • 1回复 待解决
HarmonyOS 问题
638浏览 • 1回复 待解决
HarmonyOS ArkTS线程安全问题
837浏览 • 1回复 待解决
ArkTs线程方案如何保证线程安全
3001浏览 • 2回复 待解决
HarmonyOS 关于问题
712浏览 • 1回复 待解决
HarmonyOS 对象如何实现
1005浏览 • 1回复 待解决
HarmonyOS 模式不生效
371浏览 • 1回复 待解决
HarmonyOS 如何创建WebView组件
446浏览 • 1回复 待解决
HarmonyOS navigation有模式吗
385浏览 • 1回复 待解决
HarmonyOS静态库是否是
399浏览 • 1回复 待解决
如何获取为undefined
944浏览 • 1回复 待解决
HarmonyOS 如何构建跨动态库
755浏览 • 1回复 待解决
实现模式下数据存储
1635浏览 • 1回复 待解决
HarmonyOS 模式拿不到类对象
852浏览 • 1回复 待解决
HarmonyOS 路由跳转如何实现效果
423浏览 • 1回复 待解决