步骤说明:
使用taskpool.execute()或taskpool.executePeriodically()启动任务时,会返回一个Task实例,其taskId属性为唯一标识。
取消后任务不会立即终止。
取消任务后,任务还会在子线程中继续运行,而且任务存在阻塞的话会一直占用子线程,例如以下示例代码,为了防止while造成死循环而导致子线程占用,需在执行体内部主动检查isCanceled()状态,主动跳出任务,以实现优雅退出,释放子线程资源。
@Concurrent
function printArgs(args: number): number {
while (true) {
if (taskpool.Task.isCanceled()) {
hilog.info(0x0000, 'testTag', 'task has been canceled after 2s sleep.');
return args + 1;
}
continue;
}
}
@Concurrent
function cancelFunction(taskId: number) {
try {
taskpool.cancel(taskId);
} catch (e) {
hilog.error(0x0000, 'testTag', `taskpool: cancel error code: ${e.code}, info: ${e.message}`);
}
}
function concurrentFuncAfterEighteen() {
let task = new taskpool.Task(printArgs, 100);
taskpool.execute(task).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', 'taskpool catch err: ' + err.message);
});
setTimeout(() => {
hilog.info(0x0000, 'testTag', 'cancel task after Api18');
let cancelTask = new taskpool.Task(cancelFunction, task.taskId);
taskpool.execute(cancelTask).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', 'taskpool catch err: ' + err.message);
});
}, 1000);
} 步骤说明:
取消后需在任务体中检查状态。
与高版本一致,需在任务函数内部定期调用isCanceled()检查是否被取消。
function concurrentFuncBeforeEighteen() {
let task: taskpool.Task = new taskpool.Task(printArgs, 100); // 100: test number
taskpool.execute(task).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', 'taskpool catch err: ' + err.message);
});
setTimeout(() => {
try {
hilog.info(0x0000, 'testTag', 'cancel task before Api18');
taskpool.cancel(task);
} catch (e) {
hilog.error(0x0000, 'testTag', `taskpool: cancel error code: ${e.code}, info: ${e.message}`);
}
}, 1000);
} 完整示例参考如下:
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { taskpool } from '@kit.ArkTS';
@Concurrent
function printArgs(args: number): number {
while (true) {
if (taskpool.Task.isCanceled()) {
hilog.info(0x0000, 'testTag', 'task has been canceled after 2s sleep.');
return args + 1;
}
continue;
}
}
@Concurrent
function cancelFunction(taskId: number) {
try {
taskpool.cancel(taskId);
} catch (e) {
hilog.error(0x0000, 'testTag', `taskpool: cancel error code: ${e.code}, info: ${e.message}`);
}
}
function concurrentFuncAfterEighteen() {
let task = new taskpool.Task(printArgs, 100);
taskpool.execute(task).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', 'taskpool catch err: ' + err.message);
});
setTimeout(() => {
hilog.info(0x0000, 'testTag', 'cancel task after Api18');
let cancelTask = new taskpool.Task(cancelFunction, task.taskId);
taskpool.execute(cancelTask).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', 'taskpool catch err: ' + err.message);
});
}, 1000);
}
function concurrentFuncBeforeEighteen() {
let task: taskpool.Task = new taskpool.Task(printArgs, 100); // 100: test number
taskpool.execute(task).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', 'taskpool catch err: ' + err.message);
});
setTimeout(() => {
try {
hilog.info(0x0000, 'testTag', 'cancel task before Api18');
taskpool.cancel(task);
} catch (e) {
hilog.error(0x0000, 'testTag', `taskpool: cancel error code: ${e.code}, info: ${e.message}`);
}
}, 1000);
}
@Entry
@Component
struct cancelTaskPool {
build() {
Column() {
Button('AfterApi18')
.onClick(() => {
concurrentFuncAfterEighteen();
}).margin({ bottom: 10 });
Button('BeforeApi18')
.onClick(() => {
concurrentFuncBeforeEighteen();
}).margin({ bottom: 10 });
}.width('100%')
.height('100%')
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center);
}
}