• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// 并发函数中使用自定义类或函数
17// 并发函数中使用自定义类或函数时需定义在不同文件,否则会被认为是闭包,如下例所示。
18// [Start concurrent_taskpool_custom_class_function]
19import { taskpool } from '@kit.ArkTS';
20import { BusinessError } from '@kit.BasicServicesKit';
21import { testAdd, MyTestA, MyTestB } from './Test';
22
23function add(arg: number) {
24  return ++arg;
25}
26
27class TestA {
28  constructor(name: string) {
29    this.name = name;
30  }
31  name: string = 'ClassA';
32}
33
34class TestB {
35  static nameStr: string = 'ClassB';
36}
37
38@Concurrent
39function testFunc() {
40  // case1:在并发函数中直接调用同文件内定义的类或函数
41
42  // 直接调用同文件定义的函数add(),add飘红报错:
43  // Only imported variables and local variables can be used in @Concurrent decorated functions. <ArkTSCheck>
44  // add(1);
45  // 直接使用同文件定义的TestA构造,TestA飘红报错:
46  // Only imported variables and local variables can be used in @Concurrent decorated functions. <ArkTSCheck>
47  // let a = new TestA('aaa');
48  // 直接访问同文件定义的TestB的成员nameStr,TestB飘红报错:
49  // Only imported variables and local variables can be used in @Concurrent decorated functions. <ArkTSCheck>
50  // console.info(`TestB name is: ${TestB.nameStr}`);
51
52  // case2:在并发函数中调用定义在Test.ets文件并导入当前文件的类或函数
53
54  // 输出结果:res1 is: 2
55  console.info(`res1 is: ${testAdd(1)}`);
56  const tmpStr = new MyTestA('TEST A');
57  // 输出结果:res2 is: TEST A
58  console.info(`res2 is: ${tmpStr.name}`);
59  // 输出结果:res3 is: MyTestB
60  console.info(`res3 is: ${MyTestB.nameStr}`);
61}
62
63
64@Entry
65@Component
66struct Index {
67  @State message: string = 'Hello World';
68
69  build() {
70    RelativeContainer() {
71      Text(this.message)
72        .id('HelloWorld')
73        .fontSize(50)
74        .fontWeight(FontWeight.Bold)
75        .alignRules({
76          center: { anchor: '__container__', align: VerticalAlign.Center },
77          middle: { anchor: '__container__', align: HorizontalAlign.Center }
78        })
79        .onClick(() => {
80          const task = new taskpool.Task(testFunc);
81          taskpool.execute(task).then(() => {
82            console.info('taskpool: execute task success!');
83          }).catch((e:BusinessError) => {
84            console.error(`taskpool: execute: Code: ${e.code}, message: ${e.message}`);
85          })
86          // [StartExclude update_message_on_success]
87          this.message = 'success';
88          // [EndExclude update_message_on_success]
89        })
90    }
91    .height('100%')
92    .width('100%')
93  }
94}
95// [End concurrent_taskpool_custom_class_function]
96