Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
bf552f02
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
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看板
未验证
提交
bf552f02
编写于
5月 31, 2023
作者:
O
openharmony_ci
提交者:
Gitee
5月 31, 2023
浏览文件
操作
浏览文件
下载
差异文件
!18877 3.2-release:modify taskpool api document
Merge pull request !18877 from buzhuyu/OpenHarmony-3.2-Release
上级
8b6a99fc
dd2c09f7
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
68 addition
and
73 deletion
+68
-73
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
+68
-73
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
浏览文件 @
bf552f02
...
...
@@ -60,12 +60,12 @@ Task的构造函数。
```
ts
@
Concurrent
function
func
(
args
)
{
console
.
log
(
"
func
:
"
+
args
);
function
printArgs
(
args
)
{
console
.
log
(
"
printArgs
:
"
+
args
);
return
args
;
}
let
task
=
new
taskpool
.
Task
(
func
,
"
this is my first Task
"
);
let
task
=
new
taskpool
.
Task
(
printArgs
,
"
this is my first Task
"
);
```
### 属性
...
...
@@ -112,17 +112,17 @@ execute(func: Function, ...args: unknown[]): Promise\<unknown>
```
ts
@
Concurrent
function
func
(
args
)
{
console
.
log
(
"
func
:
"
+
args
);
function
printArgs
(
args
)
{
console
.
log
(
"
printArgs
:
"
+
args
);
return
args
;
}
async
function
taskpool
Test
()
{
let
value
=
await
taskpool
.
execute
(
func
,
100
);
async
function
taskpool
Execute
()
{
let
value
=
await
taskpool
.
execute
(
printArgs
,
100
);
console
.
log
(
"
taskpool result:
"
+
value
);
}
taskpool
Test
();
taskpool
Execute
();
```
## taskpool.execute
...
...
@@ -160,18 +160,18 @@ execute(task: Task, priority?: Priority): Promise\<unknown>
```
ts
@
Concurrent
function
func
(
args
)
{
console
.
log
(
"
func
:
"
+
args
);
function
printArgs
(
args
)
{
console
.
log
(
"
printArgs
:
"
+
args
);
return
args
;
}
async
function
taskpool
Test
()
{
let
task
=
new
taskpool
.
Task
(
func
,
100
);
async
function
taskpool
Execute
()
{
let
task
=
new
taskpool
.
Task
(
printArgs
,
100
);
let
value
=
await
taskpool
.
execute
(
task
);
console
.
log
(
"
taskpool result:
"
+
value
);
}
taskpool
Test
();
taskpool
Execute
();
```
## taskpool.cancel
...
...
@@ -200,14 +200,14 @@ cancel(task: Task): void
**任务取消成功示例:**
```
ts
function
func
(
args
)
{
"
use concurrent
"
;
console
.
log
(
"
func
:
"
+
args
);
@
Concurrent
function
printArgs
(
args
)
{
console
.
log
(
"
printArgs
:
"
+
args
);
return
args
;
}
async
function
taskpool
Test
()
{
let
task
=
new
taskpool
.
Task
(
func
,
100
);
async
function
taskpool
Cancel
()
{
let
task
=
new
taskpool
.
Task
(
printArgs
,
100
);
taskpool
.
execute
(
task
);
try
{
taskpool
.
cancel
(
task
);
...
...
@@ -216,20 +216,20 @@ async function taskpoolTest() {
}
}
taskpool
Test
();
taskpool
Cancel
();
```
**已执行的任务取消失败示例:**
```
ts
function
func
(
args
)
{
"
use concurrent
"
;
console
.
log
(
"
func
:
"
+
args
);
@
Concurrent
function
printArgs
(
args
)
{
console
.
log
(
"
printArgs
:
"
+
args
);
return
args
;
}
async
function
taskpool
Test
()
{
let
task
=
new
taskpool
.
Task
(
func
,
100
);
async
function
taskpool
Cancel
()
{
let
task
=
new
taskpool
.
Task
(
printArgs
,
100
);
let
value
=
taskpool
.
execute
(
task
);
let
start
=
new
Date
().
getTime
();
while
(
new
Date
().
getTime
()
-
start
<
1000
)
{
// 延时1s,确保任务已执行
...
...
@@ -243,25 +243,25 @@ async function taskpoolTest() {
}
}
taskpool
Test
();
taskpool
Cancel
();
```
**正在执行的任务取消失败示例:**
```
ts
function
func
(
args
)
{
"
use concurrent
"
;
console
.
log
(
"
func
:
"
+
args
);
@
Concurrent
function
printArgs
(
args
)
{
console
.
log
(
"
printArgs
:
"
+
args
);
return
args
;
}
async
function
taskpool
Test
()
{
let
task1
=
new
taskpool
.
Task
(
func
,
100
);
let
task2
=
new
taskpool
.
Task
(
func
,
200
);
let
task3
=
new
taskpool
.
Task
(
func
,
300
);
let
task4
=
new
taskpool
.
Task
(
func
,
400
);
let
task5
=
new
taskpool
.
Task
(
func
,
500
);
let
task6
=
new
taskpool
.
Task
(
func
,
600
);
async
function
taskpool
Cancel
()
{
let
task1
=
new
taskpool
.
Task
(
printArgs
,
100
);
let
task2
=
new
taskpool
.
Task
(
printArgs
,
200
);
let
task3
=
new
taskpool
.
Task
(
printArgs
,
300
);
let
task4
=
new
taskpool
.
Task
(
printArgs
,
400
);
let
task5
=
new
taskpool
.
Task
(
printArgs
,
500
);
let
task6
=
new
taskpool
.
Task
(
printArgs
,
600
);
let
res1
=
taskpool
.
execute
(
task1
);
let
res2
=
taskpool
.
execute
(
task2
);
...
...
@@ -276,7 +276,7 @@ async function taskpoolTest() {
}
}
taskpool
Test
();
taskpool
Cancel
();
```
## 其他说明
...
...
@@ -288,7 +288,7 @@ taskpoolTest();
-
仅支持在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"
。
-
装饰器@Concurrent仅支持在ets文件使用。
### 简单使用
...
...
@@ -297,14 +297,14 @@ taskpoolTest();
```
ts
// 支持普通函数、引用入参传递
@
Concurrent
function
func
(
args
)
{
console
.
log
(
"
func
:
"
+
args
);
function
printArgs
(
args
)
{
console
.
log
(
"
printArgs
:
"
+
args
);
return
args
;
}
async
function
taskpool
Test
()
{
async
function
taskpool
Execute
()
{
// taskpool.execute(task)
let
task
=
new
taskpool
.
Task
(
func
,
"
create task, then execute
"
);
let
task
=
new
taskpool
.
Task
(
printArgs
,
"
create task, then execute
"
);
let
val1
=
await
taskpool
.
execute
(
task
);
console
.
log
(
"
taskpool.execute(task) result:
"
+
val1
);
...
...
@@ -313,7 +313,7 @@ async function taskpoolTest() {
console
.
log
(
"
taskpool.execute(function) result:
"
+
val2
);
}
taskpool
Test
();
taskpool
Execute
();
```
**示例二**
...
...
@@ -328,24 +328,24 @@ export var c = 2000;
import
{
c
}
from
"
./b
"
;
@
Concurrent
function
test
(
a
)
{
function
printArgs
(
a
)
{
console
.
log
(
a
);
console
.
log
(
c
);
return
a
;
}
async
function
taskpool
Test
()
{
async
function
taskpool
Execute
()
{
// taskpool.execute(task)
let
task
=
new
taskpool
.
Task
(
test
,
"
create task, then execute
"
);
let
task
=
new
taskpool
.
Task
(
printArgs
,
"
create task, then execute
"
);
let
val1
=
await
taskpool
.
execute
(
task
);
console
.
log
(
"
taskpool.execute(task) result:
"
+
val1
);
// taskpool.execute(function)
let
val2
=
await
taskpool
.
execute
(
test
,
"
execute task by func
"
);
let
val2
=
await
taskpool
.
execute
(
printArgs
,
"
execute task by func
"
);
console
.
log
(
"
taskpool.execute(function) result:
"
+
val2
);
}
taskpool
Test
();
taskpool
Execute
();
```
**示例三**
...
...
@@ -353,57 +353,52 @@ taskpoolTest();
```
ts
// 支持async函数
@
Concurrent
async
function
task
()
{
async
function
delayExcute
()
{
let
ret
=
await
Promise
.
all
([
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
1000
,
"
resolved
"
))
]);
return
ret
;
}
async
function
taskpool
Test
()
{
taskpool
.
execute
(
task
).
then
((
result
)
=>
{
async
function
taskpool
Execute
()
{
taskpool
.
execute
(
delayExcute
).
then
((
result
)
=>
{
console
.
log
(
"
TaskPoolTest task result:
"
+
result
);
});
}
taskpool
Test
();
taskpool
Execute
();
```
**示例四**
```
ts
//
在ts文件中创建taskpool任务需使用"use concurrent"
// c.ts
function
test1
(
n
)
{
"
use concurrent
"
return
n
;
//
c.ets
@
Concurrent
function
strSort
(
inPutArr
)
{
let
newArr
=
inPutArr
.
sort
();
return
newArr
;
}
export
async
function
taskpoolTest1
()
{
console
.
log
(
"
taskpoolTest1 start
"
);
var
task
=
new
taskpool
.
Task
(
test1
,
100
);
export
async
function
func1
()
{
console
.
log
(
"
taskpoolTest start
"
);
let
strArray
=
[
'
c test string
'
,
'
b test string
'
,
'
a test string
'
];
var
task
=
new
taskpool
.
Task
(
strSort
,
strArray
);
var
result
=
await
taskpool
.
execute
(
task
);
console
.
log
(
"
taskpoolTest
1 result:
"
+
result
);
console
.
log
(
"
func
1 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
()
{
export
async
function
func2
()
{
console
.
log
(
"
taskpoolTest2 start
"
);
taskpool
.
execute
(
test2
).
then
((
result
)
=>
{
console
.
log
(
"
TaskPoolTest2 result:
"
+
result
);
let
strArray
=
[
'
c test string
'
,
'
b test string
'
,
'
a test string
'
];
taskpool
.
execute
(
strSort
,
strArray
).
then
((
result
)
=>
{
console
.
log
(
"
func2 result:
"
+
result
);
});
}
```
```
ts
// a.ets(与c.ts在同一目录中)
// a.ets(与c.
e
ts在同一目录中)
import
{
taskpoolTest1
,
taskpoolTest2
}
from
"
./c
"
;
taskpoolTest
1
();
taskpoolTest
2
();
func
1
();
func
2
();
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录