1# SharedArrayBuffer对象 2<!--Kit: ArkTS--> 3<!--Subsystem: CommonLibrary--> 4<!--Owner: @wang_zhaoyong--> 5<!--Designer: @weng-changcheng--> 6<!--Tester: @kirl75; @zsw_zhushiwei--> 7<!--Adviser: @ge-yafang--> 8 9SharedArrayBuffer内部包含一块Native内存,其JS对象壳被分配在虚拟机本地堆(LocalHeap)。支持跨并发实例间共享Native内存,但是对共享Native内存的访问及修改需要采用Atomics类,防止数据竞争。SharedArrayBuffer可用于多个并发实例间的状态或数据共享。通信过程如下图所示: 10 11 12 13 14## 使用示例 15 16使用TaskPool传递Int32Array对象,实现如下: 17 18```ts 19import { taskpool } from '@kit.ArkTS'; 20 21@Concurrent 22function transferAtomics(arg1: Int32Array) { 23 console.info("wait begin::"); 24 // 使用Atomics进行操作 25 let res = Atomics.wait(arg1, 0, 0, 3000); 26 return res; 27} 28 29// 定义可共享对象 30let sab: SharedArrayBuffer = new SharedArrayBuffer(20); 31let int32 = new Int32Array(sab); 32let task: taskpool.Task = new taskpool.Task(transferAtomics, int32); 33taskpool.execute(task).then((res) => { 34 console.info("this res is: " + res); 35}); 36setTimeout(() => { 37 Atomics.notify(int32, 0, 1); 38}, 1000); 39``` 40<!-- @[example_pass_obj](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/CommunicationObjects/entry/src/main/ets/managers/SharedArrayBufferObject.ets) --> 41