提交 7179f619 编写于 作者: G Gloria

Update docs against 15668

Signed-off-by: wusongqing<wusongqing@huawei.com>
上级 e7ea18b3
...@@ -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
import taskpool from '@ohos.taskpool'; import taskpool from '@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
function func(args) { function func(args) {
"use concurrent"
console.log("func: " + args); console.log("func: " + args);
return args; return args;
} }
...@@ -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
function func(args) { function func(args) {
"use concurrent"
console.log("func: " + args); console.log("func: " + args);
return args; return args;
} }
...@@ -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
function func(args) { function func(args) {
"use concurrent"
console.log("func: " + args); console.log("func: " + args);
return args; return args;
} }
...@@ -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
function func(args) { function func(args) {
"use concurrent"
console.log("func: " + args); console.log("func: " + args);
return args; return args;
} }
...@@ -209,7 +209,11 @@ function func(args) { ...@@ -209,7 +209,11 @@ function func(args) {
async function taskpoolTest() { async function taskpoolTest() {
let task = new taskpool.Task(func, 100); let task = new taskpool.Task(func, 100);
let value = await taskpool.execute(task); let value = await taskpool.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
function func(args) { function func(args) {
"use concurrent"
console.log("func: " + args); console.log("func: " + args);
return args; return args;
} }
...@@ -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
export var c = 2000; export var c = 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'
function test(a) { function test(a) {
"use concurrent"
console.log(a); console.log(a);
console.log(c); console.log(c);
return a; return a;
...@@ -274,3 +287,63 @@ async function taskpoolTest() { ...@@ -274,3 +287,63 @@ async function taskpoolTest() {
taskpoolTest(); taskpoolTest();
``` ```
**Example 3**
```ts
// The async functions are supported.
@Concurrent
async function task() {
let ret = await Promise.all([
new Promise(resolve => setTimeout(resolve, 1000, "resolved"))
]);
return ret;
}
async function taskpoolTest() {
taskpool.execute(task).then((result) => {
console.log("TaskPoolTest task result: " + result);
});
}
taskpoolTest();
```
**Example 4**
```ts
// Use use concurrent to create a task in the task pool in the .ts file.
// c.ts
function test1(n) {
"use concurrent"
return n;
}
export async function taskpoolTest1() {
console.log("taskpoolTest1 start");
var task = new taskpool.Task(test1, 100);
var result = await taskpool.execute(task);
console.log("taskpoolTest1 result:" + result);
}
async function test2() {
"use concurrent"
var ret = await Promise.all([
new Promise(resolve => setTimeout(resolve, 1000, "resolved"))
]);
return ret;
}
export async function taskpoolTest2() {
console.log("taskpoolTest2 start");
taskpool.execute(test2).then((result) => {
console.log("TaskPoolTest2 result: " + result);
});
}
```
```ts
/ / a.ets (in the same directory as c.ts)
import { taskpoolTest1, taskpoolTest2 } from "./c";
taskpoolTest1();
taskpoolTest2();
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册