此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
Atomics.notify()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2021年12月.
Atomics.notify() 静态方法唤醒一些在等待队列中休眠的代理。
备注:此操作仅适用于基于SharedArrayBuffer 的Int32Array 或BigInt64Array 视图。对于非共享的ArrayBuffer 对象,其将返回0。
In this article
语法
js
Atomics.notify(typedArray, index, count)参数
typedArrayindextypedArray中要唤醒的位置。count可选要唤醒的休眠代理的数量。默认为
Infinity。
返回值
- 返回唤醒的代理数量。
- 如果在非共享的
ArrayBuffer上调用则返回0。
异常
TypeError如果
typedArray不是一个基于SharedArrayBuffer的Int32Array或BigInt64Array,则抛出该异常。RangeError如果
index超出typedArray的范围,则抛出该异常。
示例
>使用 notify()
给定一个共享的Int32Array:
js
const sab = new SharedArrayBuffer(1024);const int32 = new Int32Array(sab);令一个读取线程休眠并在位置 0 处等待,预期该位置的值为 0。只要条件一直为真,则将不会继续运行。然而,一旦写入线程存储了一个新的值,它将被读取线程唤醒并返回新的值(123)。
js
Atomics.wait(int32, 0, 0);console.log(int32[0]); // 123写入线程存储一个新的值并在写入后唤醒等待的线程:
js
console.log(int32[0]); // 0;Atomics.store(int32, 0, 123);Atomics.notify(int32, 0, 1);规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-atomics.notify> |