Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
14939522
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看板
未验证
提交
14939522
编写于
5月 29, 2023
作者:
O
openharmony_ci
提交者:
Gitee
5月 29, 2023
浏览文件
操作
浏览文件
下载
差异文件
!18838 master:modify taskpool api document
Merge pull request !18838 from buzhuyu/master
上级
c8fb6de2
c428f422
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
76 addition
and
81 deletion
+76
-81
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
+76
-81
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
浏览文件 @
14939522
...
...
@@ -2,9 +2,9 @@
任务池(taskpool)作用是为应用程序提供一个多线程的运行环境,降低整体资源的消耗、提高系统的整体性能,且您无需关心线程实例的生命周期。您可以使用任务池API创建后台任务(Task),并对所创建的任务进行如任务执行、任务取消的操作。理论上您可以使用任务池API创建数量不受限制的任务,但是出于内存因素不建议您这样做。此外,不建议您在任务中执行阻塞操作,特别是无限期阻塞操作,长时间的阻塞操作占据工作线程,可能会阻塞其他任务调度,影响您的应用性能。
您所创建的同一优先级任务的执行顺序可以由您决定,任务真实执行的顺序与您调用任务池API提供的任务执行接口顺序一致。任务默认优先级是MEDIUM。
(任务优先级机制暂未支持)
您所创建的同一优先级任务的执行顺序可以由您决定,任务真实执行的顺序与您调用任务池API提供的任务执行接口顺序一致。任务默认优先级是MEDIUM。
当同一时间待执行的任务数量大于任务池工作线程数量,任务池会根据负载均衡机制进行扩容,增加工作线程数量,减少整体等待时长。同样,当执行的任务数量减少,工作线程数量大于执行任务数量,部分工作线程处于空闲状态,任务池会根据负载均衡机制进行缩容,减少工作线程数量。
(负载均衡机制暂未支持)
当同一时间待执行的任务数量大于任务池工作线程数量,任务池会根据负载均衡机制进行扩容,增加工作线程数量,减少整体等待时长。同样,当执行的任务数量减少,工作线程数量大于执行任务数量,部分工作线程处于空闲状态,任务池会根据负载均衡机制进行缩容,减少工作线程数量。
任务池API以数字形式返回错误码。有关各个错误码的更多信息,请参阅文档
[
语言基础类库错误码
](
../errorcodes/errorcode-utils.md
)
。
...
...
@@ -32,13 +32,13 @@ import taskpool from '@ohos.taskpool';
**示例:**
```
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
Priority
()
{
let
task
=
new
taskpool
.
Task
(
printArgs
,
100
);
let
highCount
=
0
;
let
mediumCount
=
0
;
...
...
@@ -65,7 +65,7 @@ async function taskpoolTest() {
})
}
}
taskpool
Test
();
taskpool
Priority
();
```
## Task
...
...
@@ -99,12 +99,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
"
);
```
### 属性
...
...
@@ -151,17 +151,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
...
...
@@ -199,18 +199,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
...
...
@@ -239,14 +239,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
);
...
...
@@ -255,20 +255,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,确保任务已执行
...
...
@@ -282,25 +282,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
);
...
...
@@ -315,7 +315,7 @@ async function taskpoolTest() {
}
}
taskpool
Test
();
taskpool
Cancel
();
```
## 其他说明
...
...
@@ -327,7 +327,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文件使用。
### 简单使用
...
...
@@ -336,23 +336,23 @@ taskpoolTest();
```
ts
// 支持普通函数、引用入参传递
@
Concurrent
function
func
(
args
)
{
function
printArgs
(
args
)
{
console
.
log
(
"
func:
"
+
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
);
// taskpool.execute(function)
let
val2
=
await
taskpool
.
execute
(
func
,
"
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
();
```
**示例二**
...
...
@@ -367,24 +367,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
();
```
**示例三**
...
...
@@ -392,57 +392,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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录