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

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

HarmonyOS
2024-05-23 21:32:39
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
牙膏只用咸的

版本新增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
相关问题
HarmonyOS 如何实现滑动验证功能
531浏览 • 1回复 待解决
验证手机号是否已被注册/绑定功能
3889浏览 • 1回复 待解决
在HarmonyOS平台上测试验证so文件功能
343浏览 • 1回复 待解决
华为账号实时验证/二次放号相关咨询
1739浏览 • 1回复 待解决
webview如何调用webRTC相关功能
1348浏览 • 1回复 待解决
如何新增 Run/Debug Configurations
380浏览 • 1回复 待解决
HarmonyOS关于新增ODID含义
690浏览 • 1回复 待解决
HarmonyOS 通讯录相关功能布局实现
343浏览 • 1回复 待解决
实现验证码登录之前滑动验证实现
156浏览 • 1回复 待解决
证书管理无法新增证书
414浏览 • 1回复 待解决