Open In App

JavaScript Atomics notify() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The Atomics.notify() is an in-built method in JavaScript that is used to notify some agents that are sleeping in the wait queue. The Atomics.notify() method returns a number of woken-up agents. The integer-typed array, index, and count are passed as an argument to the function.

Syntax: 

Atomics.notify(typedArray, index, count)

Parameters: This method accepts three parameters as mentioned above and described below: 

  • typedarray: This parameter specifies a shared integer typed array Int16Array.
  • index: This parameter specifies the position in the array, typedArray to wait on.
  • count: This parameter counts the number of sleeping agents to notify. Its default value is +Infinity.

Return value: The Atomics.notify() method returns the number of woken-up agents.

The below programs illustrate the Atomics.notify() method in JavaScript:

Example 1: In this example, we will see the use of the Atomics.notify() method in JavaScript.

javascript




<script>
    const sab = new SharedArrayBuffer(1024);
    const int32 = new Int32Array(sab);
    int32[0] = 77; 
    console.log(int32[0]);
    console.log(Atomics.store(int32, 0, 123)); 
    console.log(Atomics.notify(int32, 0, 1)); 
</script>


Output: 

77
123
0

Example 2: In this example, we will see the use of the Atomics.notify() method in JavaScript.

javascript




<script>
    const sab = new SharedArrayBuffer(1024);
    const int32 = new Int32Array(sab);
    int32[0] = 77; 
    console.log(Atomics.load(int32, 66));
    console.log(int32[0]);
    console.log(Atomics.store(int32, 0, 123)); 
    console.log(Atomics.notify(int32, 6, 1));
    console.log(int32[0]);
</script>


Output: 

0
77
123
0
123

Exceptions: 

  • If the typedArray is not a shared Int32Array then the Atomics.notify( ) operation throws a TypeError.
  • If the index is used as an argument to the Atomics.notify() operation is out of the bound in the typedArray then the Atomics.store( ) operation throws a RangeError.

We have a complete list of Javascript Atomics methods, to check those please go through the Javascript Atomics Complete Reference article.

Supported Browser:

  • Google Chrome
  • Microsoft Edge
  • Firefox


Last Updated : 22 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads