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// 并发函数为一个计算两数之和的普通函数,taskpool执行该函数并返回结果。 18// [Start concurrent_taskpool_common_usage] 19import { taskpool } from '@kit.ArkTS'; 20 21@Concurrent 22function add(num1: number, num2: number): number { 23 return num1 + num2; 24} 25 26async function concurrentFunc(): Promise<void> { 27 try { 28 const task: taskpool.Task = new taskpool.Task(add, 1, 2); 29 console.info(`taskpool res is: ${await taskpool.execute(task)}`); // 输出结果:taskpool res is: 3 30 } catch (e) { 31 console.error(`taskpool execute error is: ${e}`); 32 } 33} 34 35@Entry 36@Component 37struct Index { 38 @State message: string = 'Hello World'; 39 40 build() { 41 Row() { 42 Column() { 43 Text(this.message) 44 .fontSize(50) 45 .fontWeight(FontWeight.Bold) 46 .onClick(() => { 47 concurrentFunc(); 48 // [StartExclude update_message_on_success] 49 this.message = 'success'; 50 // [EndExclude update_message_on_success] 51 }) 52 } 53 .width('100%') 54 } 55 .height('100%') 56 } 57} 58// [End concurrent_taskpool_common_usage] 59