@@ -13,7 +13,7 @@ The **TaskPool** APIs return error codes in numeric format. For details about th
## Modules to Import
```js
```ts
importtaskpoolfrom'@ohos.taskpool';
```
...
...
@@ -58,9 +58,9 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
**Example**
```js
```ts
@Concurrent
functionfunc(args){
"use concurrent"
console.log("func: "+args);
returnargs;
}
...
...
@@ -110,9 +110,9 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
**Example**
```js
```ts
@Concurrent
functionfunc(args){
"use concurrent"
console.log("func: "+args);
returnargs;
}
...
...
@@ -158,9 +158,9 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
**Example**
```js
```ts
@Concurrent
functionfunc(args){
"use concurrent"
console.log("func: "+args);
returnargs;
}
...
...
@@ -199,9 +199,9 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
**Example**
```js
```ts
@Concurrent
functionfunc(args){
"use concurrent"
console.log("func: "+args);
returnargs;
}
...
...
@@ -209,7 +209,11 @@ function func(args) {
asyncfunctiontaskpoolTest(){
lettask=newtaskpool.Task(func,100);
letvalue=awaittaskpool.execute(task);
taskpool.cancel(task);
try{
taskpool.cancel(task);
}catch(e){
console.log("taskpool.cancel occur error:"+e);
}
}
taskpoolTest();
...
...
@@ -221,12 +225,19 @@ taskpoolTest();
The following sequenceable data types are supported: All Primitive Type (excluding symbol), Date, String, RegExp, Array, Map, Set, Object, ArrayBuffer, and TypedArray.
### Precautions
A task in the task pool can reference only variables passed in by input parameters or imported variables. It does not support closure variables.
- 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**.
```js
// 1. Reference a variable passed in by the input parameter.
### Using the Task Pool in Simple Mode
**Example 1**
```ts
// Common functions are supported, and variables passed in by input parameters are also supported.
@Concurrent
functionfunc(args){
"use concurrent"
console.log("func: "+args);
returnargs;
}
...
...
@@ -245,17 +256,19 @@ async function taskpoolTest() {
taskpoolTest();
```
```js
// 2. Reference an imported variable.
**Example 2**
// b.ts
```ts
// b.ets
exportvarc=2000;
```
```ts
// Reference an imported variable.
// a.ets (in the same directory as b.ets)
import{c}from"./b";
// a.ts (in the same directory as b.ts)
import{c}from'./b'
@Concurrent
functiontest(a){
"use concurrent"
console.log(a);
console.log(c);
returna;
...
...
@@ -274,3 +287,63 @@ async function taskpoolTest() {