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