未验证 提交 b96e0864 编写于 作者: 葛亚芳 提交者: Gitee

update zh-cn/application-dev/reference/apis/js-apis-taskpool.md.

Signed-off-by: N葛亚芳 <geyafang@huawei.com>
上级 b28f393b
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
任务池API以数字形式返回错误码。有关各个错误码的更多信息,请参阅文档[语言基础类库错误码](../errorcodes/errorcode-utils.md) 任务池API以数字形式返回错误码。有关各个错误码的更多信息,请参阅文档[语言基础类库错误码](../errorcodes/errorcode-utils.md)
> **说明:**<br/> > **说明:**
>
> 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块 ## 导入模块
...@@ -17,17 +18,36 @@ ...@@ -17,17 +18,36 @@
import taskpool from '@ohos.taskpool'; import taskpool from '@ohos.taskpool';
``` ```
## Priority ## taskpool.execute
表示所创建任务(Task)的优先级。 execute(func: Function, ...args: unknown[]): Promise\<unknown>
**系统能力:** SystemCapability.Utils.Lang 将待执行的函数放入taskpool内部任务队列等待,等待分发到工作线程执行。当前执行模式不可取消任务。
| 名称 | 值 | 说明 | **系统能力:** SystemCapability.Utils.Lang
| -------- | -------- | -------- |
| HIGH | 0 | 任务为高优先级。 | **参数:**
| MEDIUM | 1 | 任务为中优先级。 |
| LOW | 2 | 任务为低优先级。 | | 参数名 | 类型 | 必填 | 说明 |
| ------ | --------- | ---- | ---------------------------------------------------------------------- |
| func | Function | 是 | 执行的逻辑需要传入函数,支持的函数返回值类型请查[序列化支持类型](#序列化支持类型)。 |
| args | unknown[] | 否 | 执行逻辑的函数所需要的参数,支持的参数类型请查[序列化支持类型](#序列化支持类型)。默认值为undefined。 |
**返回值:**
| 类型 | 说明 |
| ----------------- | ------------------------------------ |
| Promise\<unknown> | execute是异步方法,返回Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
| 错误码ID | 错误信息 |
| -------- | -------------------------------------------- |
| 10200003 | Worker initialization failure. |
| 10200006 | An exception occurred during serialization. |
| 10200014 | The function is not mark as concurrent. |
**示例:** **示例:**
...@@ -38,59 +58,41 @@ function printArgs(args) { ...@@ -38,59 +58,41 @@ function printArgs(args) {
return args; return args;
} }
let task = new taskpool.Task(printArgs, 100); // 100: test number taskpool.execute(printArgs, 100).then((value) => { // 100: test number
let highCount = 0; console.log("taskpool result: " + value);
let mediumCount = 0; });
let lowCount = 0;
let allCount = 100;
for (let i = 0; i < allCount; i++) {
taskpool.execute(task, taskpool.Priority.LOW).then((res: number) => {
lowCount++;
console.log("taskpool lowCount is :" + lowCount);
}).catch((e) => {
console.error("low task error: " + e);
})
taskpool.execute(task, taskpool.Priority.MEDIUM).then((res: number) => {
mediumCount++;
console.log("taskpool mediumCount is :" + mediumCount);
}).catch((e) => {
console.error("medium task error: " + e);
})
taskpool.execute(task, taskpool.Priority.HIGH).then((res: number) => {
highCount++;
console.log("taskpool highCount is :" + highCount);
}).catch((e) => {
console.error("high task error: " + e);
})
}
``` ```
## Task ## taskpool.execute
表示任务。使用以下方法前,需要先构造Task。
### constructor
constructor(func: Function, ...args: unknown[]) execute(task: Task, priority?: Priority): Promise\<unknown>
Task的构造函数 将创建好的任务放入taskpool内部任务队列等待,等待分发到工作线程执行。当前执行模式可尝试调用cancel进行任务取消
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------ | --------- | ---- | -------------------------------------------------------------------- | | -------- | --------------------- | ---- | ---------------------------------------- |
| func | Function | 是 | 任务执行需要传入函数,支持的函数返回值类型请查[序列化支持类型](#序列化支持类型)。 | | task | [Task](#task) | 是 | 需要在任务池中执行的任务。 |
| args | unknown[] | 否 | 任务执行传入函数的参数,支持的参数类型请查[序列化支持类型](#序列化支持类型)。默认值为undefined。 | | priority | [Priority](#priority) | 否 | 等待执行的任务的优先级,该参数默认值为taskpool.Priority.MEDIUM。 |
**返回值:**
| 类型 | 说明 |
| ---------------- | ---------------- |
| Promise\<unknown> | 返回Promise对象。 |
**错误码:** **错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md) 以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | --------------------------------------- | | -------- | ------------------------------------------- |
| 10200014 | The function is not mark as concurrent. | | 10200003 | Worker initialization failure. |
| 10200006 | An exception occurred during serialization. |
| 10200014 | The function is not mark as concurrent. |
**示例:** **示例:**
...@@ -101,43 +103,100 @@ function printArgs(args) { ...@@ -101,43 +103,100 @@ function printArgs(args) {
return args; return args;
} }
let task = new taskpool.Task(printArgs, "this is my first Task"); let task = new taskpool.Task(printArgs, 100); // 100: test number
taskpool.execute(task).then((value) => {
console.log("taskpool result: " + value);
});
``` ```
### isCanceled<sup>10+</sup> ## taskpool.execute<sup>10+</sup>
static isCanceled(): boolean execute(group: TaskGroup, priority?: Priority): Promise<unknown[]>
检查当前正在运行的任务是否已取消 将创建好的任务组放入taskpool内部任务队列等待,等待分发到工作线程执行
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | --------------------------- | ---- | -------------------------------------------------------------- |
| group | [TaskGroup](#taskgroup) | 是 | 需要在任务池中执行的任务组。 |
| priority | [Priority](#priority) | 否 | 等待执行的任务组的优先级,该参数默认值为taskpool.Priority.MEDIUM。 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------- | ------------------------------------ | | ---------------- | ---------------------------------- |
| boolean | 如果当前正在运行的任务被取消返回true,未被取消返回false。| | Promise\<unknown[]> | execute是异步方法,返回Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------- |
| 10200006 | An exception occurred during serialization. |
**示例:** **示例:**
```ts ```ts
@Concurrent @Concurrent
function inspectStatus(arg) { function printArgs(args) {
// do something console.log("printArgs: " + args);
if (taskpool.Task.isCanceled()) { return args;
console.log("task has been canceled.");
// do something
return arg + 1;
}
// do something
return arg;
} }
let taskGroup1 = new taskpool.TaskGroup();
taskGroup1.addTask(printArgs, 10); // 10: test number
taskGroup1.addTask(printArgs, 20); // 20: test number
taskGroup1.addTask(printArgs, 30); // 30: test number
let taskGroup2 = new taskpool.TaskGroup();
let task1 = new taskpool.Task(printArgs, 100); // 100: test number
let task2 = new taskpool.Task(printArgs, 200); // 200: test number
let task3 = new taskpool.Task(printArgs, 300); // 300: test number
taskGroup2.addTask(task1);
taskGroup2.addTask(task2);
taskGroup2.addTask(task3);
taskpool.execute(taskGroup1).then((res) => {
console.info("taskpool execute res is:" + res);
}).catch((e) => {
console.error("taskpool execute error is:" + e);
});
taskpool.execute(taskGroup2).then((res) => {
console.info("taskpool execute res is:" + res);
}).catch((e) => {
console.error("taskpool execute error is:" + e);
});
``` ```
> **说明:**<br/> ## taskpool.cancel
> isCanceled方法需要和taskpool.cancel方法搭配使用,如果不调用cancel方法,isCanceled方法默认返回false。
**示例:** cancel(task: Task): void
取消任务池中的任务。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------------- | ---- | -------------------- |
| task | [Task](#task) | 是 | 需要取消执行的任务。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
| 错误码ID | 错误信息 |
| -------- | -------------------------------------------- |
| 10200015 | The task does not exist when it is canceled. |
| 10200016 | The task is executing when it is canceled. |
从API version10开始,此接口调用时不再涉及上报错误码10200016。
**正在执行的任务取消示例:**
```ts ```ts
@Concurrent @Concurrent
...@@ -147,7 +206,7 @@ function inspectStatus(arg) { ...@@ -147,7 +206,7 @@ function inspectStatus(arg) {
console.log("task has been canceled before 2s sleep."); console.log("task has been canceled before 2s sleep.");
return arg + 2; return arg + 2;
} }
// 延时2s // 2s sleep
let t = Date.now(); let t = Date.now();
while (Date.now() - t < 2000) { while (Date.now() - t < 2000) {
continue; continue;
...@@ -157,110 +216,115 @@ function inspectStatus(arg) { ...@@ -157,110 +216,115 @@ function inspectStatus(arg) {
console.log("task has been canceled after 2s sleep."); console.log("task has been canceled after 2s sleep.");
return arg + 3; return arg + 3;
} }
return arg + 1; return arg + 1;
} }
let task = new taskpool.Task(inspectStatus, 100); // 100: test number let task1 = new taskpool.Task(inspectStatus, 100); // 100: test number
taskpool.execute(task).then((res)=>{ let task2 = new taskpool.Task(inspectStatus, 200); // 200: test number
let task3 = new taskpool.Task(inspectStatus, 300); // 300: test number
let task4 = new taskpool.Task(inspectStatus, 400); // 400: test number
let task5 = new taskpool.Task(inspectStatus, 500); // 500: test number
let task6 = new taskpool.Task(inspectStatus, 600); // 600: test number
taskpool.execute(task1).then((res)=>{
console.log("taskpool test result: " + res); console.log("taskpool test result: " + res);
}).catch((err) => { }).catch((err) => {
console.log("taskpool test occur error: " + err); console.log("taskpool test occur error: " + err);
}); });
// 不调用cancel,isCanceled()默认返回false,task执行的结果为101 let res2 = taskpool.execute(task2);
let res3 = taskpool.execute(task3);
let res4 = taskpool.execute(task4);
let res5 = taskpool.execute(task5);
let res6 = taskpool.execute(task6);
// 1s后取消task
setTimeout(()=>{
taskpool.cancel(task1);}, 1000);
``` ```
### setTransferList<sup>10+</sup> ## taskpool.cancel<sup>10+</sup>
setTransferList(transfer?: ArrayBuffer[]): void
设置任务的传输列表。 cancel(group: TaskGroup): void
> **说明:**<br/> 取消任务池中的任务组。
> 此接口可以设置任务池中ArrayBuffer的transfer列表,transfer列表中的ArrayBuffer对象在传输时不会复制buffer内容到工作线程而是转移buffer控制权至工作线程,传输后当前的ArrayBuffer失效。
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------- | ---- | --------------------------------------------- | | ------- | ----------------------- | ---- | -------------------- |
| transfer | ArrayBuffer[] | 否 | 可传输对象是ArrayBuffer的实例对象,默认为空数组。 | | group | [TaskGroup](#taskgroup) | 是 | 需要取消执行的任务组。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------- |
| 10200018 | The task group does not exist when it is canceled. |
**示例:** **示例:**
```ts ```ts
let buffer = new ArrayBuffer(8);
let view = new Uint8Array(buffer);
let buffer1 = new ArrayBuffer(16);
let view1 = new Uint8Array(buffer1);
console.info("testTransfer view byteLength: " + view.byteLength);
console.info("testTransfer view1 byteLength: " + view1.byteLength);
@Concurrent @Concurrent
function testTransfer(arg1, arg2) { function printArgs(args) {
console.info("testTransfer arg1 byteLength: " + arg1.byteLength); let t = Date.now();
console.info("testTransfer arg2 byteLength: " + arg2.byteLength); while (Date.now() - t < 2000) {
return 100; continue;
}
console.log("printArgs: " + args);
return args;
} }
let task = new taskpool.Task(testTransfer, view, view1);
task.setTransferList([view.buffer, view1.buffer]);
taskpool.execute(task).then((res)=>{
console.info("test result: " + res);
}).catch((e)=>{
console.error("test catch: " + e);
})
console.info("testTransfer view byteLength: " + view.byteLength);
console.info("testTransfer view1 byteLength: " + view1.byteLength);
```
### 属性
**系统能力:** SystemCapability.Utils.Lang let taskGroup1 = new taskpool.TaskGroup();
taskGroup1.addTask(printArgs, 10); // 10: test number
let taskGroup2 = new taskpool.TaskGroup();
taskGroup2.addTask(printArgs, 100); // 100: test number
taskpool.execute(taskGroup1).then((res)=>{
console.info("taskGroup1 res is:" + res)
});
taskpool.execute(taskGroup2).then((res)=>{
console.info("taskGroup2 res is:" + res)
});
setTimeout(()=>{
try {
taskpool.cancel(taskGroup2);
} catch (e) {
console.log("taskGroup.cancel occur error:" + e);
}
}, 1000);
```
| 名称 | 类型 | 可读 | 可写 | 说明 | ## taskpool.getTaskPoolInfo<sup>10+</sup>
| --------- | --------- | ---- | ---- | ------------------------------------------------------------------------- |
| function | Function | 是 | 是 | 创建任务时需要传入的函数,支持的函数返回值类型请查[序列化支持类型](#序列化支持类型)。 |
| arguments | unknown[] | 是 | 是 | 创建任务传入函数所需的参数,支持的参数类型请查[序列化支持类型](#序列化支持类型)。 |
## TaskGroup<sup>10+</sup> getTaskPoolInfo(): TaskPoolInfo
表示任务组。使用以下方法前,需要先构造TaskGroup。
### constructor<sup>10+</sup> 获取任务池内部信息。
constructor() **系统能力:** SystemCapability.Utils.Lang
TaskGroup的构造函数。 **返回值:**
**系统能力:** SystemCapability.Utils.Lang | 类型 | 说明 |
| ----------------------------------- | ------------------ |
| [TaskPoolInfo](#taskpoolinfo10) | 任务池的内部信息。 |
**示例:** **示例:**
```ts ```ts
let taskGroup = new taskpool.TaskGroup(); let taskpoolInfo:TaskPoolInfo = taskpool.getTaskPoolInfo();
``` ```
### addTask<sup>10+</sup> ## Priority
addTask(func: Function, ...args: unknown[]): void
将待执行的函数添加到任务组中。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | --------- | ---- | ---------------------------------------------------------------------- |
| func | Function | 是 | 任务执行需要传入函数,支持的函数返回值类型请查[序列化支持类型](#序列化支持类型)。 |
| args | unknown[] | 否 | 任务执行函数所需要的参数,支持的参数类型请查[序列化支持类型](#序列化支持类型)。默认值为undefined。 |
**错误码:** 表示所创建任务(Task)的优先级。
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md) **系统能力:** SystemCapability.Utils.Lang
| 错误码ID | 错误信息 | | 名称 | 值 | 说明 |
| -------- | --------------------------------------- | | -------- | -------- | -------- |
| 10200014 | The function is not mark as concurrent. | | HIGH | 0 | 任务为高优先级。 |
| MEDIUM | 1 | 任务为中优先级。 |
| LOW | 2 | 任务为低优先级。 |
**示例:** **示例:**
...@@ -271,23 +335,51 @@ function printArgs(args) { ...@@ -271,23 +335,51 @@ function printArgs(args) {
return args; return args;
} }
let taskGroup = new taskpool.TaskGroup(); let task = new taskpool.Task(printArgs, 100); // 100: test number
taskGroup.addTask(printArgs, 100); // 100: test number let highCount = 0;
let mediumCount = 0;
let lowCount = 0;
let allCount = 100;
for (let i = 0; i < allCount; i++) {
taskpool.execute(task, taskpool.Priority.LOW).then((res: number) => {
lowCount++;
console.log("taskpool lowCount is :" + lowCount);
}).catch((e) => {
console.error("low task error: " + e);
})
taskpool.execute(task, taskpool.Priority.MEDIUM).then((res: number) => {
mediumCount++;
console.log("taskpool mediumCount is :" + mediumCount);
}).catch((e) => {
console.error("medium task error: " + e);
})
taskpool.execute(task, taskpool.Priority.HIGH).then((res: number) => {
highCount++;
console.log("taskpool highCount is :" + highCount);
}).catch((e) => {
console.error("high task error: " + e);
})
}
``` ```
### addTask<sup>10+</sup> ## Task
addTask(task: Task): void 表示任务。使用以下方法前,需要先构造Task。
将创建好的任务添加到任务组中。 ### constructor
constructor(func: Function, ...args: unknown[])
Task的构造函数。
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------- | ---- | ---------------------------------------- | | ------ | --------- | ---- | -------------------------------------------------------------------- |
| task | [Task](#task) | 是 | 需要添加到任务组中的任务。 | | func | Function | 是 | 任务执行需要传入函数,支持的函数返回值类型请查[序列化支持类型](#序列化支持类型)。 |
| args | unknown[] | 否 | 任务执行传入函数的参数,支持的参数类型请查[序列化支持类型](#序列化支持类型)。默认值为undefined。 |
**错误码:** **错误码:**
...@@ -306,287 +398,218 @@ function printArgs(args) { ...@@ -306,287 +398,218 @@ function printArgs(args) {
return args; return args;
} }
let taskGroup = new taskpool.TaskGroup(); let task = new taskpool.Task(printArgs, "this is my first Task");
let task = new taskpool.Task(printArgs, 200); // 200: test number
taskGroup.addTask(task);
``` ```
## taskpool.execute ### isCanceled<sup>10+</sup>
execute(func: Function, ...args: unknown[]): Promise\<unknown> static isCanceled(): boolean
将待执行的函数放入taskpool内部任务队列等待,等待分发到工作线程执行。当前执行模式不可取消任务 检查当前正在运行的任务是否已取消
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | --------- | ---- | ---------------------------------------------------------------------- |
| func | Function | 是 | 执行的逻辑需要传入函数,支持的函数返回值类型请查[序列化支持类型](#序列化支持类型)。 |
| args | unknown[] | 否 | 执行逻辑的函数所需要的参数,支持的参数类型请查[序列化支持类型](#序列化支持类型)。默认值为undefined。 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ----------------- | ------------------------------------ | | ------- | ------------------------------------ |
| Promise\<unknown> | execute是异步方法,返回Promise对象。 | | boolean | 如果当前正在运行的任务被取消返回true,未被取消返回false。|
**错误码:** **示例:**
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md) ```ts
@Concurrent
function inspectStatus(arg) {
// do something
if (taskpool.Task.isCanceled()) {
console.log("task has been canceled.");
// do something
return arg + 1;
}
// do something
return arg;
}
```
| 错误码ID | 错误信息 | > **说明:**<br/>
| -------- | -------------------------------------------- | > isCanceled方法需要和taskpool.cancel方法搭配使用,如果不调用cancel方法,isCanceled方法默认返回false。
| 10200003 | Worker initialization failure. |
| 10200006 | An exception occurred during serialization. |
| 10200014 | The function is not mark as concurrent. |
**示例:** **示例:**
```ts ```ts
@Concurrent @Concurrent
function printArgs(args) { function inspectStatus(arg) {
console.log("printArgs: " + args); // 第一时间检查取消并回复
return args; if (taskpool.Task.isCanceled()) {
console.log("task has been canceled before 2s sleep.");
return arg + 2;
}
// 延时2s
let t = Date.now();
while (Date.now() - t < 2000) {
continue;
}
// 第二次检查取消并作出响应
if (taskpool.Task.isCanceled()) {
console.log("task has been canceled after 2s sleep.");
return arg + 3;
}
return arg + 1;
} }
taskpool.execute(printArgs, 100).then((value) => { // 100: test number let task = new taskpool.Task(inspectStatus, 100); // 100: test number
console.log("taskpool result: " + value); taskpool.execute(task).then((res)=>{
console.log("taskpool test result: " + res);
}).catch((err) => {
console.log("taskpool test occur error: " + err);
}); });
// 不调用cancel,isCanceled()默认返回false,task执行的结果为101
``` ```
## taskpool.execute ### setTransferList<sup>10+</sup>
execute(task: Task, priority?: Priority): Promise\<unknown> setTransferList(transfer?: ArrayBuffer[]): void
将创建好的任务放入taskpool内部任务队列等待,等待分发到工作线程执行。当前执行模式可尝试调用cancel进行任务取消。 设置任务的传输列表。
> **说明:**<br/>
> 此接口可以设置任务池中ArrayBuffer的transfer列表,transfer列表中的ArrayBuffer对象在传输时不会复制buffer内容到工作线程而是转移buffer控制权至工作线程,传输后当前的ArrayBuffer失效。
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------- | ---- | ---------------------------------------- | | -------- | ------------- | ---- | --------------------------------------------- |
| task | [Task](#task) | 是 | 需要在任务池中执行的任务。 | | transfer | ArrayBuffer[] | 否 | 可传输对象是ArrayBuffer的实例对象,默认为空数组。 |
| priority | [Priority](#priority) | 否 | 等待执行的任务的优先级,该参数默认值为taskpool.Priority.MEDIUM。 |
**返回值:**
| 类型 | 说明 |
| ---------------- | ---------------- |
| Promise\<unknown> | 返回Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------- |
| 10200003 | Worker initialization failure. |
| 10200006 | An exception occurred during serialization. |
| 10200014 | The function is not mark as concurrent. |
**示例:** **示例:**
```ts ```ts
let buffer = new ArrayBuffer(8);
let view = new Uint8Array(buffer);
let buffer1 = new ArrayBuffer(16);
let view1 = new Uint8Array(buffer1);
console.info("testTransfer view byteLength: " + view.byteLength);
console.info("testTransfer view1 byteLength: " + view1.byteLength);
@Concurrent @Concurrent
function printArgs(args) { function testTransfer(arg1, arg2) {
console.log("printArgs: " + args); console.info("testTransfer arg1 byteLength: " + arg1.byteLength);
return args; console.info("testTransfer arg2 byteLength: " + arg2.byteLength);
return 100;
} }
let task = new taskpool.Task(testTransfer, view, view1);
let task = new taskpool.Task(printArgs, 100); // 100: test number task.setTransferList([view.buffer, view1.buffer]);
taskpool.execute(task).then((value) => { taskpool.execute(task).then((res)=>{
console.log("taskpool result: " + value); console.info("test result: " + res);
}); }).catch((e)=>{
console.error("test catch: " + e);
})
console.info("testTransfer view byteLength: " + view.byteLength);
console.info("testTransfer view1 byteLength: " + view1.byteLength);
``` ```
## taskpool.execute<sup>10+</sup> ### 属性
execute(group: TaskGroup, priority?: Priority): Promise<unknown[]>
将创建好的任务组放入taskpool内部任务队列等待,等待分发到工作线程执行。
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
**参数:** | 名称 | 类型 | 可读 | 可写 | 说明 |
| --------- | --------- | ---- | ---- | ------------------------------------------------------------------------- |
| 参数名 | 类型 | 必填 | 说明 | | function | Function | 是 | 是 | 创建任务时需要传入的函数,支持的函数返回值类型请查[序列化支持类型](#序列化支持类型)。 |
| --------- | --------------------------- | ---- | -------------------------------------------------------------- | | arguments | unknown[] | 是 | 是 | 创建任务传入函数所需的参数,支持的参数类型请查[序列化支持类型](#序列化支持类型)。 |
| group | [TaskGroup](#taskgroup) | 是 | 需要在任务池中执行的任务组。 |
| priority | [Priority](#priority) | 否 | 等待执行的任务组的优先级,该参数默认值为taskpool.Priority.MEDIUM。 |
**返回值:** ## TaskGroup<sup>10+</sup>
表示任务组。使用以下方法前,需要先构造TaskGroup。
| 类型 | 说明 | ### constructor<sup>10+</sup>
| ---------------- | ---------------------------------- |
| Promise\<unknown[]> | execute是异步方法,返回Promise对象。 |
**错误码:** constructor()
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md) TaskGroup的构造函数
| 错误码ID | 错误信息 | **系统能力:** SystemCapability.Utils.Lang
| -------- | ------------------------------------------- |
| 10200006 | An exception occurred during serialization. |
**示例:** **示例:**
```ts ```ts
@Concurrent let taskGroup = new taskpool.TaskGroup();
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
let taskGroup1 = new taskpool.TaskGroup();
taskGroup1.addTask(printArgs, 10); // 10: test number
taskGroup1.addTask(printArgs, 20); // 20: test number
taskGroup1.addTask(printArgs, 30); // 30: test number
let taskGroup2 = new taskpool.TaskGroup();
let task1 = new taskpool.Task(printArgs, 100); // 100: test number
let task2 = new taskpool.Task(printArgs, 200); // 200: test number
let task3 = new taskpool.Task(printArgs, 300); // 300: test number
taskGroup2.addTask(task1);
taskGroup2.addTask(task2);
taskGroup2.addTask(task3);
taskpool.execute(taskGroup1).then((res) => {
console.info("taskpool execute res is:" + res);
}).catch((e) => {
console.error("taskpool execute error is:" + e);
});
taskpool.execute(taskGroup2).then((res) => {
console.info("taskpool execute res is:" + res);
}).catch((e) => {
console.error("taskpool execute error is:" + e);
});
``` ```
## taskpool.cancel ### addTask<sup>10+</sup>
cancel(task: Task): void addTask(func: Function, ...args: unknown[]): void
取消任务池中的任务 将待执行的函数添加到任务组中
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------ | ------------- | ---- | -------------------- | | ------ | --------- | ---- | ---------------------------------------------------------------------- |
| task | [Task](#task) | 是 | 需要取消执行的任务。 | | func | Function | 是 | 任务执行需要传入函数,支持的函数返回值类型请查[序列化支持类型](#序列化支持类型)。 |
| args | unknown[] | 否 | 任务执行函数所需要的参数,支持的参数类型请查[序列化支持类型](#序列化支持类型)。默认值为undefined。 |
**错误码:** **错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md) 以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | -------------------------------------------- | | -------- | --------------------------------------- |
| 10200015 | The task does not exist when it is canceled. | | 10200014 | The function is not mark as concurrent. |
| 10200016 | The task is executing when it is canceled. |
从API version10开始,此接口调用时不再涉及上报错误码10200016。
**正在执行的任务取消示例:** **示例:**
```ts ```ts
@Concurrent @Concurrent
function inspectStatus(arg) { function printArgs(args) {
// 第一时间检查取消并回复 console.log("printArgs: " + args);
if (taskpool.Task.isCanceled()) { return args;
console.log("task has been canceled before 2s sleep.");
return arg + 2;
}
// 2s sleep
let t = Date.now();
while (Date.now() - t < 2000) {
continue;
}
// 第二次检查取消并作出响应
if (taskpool.Task.isCanceled()) {
console.log("task has been canceled after 2s sleep.");
return arg + 3;
}
return arg + 1;
} }
let task1 = new taskpool.Task(inspectStatus, 100); // 100: test number let taskGroup = new taskpool.TaskGroup();
let task2 = new taskpool.Task(inspectStatus, 200); // 200: test number taskGroup.addTask(printArgs, 100); // 100: test number
let task3 = new taskpool.Task(inspectStatus, 300); // 300: test number
let task4 = new taskpool.Task(inspectStatus, 400); // 400: test number
let task5 = new taskpool.Task(inspectStatus, 500); // 500: test number
let task6 = new taskpool.Task(inspectStatus, 600); // 600: test number
taskpool.execute(task1).then((res)=>{
console.log("taskpool test result: " + res);
}).catch((err) => {
console.log("taskpool test occur error: " + err);
});
let res2 = taskpool.execute(task2);
let res3 = taskpool.execute(task3);
let res4 = taskpool.execute(task4);
let res5 = taskpool.execute(task5);
let res6 = taskpool.execute(task6);
// 1s后取消task
setTimeout(()=>{
taskpool.cancel(task1);}, 1000);
``` ```
## taskpool.cancel<sup>10+</sup> ### addTask<sup>10+</sup>
cancel(group: TaskGroup): void addTask(task: Task): void
取消任务池中的任务组 将创建好的任务添加到任务组中
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ----------------------- | ---- | -------------------- | | -------- | --------------------- | ---- | ---------------------------------------- |
| group | [TaskGroup](#taskgroup) | 是 | 需要取消执行的任务组。 | | task | [Task](#task) | 是 | 需要添加到任务组中的任务。 |
**错误码:** **错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md) 以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------- | | -------- | --------------------------------------- |
| 10200018 | The task group does not exist when it is canceled. | | 10200014 | The function is not mark as concurrent. |
**示例:** **示例:**
```ts ```ts
@Concurrent @Concurrent
function printArgs(args) { function printArgs(args) {
let t = Date.now();
while (Date.now() - t < 2000) {
continue;
}
console.log("printArgs: " + args); console.log("printArgs: " + args);
return args; return args;
} }
let taskGroup1 = new taskpool.TaskGroup(); let taskGroup = new taskpool.TaskGroup();
taskGroup1.addTask(printArgs, 10); // 10: test number let task = new taskpool.Task(printArgs, 200); // 200: test number
let taskGroup2 = new taskpool.TaskGroup(); taskGroup.addTask(task);
taskGroup2.addTask(printArgs, 100); // 100: test number
taskpool.execute(taskGroup1).then((res)=>{
console.info("taskGroup1 res is:" + res)
});
taskpool.execute(taskGroup2).then((res)=>{
console.info("taskGroup2 res is:" + res)
});
setTimeout(()=>{
try {
taskpool.cancel(taskGroup2);
} catch (e) {
console.log("taskGroup.cancel occur error:" + e);
}
}, 1000);
``` ```
## State<sup>10+</sup> ## State<sup>10+</sup>
表示任务(Task)状态的枚举。 表示任务(Task)状态的枚举。
...@@ -647,25 +670,6 @@ setTimeout(()=>{ ...@@ -647,25 +670,6 @@ setTimeout(()=>{
| threadInfos | [ThreadInfo[]](#threadinfo10) | 是 | 否 | 工作线程的内部信息。 | | threadInfos | [ThreadInfo[]](#threadinfo10) | 是 | 否 | 工作线程的内部信息。 |
| taskInfos | [TaskInfo[]](#taskinfo10) | 是 | 否 | 任务的内部信息。 | | taskInfos | [TaskInfo[]](#taskinfo10) | 是 | 否 | 任务的内部信息。 |
## taskpool.getTaskPoolInfo<sup>10+</sup>
getTaskPoolInfo(): TaskPoolInfo
获取任务池内部信息。
**系统能力:** SystemCapability.Utils.Lang
**返回值:**
| 类型 | 说明 |
| ----------------------------------- | ------------------ |
| [TaskPoolInfo](#taskpoolinfo10) | 任务池的内部信息。 |
**示例:**
```ts
let taskpoolInfo:TaskPoolInfo = taskpool.getTaskPoolInfo();
```
## 其他说明 ## 其他说明
...@@ -710,6 +714,7 @@ taskpoolExecute(); ...@@ -710,6 +714,7 @@ taskpoolExecute();
// b.ets // b.ets
export let c = 2000; export let c = 2000;
``` ```
```ts ```ts
// 引用import变量 // 引用import变量
// a.ets(与b.ets位于同一目录中) // a.ets(与b.ets位于同一目录中)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册