二十九、【鸿蒙 NEXT】异步锁实现并发控制

【前言】

在开发鸿蒙代码时,经常会用到async的异步函数,假设函数名称是A,如果A函数里面有个异步操作很耗时的话,我们可能会用到缓存,将耗时操作的结果保存到缓存中,如果下次再有调用A函数的话,直接返回缓存中的值。但是有时候,当缓存还没有值的时候,A函数在短时间内被调用多次,导致耗时函数会被执行多次,可能导致阻塞主线程的问题。下面首先复现下这种场景,并给出一个解决方案。

1、模拟一个耗时的异步函数,短时间内被调用多次

代码如下,其中getValue函数表示一个耗时的异步函数,我们用一个for循环执行多次,来模拟短时间被调用多次,我们可以看结果,每次调用,缓存值都还没有生成,这就导致,计算缓存值的函数会被多次执行。

javascript 复制代码
@Entry
@Component
struct LockPage {
  async aboutToAppear() {
    let util = new LockUtil()
    for (let index = 0; index < 4; index++) {
      util.getValue().then((value) => {
        console.log(`[lock] getValue is ${value}`)
      })
    }
  }
  build() {
    RelativeContainer() {
    }
  }
}

export class LockUtil {
  private cache = ''

  public async getValue():Promise<string> {
    if (this.cache) {
      return this.cache
    }
    console.log('[lock] no cache')
    // 模拟耗时操作
    await new Promise<void>(resolve => setTimeout(resolve, 3000));
    this.cache = '6666'
    return this.cache
  }
}

执行结果:

2、使用异步锁ArkTSUtils.locks.AsyncLock控制异步并发

我们这里引入系统的异步锁,来控制异步函数的并发,使得计算缓存的耗时操作只执行一次,代码如下,看日志打印,耗时函数只执行一次,其余都是从缓存中获取

javascript 复制代码
public async getValueLock():Promise<string> {
    if (this.cache) {
      return this.cache
    }
    let lock:ArkTSUtils.locks.AsyncLock = ArkTSUtils.locks.AsyncLock.request('lock')
    let result = await lock.lockAsync(async () => {
      if (this.cache) {
        return this.cache
      }
      console.log('[lock] no cache')
      // 模拟耗时操作
      await new Promise<void>(resolve => setTimeout(resolve, 3000));
      this.cache = '6666'
      return this.cache
    })

    return result
  }

完整代码示例如下:

javascript 复制代码
@Entry
@Component
struct LockPage {
  async aboutToAppear() {
    let util = new LockUtil()
    for (let index = 0; index < 4; index++) {
      util.getValueLock().then((value) => {
        console.log(`[lock] getValue is ${value}`)
      })
    }
  }
  build() {
    RelativeContainer() {
    }
  }
}


import { ArkTSUtils } from "@kit.ArkTS";

export class LockUtil {
  private cache = ''

  public async getValue():Promise<string> {
    if (this.cache) {
      return this.cache
    }
    console.log('[lock] no cache')
    // 模拟耗时操作
    await new Promise<void>(resolve => setTimeout(resolve, 3000));
    this.cache = '6666'
    return this.cache
  }

  public async getValueLock():Promise<string> {
    if (this.cache) {
      return this.cache
    }
    let lock:ArkTSUtils.locks.AsyncLock = ArkTSUtils.locks.AsyncLock.request('lock')
    let result = await lock.lockAsync(async () => {
      if (this.cache) {
        return this.cache
      }
      console.log('[lock] no cache')
      // 模拟耗时操作
      await new Promise<void>(resolve => setTimeout(resolve, 3000));
      this.cache = '6666'
      return this.cache
    })

    return result
  }
}
相关推荐
zhangfeng11333 小时前
ATK(华为算子测试平台)详细介绍 CANN(Compute Architecture for Neural Networks,神经网络计算架构
人工智能·华为·ai编程·npu·cann
贾伟康3 小时前
【HarmonyOS 7新能力|043】可变字体工程封装:把接入逻辑放进可维护的分层结构
harmonyos·arkts·arkui·ui设计·可变字体
HarmonyOS_SDK3 小时前
HarmonyOS智慧多窗,让应用在任意窗口都“恰到好处”
harmonyos
zhangfeng11334 小时前
《从“人工适配“到“智能生成“:KernelSwift 跨国产芯片算子迁移全栈方案解读》 —— 强调范式跃迁和跨硬件属性,适合偏架构分析的写法
人工智能·算法·华为·ai编程·npu
传奇开心果编程6 小时前
【ArkUI 练中学】第14课:应用性能优化实战
学习·ui·华为·harmonyos
用户593096009786 小时前
Flutter 鸿蒙化实战:flutter_tts 适配 OpenHarmony,文本转语音
harmonyos
威哥爱编程8 小时前
HarmonyOS 互动卡片实战:摇一摇触发静态转动态,前景元素出框
华为·harmonyos·arkts
威哥爱编程9 小时前
HarmonyOS 7 闪控窗实战:标准悬浮窗、侧边栏暂存与闪控球一键切换
华为·harmonyos·arkts
威哥爱编程9 小时前
HarmonyOS 7 图像超分实战:端侧 4 倍高清放大,数据不出设备
华为·harmonyos·arkts
威哥爱编程9 小时前
HarmonyOS 6.1 智能窥屏防护实战:实时感知窥屏风险,敏感页自动防护
华为·harmonyos·arkts