Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
ae503873
D
Docs
项目概览
OpenHarmony
/
Docs
大约 2 年 前同步成功
通知
161
Star
293
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
ae503873
编写于
3月 24, 2023
作者:
O
openharmony_ci
提交者:
Gitee
3月 24, 2023
浏览文件
操作
浏览文件
下载
差异文件
!16442 taskpool api document add content (cp 3.2release)
Merge pull request !16442 from wangzhaoyong/cherry-pick-1679575457
上级
82bac522
45c20e85
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
94 addition
and
21 deletion
+94
-21
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
+94
-21
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
浏览文件 @
ae503873
...
...
@@ -13,7 +13,7 @@
## 导入模块
```
j
s
```
t
s
import
taskpool
from
'
@ohos.taskpool
'
;
```
...
...
@@ -58,9 +58,9 @@ Task的构造函数。
**示例:**
```
js
```
ts
@
Concurrent
function
func
(
args
)
{
"
use concurrent
"
console
.
log
(
"
func:
"
+
args
);
return
args
;
}
...
...
@@ -110,9 +110,9 @@ execute(func: Function, ...args: unknown[]): Promise\<unknown>
**示例:**
```
js
```
ts
@
Concurrent
function
func
(
args
)
{
"
use concurrent
"
console
.
log
(
"
func:
"
+
args
);
return
args
;
}
...
...
@@ -158,9 +158,9 @@ execute(task: Task, priority?: Priority): Promise\<unknown>
**示例:**
```
js
```
ts
@
Concurrent
function
func
(
args
)
{
"
use concurrent
"
console
.
log
(
"
func:
"
+
args
);
return
args
;
}
...
...
@@ -199,9 +199,9 @@ cancel(task: Task): void
**示例:**
```
js
```
ts
@
Concurrent
function
func
(
args
)
{
"
use concurrent
"
console
.
log
(
"
func:
"
+
args
);
return
args
;
}
...
...
@@ -209,7 +209,11 @@ function func(args) {
async
function
taskpoolTest
()
{
let
task
=
new
taskpool
.
Task
(
func
,
100
);
let
value
=
await
taskpool
.
execute
(
task
);
taskpool
.
cancel
(
task
);
try
{
taskpool
.
cancel
(
task
);
}
catch
(
e
)
{
console
.
log
(
"
taskpool.cancel occur error:
"
+
e
);
}
}
taskpoolTest
();
...
...
@@ -221,12 +225,19 @@ taskpoolTest();
序列化支持类型包括:All Primitive Type(不包括symbol)、Date、String、RegExp、Array、Map、Set、Object、ArrayBuffer、TypedArray。
### 注意事项
taskpool任务只支持引用入参传递或者import的变量,不支持使用闭包变量。
-
仅支持在Stage模型且module的compileMode为esmodule的project中使用taskpool api。确认module的compileMode方法:查看当前module的build-profile.json5,在buildOption中补充"compileMode": "esmodule"。
-
taskpool任务只支持引用入参传递或者import的变量,不支持使用闭包变量,使用装饰器@Concurrent进行拦截。
-
taskpool任务只支持普通函数或者async函数,不支持类成员函数或者匿名函数,使用装饰器@Concurrent进行拦截。
-
装饰器@Concurrent仅支持在ets文件使用,在ts文件中创建taskpool任务需使用"use concurrent"。
```
js
// 1. 引用入参传递
### 简单使用
**示例一**
```
ts
// 支持普通函数、引用入参传递
@
Concurrent
function
func
(
args
)
{
"
use concurrent
"
console
.
log
(
"
func:
"
+
args
);
return
args
;
}
...
...
@@ -245,17 +256,19 @@ async function taskpoolTest() {
taskpoolTest
();
```
```
js
// 2. 引用import变量
**示例二**
// b.ts
```
ts
// b.ets
export
var
c
=
2000
;
```
```
ts
// 引用import变量
// a.ets(与b.ets位于同一目录中)
import
{
c
}
from
"
./b
"
;
// a.ts(与b.ts同目录)
import
{
c
}
from
'
./b
'
@
Concurrent
function
test
(
a
)
{
"
use concurrent
"
console
.
log
(
a
);
console
.
log
(
c
);
return
a
;
...
...
@@ -273,4 +286,64 @@ async function taskpoolTest() {
}
taskpoolTest
();
```
**示例三**
```
ts
// 支持async函数
@
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
();
```
**示例四**
```
ts
// 在ts文件中创建taskpool任务需使用"use concurrent"
// 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(与c.ts在同一目录中)
import
{
taskpoolTest1
,
taskpoolTest2
}
from
"
./c
"
;
taskpoolTest1
();
taskpoolTest2
();
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录