可以利用TaskPool接口转换,具体分为以下四个场景:
class Task {
static run(args) {
// Do some independent tasks
}
}
let thread = new Thread(() => {
let result = Task.run(args)
// deal with result
}) ArkTS写法:
import { taskpool } from '@kit.ArkTS';
@Concurrent
function run(args: number) {
// Do some independent tasks
}
let task: taskpool.Task = new taskpool.Task(run, 100); // 100: test number
taskpool.execute(task).then((res) => {
// Return result
}); class Material {
action(args) {
// Do some independent tasks
}
}
let material = new Material()
let thread = new Thread(() => {
let result = material.action(args)
// deal with result
}) ArkTS写法:
import { taskpool } from '@kit.ArkTS';
@Concurrent
function runner(material: Material): void {
return material.action(100); // 100: test number
}
@Sendable
class Material {
action(args: number) {
// Do some independent tasks
}
}
let material = new Material()
taskpool.execute(runner, material).then((ret) => {
// Return result
}) class Task {
run(args) {
// deal with result
runOnUiThread(() => {
UpdateUI(result)
})
}
}
let task = new Task()
let thread = new Thread(() => {
let result = task.run(args)
// Processing results
}) ArkTS写法:
import taskpool from '@ohos.taskpool'
// let result: Object[] | undefined = undefined
@Concurrent
function runner(task:Task) {
task.run()
}
@Sendable
class Task {
run(args?: Object[] | undefined) {
// Do some independent tasks
taskpool.Task.sendData(JsResult)
}
}
let task = new Task()
let run = new taskpool.Task(runner, task)
run.onReceiveData((result?: Function | undefined) => {
UpdateUI(result)
})
taskpool.execute(run).then((ret) => {
// Return result
}) class SdkU3d {
static getInst() {
return SdkMgr.getInst();
}
getPropStr(str: string) {
return xx;
}
}
let thread = new Thread(() => {
// In the game thread
let sdk = SdkU3d.getInst()
let ret = sdk.getPropStr("xx")
}) ArkTS写法:
import { MessageEvents, taskpool, worker } from '@kit.ArkTS';
class SdkU3d {
static getInst(): Object {
return SdkMgr.getInst();
}
getPropStr(str: string) { }
}
let workerInstance = new worker.ThreadWorker("xx/worker.ts");
workerInstance.registerGlobalCallObject("instance_xx", SdkU3d.getInst());
workerInstance.postMessage("start");
// In the game worker thread
const mainPort = worker.workerPort;
mainPort.onmessage = (e: MessageEvents): void => {
let ret = mainPort.callGlobalCallObjectMethod("instance_xx", "getPropStr", 100); // 100: test number
} 参考链接