• 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// [Start example_normal_obj]
17import { taskpool } from '@kit.ArkTS';
18import { BusinessError } from '@kit.BasicServicesKit';
19import { TestA } from './Test';
20
21@Concurrent
22async function test1(arg: TestA) {
23  console.info('TestA name is: ' + arg.name);
24}
25
26@Entry
27@Component
28struct Index {
29  @State message: string = 'Hello World';
30
31  build() {
32    RelativeContainer() {
33      Text(this.message)
34        .id('HelloWorld')
35        .fontSize(50)
36        .fontWeight(FontWeight.Bold)
37        .alignRules({
38          center: { anchor: '__container__', align: VerticalAlign.Center },
39          middle: { anchor: '__container__', align: HorizontalAlign.Center }
40        })
41        .onClick(() => {
42          // 1. 创建Test实例objA
43          let objA = new TestA('TestA');
44          // 2. 创建任务task,将objA传递给该任务,objA非sendable对象,通过序列化传递给子线程
45          let task = new taskpool.Task(test1, objA);
46          // 3. 执行任务
47          taskpool.execute(task).then(() => {
48            console.info('taskpool: execute task success!');
49          }).catch((e:BusinessError) => {
50            console.error(`taskpool: execute task: Code: ${e.code}, message: ${e.message}`);
51          })
52          this.message = 'success';
53        })
54    }
55    .height('100%')
56    .width('100%')
57  }
58}
59// [End example_normal_obj]
60