W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在使用TaskPool時(shí),執(zhí)行的并發(fā)函數(shù)需要使用該裝飾器修飾,否則無(wú)法通過(guò)相關(guān)校驗(yàn)。
從API version 9開(kāi)始,該裝飾器支持在ArkTS卡片中使用。
@Concurrent并發(fā)裝飾器 | 說(shuō)明 |
---|---|
裝飾器參數(shù) | 無(wú)。 |
使用場(chǎng)景 | 僅支持在Stage模型的工程中使用。 |
裝飾的函數(shù)類型 | 允許標(biāo)注async函數(shù)或普通函數(shù)。禁止標(biāo)注generator、箭頭函數(shù)、method。不支持類成員函數(shù)或者匿名函數(shù)。 |
裝飾的函數(shù)內(nèi)的變量類型 | 允許使用local變量、入?yún)⒑屯ㄟ^(guò)import引入的變量。禁止使用閉包變量。 |
- import taskpool from '@ohos.taskpool';
- @Concurrent
- function add(num1: number, num2: number): number {
- return num1 + num2;
- }
- async function ConcurrentFunc(): Promise<void> {
- try {
- let task: taskpool.Task = new taskpool.Task(add, 1, 2);
- console.info("taskpool res is: " + await taskpool.execute(task));
- } catch (e) {
- console.error("taskpool execute error is: " + e);
- }
- }
- @Entry
- @Component
- struct Index {
- @State message: string = 'Hello World'
- build() {
- Row() {
- Column() {
- Text(this.message)
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
- .onClick(() => {
- ConcurrentFunc();
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: