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