@@ -4,7 +4,7 @@ The **AbilityDelegatorRegistry** module provides APIs for storing the global reg
> **NOTE**
>
> The APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.abilityDelegatorRegistry](js-apis-app-ability-abilityDelegatorRegistry.md) instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.
@@ -4,7 +4,7 @@ The **AbilityManager** module provides APIs for obtaining, adding, and modifying
> **NOTE**
>
> The APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.abilityManager](js-apis-app-ability-abilityManager.md) instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs of this module are system APIs and cannot be called by third-party applications.
@@ -4,7 +4,7 @@ The **missionManager** module provides APIs to lock, unlock, and clear missions,
> **NOTE**
>
> The APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.missionManager](js-apis-app-ability-missionManager.md) instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.
The task pool provides a multi-thread running environment for applications. It helps reduce resource consumption and improve system performance. It also frees you from caring about the lifecycle of thread instances. You can use the **TaskPool** APIs to create background tasks and perform operations on them, for example, executing or canceling a task. Theoretically, you can create an unlimited number of tasks, but this is not recommended for memory considerations. In addition, you are not advised performing blocking operations in a task, especially indefinite blocking. Long-time blocking operations occupy worker threads and may block other task scheduling, adversely affecting your application performance.
...
...
@@ -60,12 +60,12 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```ts
@Concurrent
functionfunc(args){
console.log("func: "+args);
functionprintArgs(args){
console.log("printArgs: "+args);
returnargs;
}
lettask=newtaskpool.Task(func,"this is my first Task");
lettask=newtaskpool.Task(printArgs,"this is my first Task");
```
### Attributes
...
...
@@ -112,17 +112,17 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```ts
@Concurrent
functionfunc(args){
console.log("func: "+args);
functionprintArgs(args){
console.log("printArgs: "+args);
returnargs;
}
asyncfunctiontaskpoolTest(){
letvalue=awaittaskpool.execute(func,100);
asyncfunctiontaskpoolExecute(){
letvalue=awaittaskpool.execute(printArgs,100);
console.log("taskpool result: "+value);
}
taskpoolTest();
taskpoolExecute();
```
## taskpool.execute
...
...
@@ -136,7 +136,7 @@ Places a task in the internal task queue of the task pool. The task will be dist
| task | [Task](#task) | Yes | Task to be executed. |
| priority | [Priority](#priority) | No | Priority of the task (not supported yet).|
...
...
@@ -160,18 +160,18 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```ts
@Concurrent
functionfunc(args){
console.log("func: "+args);
functionprintArgs(args){
console.log("printArgs: "+args);
returnargs;
}
asyncfunctiontaskpoolTest(){
lettask=newtaskpool.Task(func,100);
asyncfunctiontaskpoolExecute(){
lettask=newtaskpool.Task(printArgs,100);
letvalue=awaittaskpool.execute(task);
console.log("taskpool result: "+value);
}
taskpoolTest();
taskpoolExecute();
```
## taskpool.cancel
...
...
@@ -200,14 +200,14 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
**Example of successful task cancellation**
```ts
functionfunc(args){
"use concurrent";
console.log("func: "+args);
@Concurrent
functionprintArgs(args){
console.log("printArgs: "+args);
returnargs;
}
asyncfunctiontaskpoolTest(){
lettask=newtaskpool.Task(func,100);
asyncfunctiontaskpoolCancel(){
lettask=newtaskpool.Task(printArgs,100);
taskpool.execute(task);
try{
taskpool.cancel(task);
...
...
@@ -216,20 +216,20 @@ async function taskpoolTest() {
}
}
taskpoolTest();
taskpoolCancel();
```
**Example of a failure to cancel a task that has been executed**
```ts
functionfunc(args){
"use concurrent";
console.log("func: "+args);
@Concurrent
functionprintArgs(args){
console.log("printArgs: "+args);
returnargs;
}
asyncfunctiontaskpoolTest(){
lettask=newtaskpool.Task(func,100);
asyncfunctiontaskpoolCancel(){
lettask=newtaskpool.Task(printArgs,100);
letvalue=taskpool.execute(task);
letstart=newDate().getTime();
while(newDate().getTime()-start<1000){// Wait for 1s to ensure that the task has been executed.
...
...
@@ -243,25 +243,25 @@ async function taskpoolTest() {
}
}
taskpoolTest();
taskpoolCancel();
```
**Example of a failure to cancel an ongoing task**
```ts
functionfunc(args){
"use concurrent";
console.log("func: "+args);
@Concurrent
functionprintArgs(args){
console.log("printArgs: "+args);
returnargs;
}
asyncfunctiontaskpoolTest(){
lettask1=newtaskpool.Task(func,100);
lettask2=newtaskpool.Task(func,200);
lettask3=newtaskpool.Task(func,300);
lettask4=newtaskpool.Task(func,400);
lettask5=newtaskpool.Task(func,500);
lettask6=newtaskpool.Task(func,600);
asyncfunctiontaskpoolCancel(){
lettask1=newtaskpool.Task(printArgs,100);
lettask2=newtaskpool.Task(printArgs,200);
lettask3=newtaskpool.Task(printArgs,300);
lettask4=newtaskpool.Task(printArgs,400);
lettask5=newtaskpool.Task(printArgs,500);
lettask6=newtaskpool.Task(printArgs,600);
letres1=taskpool.execute(task1);
letres2=taskpool.execute(task2);
...
...
@@ -276,7 +276,7 @@ async function taskpoolTest() {
}
}
taskpoolTest();
taskpoolCancel();
```
## Additional Information
...
...
@@ -288,7 +288,7 @@ The following sequenceable data types are supported: All Primitive Type (excludi
- The task pool APIs can be used only in the module with **compileMode** set to **esmodule** in the stage model. To check the **compileMode** setting of a module, open the **build-profile.json5** file of the module and check for **"compileMode": "esmodule"** under **buildOption**.
- A task in the task pool can reference only variables passed in by input parameters or imported variables, rather than closure variables. The decorator **@Concurrent** is used to intercept unsupported variables.
- A task in the task pool supports only common functions or async functions, rather than class member functions or anonymous functions. The decorator **@Concurrent** is used to intercept unsupported functions.
- The decorator **@Concurrent** can be used only in the .ets file. To create a task in the task pool in the .ts file, use the statement **use concurrent**.
- The decorator **@Concurrent** can be used only in .ets files.
### Using the Task Pool in Simple Mode
...
...
@@ -297,14 +297,14 @@ The following sequenceable data types are supported: All Primitive Type (excludi
```ts
// Common functions are supported, and variables passed in by input parameters are also supported.
@Concurrent
functionfunc(args){
console.log("func: "+args);
functionprintArgs(args){
console.log("printArgs: "+args);
returnargs;
}
asyncfunctiontaskpoolTest(){
asyncfunctiontaskpoolExecute(){
// taskpool.execute(task)
lettask=newtaskpool.Task(func,"create task, then execute");
lettask=newtaskpool.Task(printArgs,"create task, then execute");