diff --git a/en/application-dev/reference/apis/js-apis-taskpool.md b/en/application-dev/reference/apis/js-apis-taskpool.md index 5e5fb1bbd6e67ef575778c601667f239b895e005..d05f7cdbed1cd756d195f442d368a73f74503c2f 100644 --- a/en/application-dev/reference/apis/js-apis-taskpool.md +++ b/en/application-dev/reference/apis/js-apis-taskpool.md @@ -16,6 +16,302 @@ The **TaskPool** APIs return error codes in numeric format. For details about th ```ts import taskpool from '@ohos.taskpool'; ``` +## taskpool.execute + +execute(func: Function, ...args: unknown[]): Promise\ + +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\ | 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\ + +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\ | 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.execute10+ + +execute(group: TaskGroup, priority?: Priority): Promise + +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\ | 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.cancel10+ + +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.getTaskPoolInfo10+ + +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 @@ -67,7 +363,7 @@ for (let i = 0; i < allCount; i++) { ## 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 @@ -108,7 +404,7 @@ let task = new taskpool.Task(printArgs, "this is my first Task"); 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 @@ -174,7 +470,7 @@ taskpool.execute(task).then((res)=>{ 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** > @@ -212,183 +508,58 @@ taskpool.execute(task).then((res)=>{ console.error("test catch: " + e); }) console.info("testTransfer view byteLength: " + view.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).| - -## TaskGroup10+ - -Implements a task group. Before using any of the following APIs, you must create a **TaskGroup** instance. - -### constructor10+ - -constructor() - -Constructor used to create a **TaskGroup** instance. - -**System capability**: SystemCapability.Utils.Lang - -**Example** - -```ts -let taskGroup = new taskpool.TaskGroup(); -``` - -### addTask10+ - -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 -``` - -### addTask10+ - -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\ +console.info("testTransfer view1 byteLength: " + view1.byteLength); +``` -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 -**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 | -| ------ | --------- | ---- | ---------------------------------------------------------------------- | -| 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**.| +## TaskGroup10+ -**Return value** +Implements a task group. Before calling any APIs in **TaskGroup**, you must use [constructor](#constructor10) to create a **TaskGroup** instance. -| Type | Description | -| ----------------- | ------------------------------------ | -| Promise\ | Promise used to return the result.| +### constructor10+ -**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 | -| -------- | -------------------------------------------- | -| 10200003 | Worker initialization failure. | -| 10200006 | An exception occurred during serialization. | -| 10200014 | The function is not mark as concurrent. | +**System capability**: SystemCapability.Utils.Lang **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); -}); +let taskGroup = new taskpool.TaskGroup(); ``` -## taskpool.execute +### addTask10+ -execute(task: Task, priority?: Priority): Promise\ +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 **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\ | Promise used to return the result.| +| 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 | -| -------- | ------------------------------------------- | -| 10200003 | Worker initialization failure. | -| 10200006 | An exception occurred during serialization. | -| 10200014 | The function is not mark as concurrent. | +| ID| Error Message | +| -------- | --------------------------------------- | +| 10200014 | The function is not mark as concurrent. | **Example** @@ -399,40 +570,31 @@ function printArgs(args) { return args; } -let task = new taskpool.Task(printArgs, 100); // 100: test number -taskpool.execute(task).then((value) => { - console.log("taskpool result: " + value); -}); +let taskGroup = new taskpool.TaskGroup(); +taskGroup.addTask(printArgs, 100); // 100: test number ``` -## taskpool.execute10+ +### addTask10+ -execute(group: TaskGroup, priority?: Priority): Promise +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 **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\ | Promise used to return the result.| +| 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 | -| -------- | ------------------------------------------- | -| 10200006 | An exception occurred during serialization. | +| ID| Error Message | +| -------- | --------------------------------------- | +| 10200014 | The function is not mark as concurrent. | **Example** @@ -443,152 +605,72 @@ function 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); -}); +let taskGroup = new taskpool.TaskGroup(); +let task = new taskpool.Task(printArgs, 200); // 200: test number +taskGroup.addTask(task); ``` -## taskpool.cancel -cancel(task: Task): void +## State10+ -Cancels a task in the task pool. +Enumerates the task states. **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** +## TaskInfo10+ -For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). +Describes the internal information about a task. -| ID| Error Message | -| -------- | -------------------------------------------- | -| 10200015 | The task does not exist when it is canceled. | -| 10200016 | The task is executing when it is canceled. | +**System capability**: SystemCapability.Utils.Lang -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 -@Concurrent -function inspectStatus(arg) { - // Check the task cancellation state 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 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; -} +| Name | Type | Readable| Writable| Description | +| -------- | ------------------ | ---- | ---- | ------------------------------------------------------------- | +| taskId | number | Yes | No | Task ID. | +| state | [State](#state10) | Yes | No | Task state. | +| 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. | -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); -``` +## ThreadInfo10+ -## taskpool.cancel10+ +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 -**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 | -| ------- | ----------------------- | ---- | -------------------- | -| group | [TaskGroup](#taskgroup) | Yes | Task group to cancel.| +## TaskPoolInfo10+ -**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 | -| -------- | ------------------------------------------------------- | -| 10200018 | The task group does not exist when it is canceled. | +### Attributes -**Example** +**System capability**: SystemCapability.Utils.Lang -```ts -@Concurrent -function printArgs(args) { - let t = Date.now(); - while (Date.now() - t < 2000) { - continue; - } - console.log("printArgs: " + args); - return args; -} +| Name | Type | Readable| Writable| Description | +| ------------- | -------------------------------- | ---- | ---- | -------------------- | +| threadInfos | [ThreadInfo[]](#threadinfo10) | Yes | No | Internal information about the worker threads. | +| taskInfos | [TaskInfo[]](#taskinfo10) | Yes | No | Internal information about the tasks. | -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 @@ -720,7 +802,7 @@ func2(); // Success in canceling a task @Concurrent function inspectStatus(arg) { - // Check the task cancellation state and return the result. + // Check the cancellation status and return the result. if (taskpool.Task.isCanceled()) { console.log("task has been canceled before 2s sleep."); return arg + 2; @@ -730,7 +812,7 @@ function inspectStatus(arg) { while (Date.now() - t < 2000) { continue; } - // Check the task cancellation state again and return the result. + // 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; @@ -836,3 +918,64 @@ async function 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); +} +``` diff --git a/en/application-dev/tools/unpacking-tool.md b/en/application-dev/tools/unpacking-tool.md index 3b9571faaf6ac451a4e261037289aca664f6ecf6..3bae9bc88ac6195d166f6ca7bca408a1e6a48090 100644 --- a/en/application-dev/tools/unpacking-tool.md +++ b/en/application-dev/tools/unpacking-tool.md @@ -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 | | 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 | -| shellVersionCode | String | API version in HarmonyOS. | NA | -| shellVersionName | String | API version name in HarmonyOS. | NA | -| multiFrameworkBundle | String | Bundle name of the application in the dual-framework scenario. | NA | +| shellVersionCode | String | API version number of the application. | NA | +| shellVersionName | String | API version name of the application. | NA | +| multiFrameworkBundle | boolean | Application framework. | NA | | debug | boolean | Whether the application can be debugged. | NA | | icon | String | Path of the application icon. | NA | | label | String | Label of the application. | NA |