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'; 18 19@Concurrent 20function adjustImageValue(arrayBuffer: ArrayBuffer): ArrayBuffer { 21 // 对arrayBuffer进行操作 22 return arrayBuffer; // 返回值默认转移 23} 24 25function createImageTask(arrayBuffer: ArrayBuffer, isParamsByTransfer: boolean): taskpool.Task { 26 let task: taskpool.Task = new taskpool.Task(adjustImageValue, arrayBuffer); 27 if (!isParamsByTransfer) { // 是否使用转移方式 28 // 传递空数组[],全部arrayBuffer参数传递均采用拷贝方式 29 task.setTransferList([]); 30 } 31 return task; 32} 33 34@Entry 35@Component 36struct Index { 37 @State message: string = 'Hello World'; 38 39 build() { 40 RelativeContainer() { 41 Text(this.message) 42 .id('HelloWorld') 43 .fontSize(50) 44 .fontWeight(FontWeight.Bold) 45 .alignRules({ 46 center: { anchor: '__container__', align: VerticalAlign.Center }, 47 middle: { anchor: '__container__', align: HorizontalAlign.Center } 48 }) 49 .onClick(() => { 50 let taskNum = 4; 51 let arrayBuffer = new ArrayBuffer(1024 * 1024); 52 let taskPoolGroup = new taskpool.TaskGroup(); 53 // 创建taskNum个Task 54 for (let i: number = 0; i < taskNum; i++) { 55 let arrayBufferSlice: ArrayBuffer = 56 arrayBuffer.slice(arrayBuffer.byteLength / taskNum * i, arrayBuffer.byteLength / taskNum * (i + 1)); 57 // 使用拷贝方式传入ArrayBuffer,所以isParamsByTransfer为false 58 taskPoolGroup.addTask(createImageTask(arrayBufferSlice, false)); 59 } 60 // 执行Task 61 taskpool.execute(taskPoolGroup).then((data) => { 62 // 返回结果,对数组拼接,获得最终结果 63 }).catch((e: BusinessError) => { 64 console.error(e.message); 65 }) 66 this.message = 'success'; 67 }) 68 } 69 .height('100%') 70 .width('100%') 71 } 72} 73