版本新增taskpool的相关功能验证

版本新增taskpool的相关功能验证

HarmonyOS
2024-05-23 21:32:39
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
hujianwu

版本新增taskpool相关功能验证demo

sendData 在任务执行过程中向宿主线程发送消息并触发回调

设置任务名称

Sendable注解类序列化并setCloneList

任务依赖(执行顺序)

使用的核心API

import taskpool from '@ohos.taskpool';

核心代码解释

sendData 在任务执行过程中向宿主线程发送消息并触发回调

testFunc() 
 
@Concurrent 
function ConcurrentFunc(num: number): number { 
let res: number = num * 10; 
taskpool.Task.sendData(res); 
return num; 
} 
​ 
function pringLog(data: number): void { 
console.info("taskpool: data is: " + data); 
} 
​ 
async function testFunc(): Promise<void> { 
let task: taskpool.Task = new taskpool.Task(ConcurrentFunc, 1); 
task.onReceiveData(pringLog); 
let ret: number = await taskpool.execute(task) as number; 
console.info("taskpool: result is: " + ret); 
}

设置任务名称

let taskName: string = "taskName"; 
let task: taskpool.Task = new taskpool.Task(taskName, printArgs, "this is my first Task"); 
let name: string = task.name; 
console.log(name+"xxxxx"); 
​ 
@Concurrent 
function printArgs(args: string): string { 
console.info("printArgs: " + args); 
return args; 
}

Sendable注解类序列化并setCloneList

// task1访问调用BaseClass.str1/BaseClass.SetNum/BaseClass.GetNum/BaseClass.isDone1/BaseClass.publicFunc 
let baseInstance1: BaseClass = new BaseClass(); 
let array1 = new Array<BaseClass>(); 
array1.push(baseInstance1); 
let task1 = new taskpool.Task(testFunc, array1, 10); 
task1.setCloneList(array1); 
taskpool.execute(task1).then((res: Object) => { 
console.info("sendable: task1 res is: " + res); 
}).catch((e:BusinessError) => { 
console.error(`sendable: task1 execute Code is ${e.code}, message is ${e.message}`); 
}) 
​ 
// task2调用DeriveClass.printName 
let deriveInstance: DeriveClass = new DeriveClass(); 
let array2 = new Array<DeriveClass>(); 
array2.push(deriveInstance); 
let task2 = new taskpool.Task(printLog, array2); 
task2.setCloneList(array2); 
taskpool.execute(task2).then(() => { 
console.info("sendable: task2 execute success"); 
}).catch((e:BusinessError) => { 
console.error(`sendable: task2 execute Code is ${e.code}, message is ${e.message}`); 
}) 
​ 
​ 
@Concurrent 
function ConcurrentFunc(num: number): number { 
let res: number = num * 10; 
taskpool.Task.sendData(res); 
return num; 
} 
​ 
function pringLog(data: number): void { 
console.info("taskpool: data is: " + data); 
} 
​ 
async function testFunc(): Promise<void> { 
let task: taskpool.Task = new taskpool.Task(ConcurrentFunc, 1); 
task.onReceiveData(pringLog); 
let ret: number = await taskpool.execute(task) as number; 
console.info("taskpool: result is: " + ret); 
} 
​ 
​ 
​ 
@Sendable 
export class BaseClass { 
private str: string = "sendable: BaseClass"; 
static num :number = 10; 
str1: string = "sendable: this is BaseClass's string"; 
num1: number = 5; 
isDone1: boolean = false; 
​ 
private fibonacciRecursive(n: number): number { 
  if (n <= 1) { 
    return n; 
  } else { 
    return this.fibonacciRecursive(n - 1) + this.fibonacciRecursive(n - 2); 
  } 
} 
​ 
private privateFunc(num: number): number{ 
  let res: number = this.fibonacciRecursive(num); 
  console.info("sendable: BaseClass privateFunc res is: " + res); 
  return res; 
} 
​ 
publicFunc(num: number): number { 
  return this.privateFunc(num); 
} 
​ 
get GetNum(): number { 
  return this.num1; 
} 
set SetNum(num: number) { 
  this.num1 = num; 
} 
​ 
constructor(){ 
  console.info(this.str); 
  this.isDone1 = true; 
} 
} 
​ 
@Sendable 
export class DeriveClass extends BaseClass { 
name: string = "sendable: this is DeriveClass"; 
printName() { 
  console.info(this.name); 
} 
constructor() { 
  super(); 
} 
}

任务依赖(执行顺序)

let task1:taskpool.Task = new taskpool.Task(delay, 100); 
let task2:taskpool.Task = new taskpool.Task(delay, 200); 
let task3:taskpool.Task = new taskpool.Task(delay, 200); 
​ 
console.info("dependency: add dependency start"); 
task1.addDependency(task3); 
task3.addDependency(task2); 
console.info("dependency: add dependency end"); 
​ 
console.info("dependency: start execute second") 
taskpool.execute(task1).then(() => { 
console.info("dependency: second task1 success"); 
}) 
taskpool.execute(task2).then(() => { 
console.info("dependency: second task2 success"); 
}) 
taskpool.execute(task3).then(() => { 
console.info("dependency: second task3 success"); 
}) 
​ 
​ 
@Concurrent 
function delay(args: number): number { 
let t: number = Date.now(); 
while ((Date.now() - t) < 1000) { 
  continue; 
} 
return args; 
}

实现效果

适配版本

DevEco Studio Version: 4.1.3.500

SDK:HarmoneyOS NEXT Developer Preview1

分享
微博
QQ
微信
回复
2024-05-24 16:18:28
相关问题
验证手机号是否已被注册/绑定功能
2441浏览 • 1回复 待解决
webview如何调用webRTC相关功能
580浏览 • 1回复 待解决
TaskPool和Worker异同点
772浏览 • 1回复 待解决
TaskPool与Worker区别
724浏览 • 1回复 待解决
taskPool @Concurrent报红
1238浏览 • 1回复 待解决
前端验证码配合后端实现思路?
2175浏览 • 1回复 待解决
taskpool管理策略是什么
359浏览 • 1回复 待解决
taskpool使用 ,谁有好方案?
226浏览 • 1回复 待解决
woeker及taskpool简单实现
345浏览 • 1回复 待解决
鸿蒙JS API7新增接口在哪里
4237浏览 • 1回复 待解决
PolarDB MySQL如何验证读写分离?
1206浏览 • 1回复 待解决
taskPool参数支持Function类型吗
601浏览 • 1回复 待解决
密码或验证码登录页面完整代码
464浏览 • 1回复 待解决