提交 d58d713e 编写于 作者: G Gloria

Update docs against 21191+21724+21599+21534

Signed-off-by: wusongqing<wusongqing@huawei.com>
上级 bae87b24
...@@ -16,6 +16,302 @@ The **TaskPool** APIs return error codes in numeric format. For details about th ...@@ -16,6 +16,302 @@ The **TaskPool** APIs return error codes in numeric format. For details about th
```ts ```ts
import taskpool from '@ohos.taskpool'; import taskpool from '@ohos.taskpool';
``` ```
## taskpool.execute
execute(func: Function, ...args: unknown[]): Promise\<unknown>
Places the function to be executed in the internal task queue of the task pool. The function will be distributed to the worker thread for execution. The function to be executed in this mode cannot be canceled.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | --------- | ---- | ---------------------------------------------------------------------- |
| func | Function | Yes | Function to be executed. For details about the supported return value types of the function, see [Sequenceable Data Types](#sequenceable-data-types). |
| args | unknown[] | No | Arguments of the function. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types). The default value is **undefined**.|
**Return value**
| Type | Description |
| ----------------- | ------------------------------------ |
| Promise\<unknown> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 10200003 | Worker initialization failure. |
| 10200006 | An exception occurred during serialization. |
| 10200014 | The function is not mark as concurrent. |
**Example**
```ts
@Concurrent
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
taskpool.execute(printArgs, 100).then((value) => { // 100: test number
console.log("taskpool result: " + value);
});
```
## taskpool.execute
execute(task: Task, priority?: Priority): Promise\<unknown>
Places a task in the internal task queue of the task pool. The task will be distributed to the worker thread for execution. The task to be executed in this mode can be canceled.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------- | ---- | ---------------------------------------- |
| task | [Task](#task) | Yes | Task to be executed. |
| priority | [Priority](#priority) | No | Priority of the task. The default value is **taskpool.Priority.MEDIUM**.|
**Return value**
| Type | Description |
| ---------------- | ---------------- |
| Promise\<unknown> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------------------- |
| 10200003 | Worker initialization failure. |
| 10200006 | An exception occurred during serialization. |
| 10200014 | The function is not mark as concurrent. |
**Example**
```ts
@Concurrent
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
let task = new taskpool.Task(printArgs, 100); // 100: test number
taskpool.execute(task).then((value) => {
console.log("taskpool result: " + value);
});
```
## taskpool.execute<sup>10+</sup>
execute(group: TaskGroup, priority?: Priority): Promise<unknown[]>
Places a task group in the internal task queue of the task pool. The task group will be distributed to the worker thread for execution.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| --------- | --------------------------- | ---- | -------------------------------------------------------------- |
| group | [TaskGroup](#taskgroup) | Yes | Task group to be executed. |
| priority | [Priority](#priority) | No | Priority of the task group. The default value is **taskpool.Priority.MEDIUM**.|
**Return value**
| Type | Description |
| ---------------- | ---------------------------------- |
| Promise\<unknown[]> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------------------- |
| 10200006 | An exception occurred during serialization. |
**Example**
```ts
@Concurrent
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
cancel(task: Task): void
Cancels a task in the task pool.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------------- | ---- | -------------------- |
| task | [Task](#task) | Yes | Task to cancel.|
**Error codes**
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 10200015 | The task does not exist when it is canceled. |
| 10200016 | The task is executing when it is canceled. |
Since API version 10, error code 10200016 is not reported when this API is called.
**Example of canceling an ongoing task**
```ts
@Concurrent
function inspectStatus(arg) {
// Check the cancellation status and return the result.
if (taskpool.Task.isCanceled()) {
console.log("task has been canceled before 2s sleep.");
return arg + 2;
}
// 2s sleep
let t = Date.now();
while (Date.now() - t < 2000) {
continue;
}
// Check the cancellation status again and return the result.
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 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);
}).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);
// Cancel the task 1s later.
setTimeout(()=>{
taskpool.cancel(task1);}, 1000);
```
## taskpool.cancel<sup>10+</sup>
cancel(group: TaskGroup): void
Cancels a task group in the task pool.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| ------- | ----------------------- | ---- | -------------------- |
| group | [TaskGroup](#taskgroup) | Yes | Task group to cancel.|
**Error codes**
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------------------------------- |
| 10200018 | The task group does not exist when it is canceled. |
**Example**
```ts
@Concurrent
function printArgs(args) {
let t = Date.now();
while (Date.now() - t < 2000) {
continue;
}
console.log("printArgs: " + args);
return args;
}
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>
getTaskPoolInfo(): TaskPoolInfo
Obtains the internal information about this task pool.
**System capability**: SystemCapability.Utils.Lang
**Return value**
| Type | Description |
| ----------------------------------- | ------------------ |
| [TaskPoolInfo](#taskpoolinfo10) | Internal information about the task pool. |
**Example**
```ts
let taskpoolInfo = taskpool.getTaskPoolInfo();
```
## Priority ## Priority
...@@ -67,7 +363,7 @@ for (let i = 0; i < allCount; i++) { ...@@ -67,7 +363,7 @@ for (let i = 0; i < allCount; i++) {
## Task ## Task
Implements a task. Before using any of the following APIs, you must create a **Task** instance. Implements a task. Before calling any APIs in **Task**, you must use [constructor](#constructor) to create a **Task** instance.
### constructor ### constructor
...@@ -108,7 +404,7 @@ let task = new taskpool.Task(printArgs, "this is my first Task"); ...@@ -108,7 +404,7 @@ let task = new taskpool.Task(printArgs, "this is my first Task");
static isCanceled(): boolean static isCanceled(): boolean
Checks whether the running task is canceled. Checks whether the running task is canceled. Before using this API, you must create a **Task** instance.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
...@@ -174,7 +470,7 @@ taskpool.execute(task).then((res)=>{ ...@@ -174,7 +470,7 @@ taskpool.execute(task).then((res)=>{
setTransferList(transfer?: ArrayBuffer[]): void setTransferList(transfer?: ArrayBuffer[]): void
Sets the task transfer list. Sets the task transfer list. Before using this API, you must create a **Task** instance.
> **NOTE** > **NOTE**
> >
...@@ -212,183 +508,58 @@ taskpool.execute(task).then((res)=>{ ...@@ -212,183 +508,58 @@ taskpool.execute(task).then((res)=>{
console.error("test catch: " + e); console.error("test catch: " + e);
}) })
console.info("testTransfer view byteLength: " + view.byteLength); console.info("testTransfer view byteLength: " + view.byteLength);
console.info("testTransfer view1 byteLength: " + view1.byteLength); console.info("testTransfer view1 byteLength: " + view1.byteLength);
``` ```
### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name | Type | Readable| Writable| Description |
| --------- | --------- | ---- | ---- | ------------------------------------------------------------------------- |
| function | Function | Yes | Yes | Function to be passed in during task creation. For details about the supported return value types of the function, see [Sequenceable Data Types](#sequenceable-data-types). |
| arguments | unknown[] | Yes | Yes | Arguments of the function. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
## TaskGroup<sup>10+</sup>
Implements a task group. Before using any of the following APIs, you must create a **TaskGroup** instance.
### constructor<sup>10+</sup>
constructor()
Constructor used to create a **TaskGroup** instance.
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
let taskGroup = new taskpool.TaskGroup();
```
### addTask<sup>10+</sup>
addTask(func: Function, ...args: unknown[]): void
Adds the function to be executed to this task group.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | --------- | ---- | ---------------------------------------------------------------------- |
| func | Function | Yes | Function to be passed in for task execution. For details about the supported return value types of the function, see [Sequenceable Data Types](#sequenceable-data-types). |
| args | unknown[] | No | Arguments of the function. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types). The default value is **undefined**.|
**Error codes**
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
| ID| Error Message |
| -------- | --------------------------------------- |
| 10200014 | The function is not mark as concurrent. |
**Example**
```ts
@Concurrent
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
let taskGroup = new taskpool.TaskGroup();
taskGroup.addTask(printArgs, 100); // 100: test number
```
### addTask<sup>10+</sup>
addTask(task: Task): void
Adds a created task to this task group.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------- | ---- | ---------------------------------------- |
| task | [Task](#task) | Yes | Task to be added to the task group. |
**Error codes**
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
| ID| Error Message |
| -------- | --------------------------------------- |
| 10200014 | The function is not mark as concurrent. |
**Example**
```ts
@Concurrent
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
let taskGroup = new taskpool.TaskGroup();
let task = new taskpool.Task(printArgs, 200); // 200: test number
taskGroup.addTask(task);
```
## taskpool.execute
execute(func: Function, ...args: unknown[]): Promise\<unknown>
Places the function to be executed in the internal task queue of the task pool. The function will be distributed to the worker thread for execution. The function to be executed in this mode cannot be canceled. ### Attributes
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters** | Name | Type | Readable| Writable| Description |
| --------- | --------- | ---- | ---- | ------------------------------------------------------------------------- |
| function | Function | Yes | Yes | Function to be passed in during task creation. For details about the supported return value types of the function, see [Sequenceable Data Types](#sequenceable-data-types). |
| arguments | unknown[] | Yes | Yes | Arguments of the function. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
| Name| Type | Mandatory| Description | ## TaskGroup<sup>10+</sup>
| ------ | --------- | ---- | ---------------------------------------------------------------------- |
| func | Function | Yes | Function to be executed. For details about the supported return value types of the function, see [Sequenceable Data Types](#sequenceable-data-types). |
| args | unknown[] | No | Arguments of the function. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types). The default value is **undefined**.|
**Return value** Implements a task group. Before calling any APIs in **TaskGroup**, you must use [constructor](#constructor10) to create a **TaskGroup** instance.
| Type | Description | ### constructor<sup>10+</sup>
| ----------------- | ------------------------------------ |
| Promise\<unknown> | Promise used to return the result.|
**Error codes** constructor()
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). Constructor used to create a **TaskGroup** instance.
| ID| Error Message | **System capability**: SystemCapability.Utils.Lang
| -------- | -------------------------------------------- |
| 10200003 | Worker initialization failure. |
| 10200006 | An exception occurred during serialization. |
| 10200014 | The function is not mark as concurrent. |
**Example** **Example**
```ts ```ts
@Concurrent let taskGroup = new taskpool.TaskGroup();
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
taskpool.execute(printArgs, 100).then((value) => { // 100: test number
console.log("taskpool result: " + value);
});
``` ```
## taskpool.execute ### addTask<sup>10+</sup>
execute(task: Task, priority?: Priority): Promise\<unknown> addTask(func: Function, ...args: unknown[]): void
Places a task in the internal task queue of the task pool. The task will be distributed to the worker thread for execution. The task to be executed in this mode can be canceled. Adds the function to be executed to this task group. Before using this API, you must create a **TaskGroup** instance.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| -------- | --------------------- | ---- | ---------------------------------------- | | ------ | --------- | ---- | ---------------------------------------------------------------------- |
| task | [Task](#task) | Yes | Task to be executed. | | func | Function | Yes | Function to be passed in for task execution. For details about the supported return value types of the function, see [Sequenceable Data Types](#sequenceable-data-types). |
| priority | [Priority](#priority) | No | Priority of the task. The default value is **taskpool.Priority.MEDIUM**.| | args | unknown[] | No | Arguments of the function. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types). The default value is **undefined**.|
**Return value**
| Type | Description |
| ---------------- | ---------------- |
| Promise\<unknown> | Promise used to return the result.|
**Error codes** **Error codes**
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
| ID| Error Message | | ID| Error Message |
| -------- | ------------------------------------------- | | -------- | --------------------------------------- |
| 10200003 | Worker initialization failure. | | 10200014 | The function is not mark as concurrent. |
| 10200006 | An exception occurred during serialization. |
| 10200014 | The function is not mark as concurrent. |
**Example** **Example**
...@@ -399,40 +570,31 @@ function printArgs(args) { ...@@ -399,40 +570,31 @@ function printArgs(args) {
return args; return args;
} }
let task = new taskpool.Task(printArgs, 100); // 100: test number let taskGroup = new taskpool.TaskGroup();
taskpool.execute(task).then((value) => { taskGroup.addTask(printArgs, 100); // 100: test number
console.log("taskpool result: " + value);
});
``` ```
## taskpool.execute<sup>10+</sup> ### addTask<sup>10+</sup>
execute(group: TaskGroup, priority?: Priority): Promise<unknown[]> addTask(task: Task): void
Places a task group in the internal task queue of the task pool. The task group will be distributed to the worker thread for execution. Adds a created task to this task group. Before using this API, you must create a **TaskGroup** instance.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| --------- | --------------------------- | ---- | -------------------------------------------------------------- | | -------- | --------------------- | ---- | ---------------------------------------- |
| group | [TaskGroup](#taskgroup) | Yes | Task group to be executed. | | task | [Task](#task) | Yes | Task to be added to the task group. |
| priority | [Priority](#priority) | No | Priority of the task group. The default value is **taskpool.Priority.MEDIUM**.|
**Return value**
| Type | Description |
| ---------------- | ---------------------------------- |
| Promise\<unknown[]> | Promise used to return the result.|
**Error codes** **Error codes**
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
| ID| Error Message | | ID| Error Message |
| -------- | ------------------------------------------- | | -------- | --------------------------------------- |
| 10200006 | An exception occurred during serialization. | | 10200014 | The function is not mark as concurrent. |
**Example** **Example**
...@@ -443,152 +605,72 @@ function printArgs(args) { ...@@ -443,152 +605,72 @@ function 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
taskGroup1.addTask(printArgs, 20); // 20: test number taskGroup.addTask(task);
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
cancel(task: Task): void ## State<sup>10+</sup>
Cancels a task in the task pool. Enumerates the task states.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters** | Name | Value | Description |
| --------- | -------- | ------------- |
| WAITING | 1 | The task is waiting.|
| RUNNING | 2 | The task is running.|
| CANCELED | 3 | The task is canceled.|
| Name| Type | Mandatory| Description |
| ------ | ------------- | ---- | -------------------- |
| task | [Task](#task) | Yes | Task to cancel.|
**Error codes** ## TaskInfo<sup>10+</sup>
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). Describes the internal information about a task.
| ID| Error Message | **System capability**: SystemCapability.Utils.Lang
| -------- | -------------------------------------------- |
| 10200015 | The task does not exist when it is canceled. |
| 10200016 | The task is executing when it is canceled. |
Since API version 10, error code 10200016 is not reported when this API is called. ### Attributes
**Example of canceling an ongoing task** **System capability**: SystemCapability.Utils.Lang
```ts | Name | Type | Readable| Writable| Description |
@Concurrent | -------- | ------------------ | ---- | ---- | ------------------------------------------------------------- |
function inspectStatus(arg) { | taskId | number | Yes | No | Task ID. |
// Check the task cancellation state and return the result. | state | [State](#state10) | Yes | No | Task state. |
if (taskpool.Task.isCanceled()) { | duration | number | Yes | No | Duration that the task has been executed, in ms. If the return value is **0**, the task is not running. If the return value is empty, no task is running. |
console.log("task has been canceled before 2s sleep.");
return arg + 2;
}
// 2s sleep
let t = Date.now();
while (Date.now() - t < 2000) {
continue;
}
// Check the task cancellation state again and return the result.
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 ## ThreadInfo<sup>10+</sup>
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);
}).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);
// Cancel the task 1s later.
setTimeout(()=>{
taskpool.cancel(task1);}, 1000);
```
## taskpool.cancel<sup>10+</sup> Describes the internal information about a worker thread.
cancel(group: TaskGroup): void **System capability**: SystemCapability.Utils.Lang
Cancels a task group in the task pool. ### Attributes
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters** | Name | Type | Readable| Writable| Description |
| -------- | ---------------------- | ---- | ---- | -------------------------------------------------------- |
| tid | number | Yes | No | ID of the worker thread. If the return value is empty, no task is running. |
| taskIds | number[] | Yes | No | IDs of tasks running on the calling thread. If the return value is empty, no task is running. |
| priority | [Priority](#priority) | Yes | No | Priority of the calling thread. If the return value is empty, no task is running. |
| Name | Type | Mandatory| Description | ## TaskPoolInfo<sup>10+</sup>
| ------- | ----------------------- | ---- | -------------------- |
| group | [TaskGroup](#taskgroup) | Yes | Task group to cancel.|
**Error codes** Describes the internal information about a task pool.
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). **System capability**: SystemCapability.Utils.Lang
| ID| Error Message | ### Attributes
| -------- | ------------------------------------------------------- |
| 10200018 | The task group does not exist when it is canceled. |
**Example** **System capability**: SystemCapability.Utils.Lang
```ts | Name | Type | Readable| Writable| Description |
@Concurrent | ------------- | -------------------------------- | ---- | ---- | -------------------- |
function printArgs(args) { | threadInfos | [ThreadInfo[]](#threadinfo10) | Yes | No | Internal information about the worker threads. |
let t = Date.now(); | taskInfos | [TaskInfo[]](#taskinfo10) | Yes | No | Internal information about the tasks. |
while (Date.now() - t < 2000) {
continue;
}
console.log("printArgs: " + args);
return args;
}
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);
```
## Additional Information ## Additional Information
...@@ -720,7 +802,7 @@ func2(); ...@@ -720,7 +802,7 @@ func2();
// Success in canceling a task // Success in canceling a task
@Concurrent @Concurrent
function inspectStatus(arg) { function inspectStatus(arg) {
// Check the task cancellation state and return the result. // Check the cancellation status and return the result.
if (taskpool.Task.isCanceled()) { if (taskpool.Task.isCanceled()) {
console.log("task has been canceled before 2s sleep."); console.log("task has been canceled before 2s sleep.");
return arg + 2; return arg + 2;
...@@ -730,7 +812,7 @@ function inspectStatus(arg) { ...@@ -730,7 +812,7 @@ function inspectStatus(arg) {
while (Date.now() - t < 2000) { while (Date.now() - t < 2000) {
continue; continue;
} }
// Check the task cancellation state again and return the result. // Check the cancellation status again and return the result.
if (taskpool.Task.isCanceled()) { if (taskpool.Task.isCanceled()) {
console.log("task has been canceled after 2s sleep."); console.log("task has been canceled after 2s sleep.");
return arg + 3; return arg + 3;
...@@ -836,3 +918,64 @@ async function taskpoolGroupCancelTest() { ...@@ -836,3 +918,64 @@ async function taskpoolGroupCancelTest() {
taskpoolGroupCancelTest() taskpoolGroupCancelTest()
``` ```
**Example 8**
```ts
// Create and execute 100 tasks with different priorities, and view their information.
@Concurrent
function delay() {
let start = new Date().getTime();
while (new Date().getTime() - start < 500) {
continue;
}
}
let highCount = 0;
let mediumCount = 0;
let lowCount = 0;
let allCount = 100;
for (let i = 0; i < allCount; i++) {
let task1 = new taskpool.Task(delay);
let task2 = new taskpool.Task(delay);
let task3 = new taskpool.Task(delay);
taskpool.execute(task1, taskpool.Priority.LOW).then(() => {
lowCount++;
}).catch((e) => {
console.error("low task error: " + e);
})
taskpool.execute(task2, taskpool.Priority.MEDIUM).then(() => {
mediumCount++;
}).catch((e) => {
console.error("medium task error: " + e);
})
taskpool.execute(task3, taskpool.Priority.HIGH).then(() => {
highCount++;
}).catch((e) => {
console.error("high task error: " + e);
})
}
let start = new Date().getTime();
while (new Date().getTime() - start < 1000) {
continue;
}
let taskpoolInfo = taskpool.getTaskPoolInfo();
let tid = 0;
let taskIds = [];
let priority = 0;
let taskId = 0;
let state = 0;
let duration = 0;
for(let threadInfo of taskpoolInfo.threadInfos) {
tid = threadInfo.tid;
taskIds.length = threadInfo.taskIds.length;
priority = threadInfo.priority;
console.info("taskpool---tid is:" + tid + ", taskIds is:" + taskIds + ", priority is:" + priority);
}
for(let taskInfo of taskpoolInfo.taskInfos) {
taskId = taskInfo.taskId;
state = taskInfo.state;
duration = taskInfo.duration;
console.info("taskpool---taskId is:" + taskId + ", state is:" + state + ", duration is:" + duration);
}
```
...@@ -185,9 +185,9 @@ The package parsing APIs are used to parse an HAP, APP, or HSP file and obtain i ...@@ -185,9 +185,9 @@ The package parsing APIs are used to parse an HAP, APP, or HSP file and obtain i
| appName | String | Label of the ability displayed on the home screen. | NA | | appName | String | Label of the ability displayed on the home screen. | NA |
| appNameEN | String | Label of the ability displayed on the home screen. | NA | | appNameEN | String | Label of the ability displayed on the home screen. | NA |
| releaseType | String | Release type of the target API version required for running the application. | NA | | releaseType | String | Release type of the target API version required for running the application. | NA |
| shellVersionCode | String | API version in HarmonyOS. | NA | | shellVersionCode | String | API version number of the application. | NA |
| shellVersionName | String | API version name in HarmonyOS. | NA | | shellVersionName | String | API version name of the application. | NA |
| multiFrameworkBundle | String | Bundle name of the application in the dual-framework scenario. | NA | | multiFrameworkBundle | boolean | Application framework. | NA |
| debug | boolean | Whether the application can be debugged. | NA | | debug | boolean | Whether the application can be debugged. | NA |
| icon | String | Path of the application icon. | NA | | icon | String | Path of the application icon. | NA |
| label | String | Label of the application. | NA | | label | String | Label of the application. | NA |
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册