鸿蒙 NEXT 开发中,普通对象跨线程如何传递
大家好,我是 V 哥,在鸿蒙HarmonyOS NEXT开发中,跨线程对象传递可以通过拷贝形式实现,确保两个线程的对象内容一致,但各自指向线程的隔离内存区间。以下是使用
SharedArrayBuffer
实现跨线程共享内存的完整案例代码,包括详细解释,整理的学习笔记,分享给大家。关注威哥不迷路,学习鸿蒙就很酷。
案例代码
1. 主线程代码
@Component export struct LockUsage { taskNum: number = 10; // 任务数,实际并行线程数依设备而定 baseDir: string = getContext().filesDir + '/TextDir'; // 文件写入的应用沙箱路径 sabInLock: SharedArrayBuffer = new SharedArrayBuffer(4); // 在主线程,初始化子线程锁标志位,所使用的共享内存 sabForLine: SharedArrayBuffer = new SharedArrayBuffer(4); // 在主线程,初始化子线程偏移位,所使用的共享内存 @State result: string = ""; build() { Row() { Column() { Button($r('app.string.not_use_lock')) .width("80%").fontSize(30) .fontWeight(FontWeight.Bold) .margin({ top: 30 }) .onClick(async () => { this.startWrite(false); }) Button($r('app.string.use_lock')) .width("80%") .fontSize(30) .fontWeight(FontWeight.Bold) .margin({ top: 30 }) .onClick(async () => { this.startWrite(true); }) Text(this.result) .width("80%") .fontSize(30) .fontWeight(FontWeight.Bold) .fontColor(Color.Blue) .margin({ top: 30 }) } .width('100%') } .height('100%') } startWrite(useLock: boolean): void { this.result = getContext().resourceManager.getStringSync($r('app.string.write_file_start')); let whichLineToWrite: Int32Array = new Int32Array(this.sabForLine); Atomics.store(whichLineToWrite, 0, 0); let taskPoolGroup: taskpool.TaskGroup = new taskpool.TaskGroup(); for (let i: number = 0; i < this.taskNum; i++) { taskPoolGroup.addTask(new taskpool.Task(createWriteTask, this.baseDir, i, this.sabInLock, this.sabForLine, useLock)); } taskpool.execute(taskPoolGroup).then(() => { this.result = getContext().resourceManager.getStringSync($r('app.string.write_file_success')); }).catch(() => { this.result = getContext().resourceManager.getStringSync($r('app.string.write_file_failed')); }) } }
2. 子线程代码
@Concurrent async function createWriteTask(baseDir: string, writeText: number, sabInLock: SharedArrayBuffer, sabForLine: SharedArrayBuffer, useLock: boolean): Promise<void> { class Option { offset: number = 0; length: number = 0; encoding: string = 'utf-8'; constructor(offset: number, length: number) { this.offset = offset; this.length = length; } } let filePath: string | undefined = undefined; filePath = baseDir + useLock ? "/useLock.txt" : "/unusedLock.txt"; if (!fs.accessSync(baseDir)) { fs.mkdirSync(baseDir); } let nrl: NonReentrantLock | undefined = undefined; if (useLock) { nrl = new NonReentrantLock(sabInLock); } let whichLineToWrite: Int32Array = new Int32Array(sabForLine); let str: string = writeText + '\n'; for (let i: number = 0; i < 100; i++) { if (useLock && nrl !== undefined) { nrl.lock(); } let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); try { fs.writeSync(file.fd, str, new Option(whichLineToWrite[0], str.length)); } catch (err) { logger.error(`errorCode : ${err.code},errMessage : ${err.message}`); } fs.closeSync(file); whichLineToWrite[0] += str.length; if (useLock && nrl !== undefined) { nrl.unlock(); } } }
详细解释
- 主线程初始化共享内存:
- 子线程写入文件:
- 线程间参数传递:
- 线程安全写入:
这个案例展示了如何在鸿蒙HarmonyOS NEXT开发中实现跨线程对象传递和共享内存,确保线程安全和数据一致性。通过使用 SharedArrayBuffer
和线程间参数传递,可以实现高效的并发编程。关注威哥爱编程,一起向鸿蒙出发。