HarmonyOS taskpool怎么使用

下面是代码两种方式均报错:

第一种:

static initData() {
  console.debug("taskpool GB_T_4814_2013_Utils init start!!!")
  taskpool.execute(()=>{
    for (let i = 0; i < gbt2013Infos.length; i++) {
      const element = gbt2013Infos[i];
      let l=element.l;
      let d=element.d;
      let v=element.v;
      GB_T_4814_2013_Utils.setNumber(GB_T_4814_2013_Utils.getKey(d,l),v);
    }
    console.debug("taskpool GB_T_4814_2013_Utils init finish!!!")
  })
}

报错:

Version:1.0.0
VersionCode:1
PreInstalled:No
Foreground:Yes
Pid:64367
Uid:20020126
Reason:BusinessError
Error name:BusinessError
Error message:The function is not mark as concurrent, taskpool: failed to serialize function.
Error code:
  SourceCode:
    taskpool.execute(() => {
      ^
      Stacktrace:
        SourceMap is not initialized yet
      at initData (entry/src/main/ets/utils/caiji/GB_T_4814_2013_Utils.ets:12:9)
      at init (entry/src/main/ets/utils/caiji/GB_T_4814_2013_Utils.ets:8:9)
      at init (entry/src/main/ets/utils/CaijiUtils.ets:10:9)
      at onCreate (entry/src/main/ets/entryability/EntryAbility.ets:36:9)

第二种:

export class VolumeChartData {
  tops: string[];
  diameters: string[];
  volumes: string[][];
  
  constructor(tops: string[], diameters: string[], volumes: string[][]) {
    this.tops = tops
    this.diameters = diameters
    this.volumes = volumes
  }
}

@Concurrent
function printArgs(): VolumeChartData {
  console.info("taskpool printArgs 执行 start ---------");
  let space = 1
  if (SystemConfig.isDiameterMustEven()) {
    space = 2
  }
  let ts: string[] = new Array
  let ds: string[] = new Array
  let vs: string[][] = new Array
  //乘以10倍的长度
  let startlen_10 = 5
  let endlen_10 = 100
  for (let d = 4; d <= 120; d += space) {
    ds.push(d + "cm")
    let rows: string[] = new Array
    for (let l_10 = startlen_10; l_10 <= endlen_10; l_10++) {
      let v = CaijiUtils.getCaiji(d, BigUtil.div(l_10, 10).toNumber())
      let v_str = CaijiUtils.getShowVolume(v, d)
      rows.push(v_str)

    }
    vs.push(rows)
  }
  for (let l_10 = startlen_10; l_10 <= endlen_10; l_10++) {
    ts.push(BigUtil.div(l_10, 10).toFixed(1) + "m")
  }
  console.debug("getVolumeChartDatas ts = " + JSON.stringify(ts))
  console.debug("getVolumeChartDatas ds = " + JSON.stringify(ds))
  console.debug("getVolumeChartDatas vs = " + JSON.stringify(vs))
  console.info("taskpool printArgs 执行 end---------");
  return new VolumeChartData(ts,ds,vs);
}

let task1: taskpool.Task = new taskpool.Task(printArgs); // 100: test number
taskpool.execute(task1, taskpool.Priority.HIGH)
  .then((value: Object) => {
    console.info("taskpool result1: " + JSON.stringify(value));
  });

报错:

06-26 22:11:47.559   14997-15211  C03F00/ArkCompiler             com.cch.f...istanthm  E     [nodict]Error: You should Call MMKV.initialize() first.
06-26 22:11:47.559   14997-15211  C03F00/ArkCompiler             com.cch.f...istanthm  E     [nodict]    at defaultMMKV (oh_modules/.ohpm/@tencent+mmkv@1.3.5/oh_modules/@tencent/mmkv/src/main/ets/utils/MMKV.ets:87:19)
06-26 22:11:47.559   14997-15211  C03F00/ArkCompiler             com.cch.f...istanthm  E     [nodict]    at getString (entry/src/main/ets/utils/MMKVUtils.ets:8:20)
06-26 22:11:47.559   14997-15211  C03F00/ArkCompiler             com.cch.f...istanthm  E     [nodict]    at getSelectBiaozhun (entry/src/main/ets/utils/SystemConfig.ets:84:18)
06-26 22:11:47.559   14997-15211  C03F00/ArkCompiler             com.cch.f...istanthm  E     [nodict]    at getCaiji (entry/src/main/ets/utils/CaijiUtils.ets:26:24)
06-26 22:11:47.559   14997-15211  C03F00/ArkCompiler             com.cch.f...istanthm  E     [nodict]    at printArgs (entry/src/main/ets/utils/VolumeChartUtils.ets:23:21)
06-26 22:11:47.559   14997-15211  C03F01/NAPI                    com.cch.f...istanthm  E     [nodict][(worker.cpp:383)(PerformTask)] taskpool::PerformTask occur exception
06-26 22:11:53.375   14997-14997  C01304/AbilityManagerService   com.cch.f...istanthm  E     [nodict][ui_ability_impl.cpp(operator():387)]applicationContext or lifecycleCallback is nullptr.
06-26 22:11:53.416   14997-14997  C01305/Appkit                  com.cch.f...istanthm  E     [nodict][application_context.cpp(NotifyApplicationBackground:265)]applicationStateCallback is nullptr

这里报错说是MMKV没有初始化,但是明明已经初始化过了,难道子线程里调用还要初始化一次?

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

子线程中批量写入MMKV是不行的,taskpool里使用的数据如果依赖嵌套不多,可以尝试拆分出来单独一块,避免间接引用到UI相关的。相关线程安全请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/multi-thread-safety-V5

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS taskpool使用crash
1435浏览 • 2回复 待解决
HarmonyOS Promise & taskpool使用问题
312浏览 • 1回复 待解决
HarmonyOS taskpool使用问题咨询
223浏览 • 1回复 待解决
HarmonyOS 使用worker和TaskPool咨询
200浏览 • 1回复 待解决
HarmonyOS Context在taskpool怎么获取
62浏览 • 1回复 待解决
HarmonyOS TaskPool使用首选项报错
474浏览 • 1回复 待解决
如何使用taskpool实现多线程
1175浏览 • 1回复 待解决
taskpool使用 ,谁有好的方案?
763浏览 • 1回复 待解决
TaskPool里面是否可以使用EventHub
1790浏览 • 1回复 待解决
taskpool 使用问题,有人知道吗?
934浏览 • 1回复 待解决
HarmonyOS 关于taskpool的问题
240浏览 • 1回复 待解决