| type | string | Yes| Event type.<br>- **add**, indicating the display addition event. Example: event that a display is connected.<br>- **remove**, indicating the display removal event. Example: event that a display is disconnected.<br>- **change**, indicating the display change event. Example: event that the display orientation is changed.|
| callback | Callback<number> | Yes| Callback used to return the ID of the display.|
| callback | Callback<number> | Yes| Callback used to return the ID of the display, which is an integer. |
**Example**
...
...
@@ -266,7 +266,7 @@ Unsubscribes from display changes.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type.<br>- **add**, indicating the display addition event. Example: event that a display is connected.<br>- **remove**, indicating the display removal event. Example: event that a display is disconnected.<br>- **change**, indicating the display change event. Example: event that the display orientation is changed.|
| callback | Callback<number> | No| Callback used to return the ID of the display.|
| callback | Callback<number> | No| Callback used to return the ID of the display, which is an integer.|
**Example**
...
...
@@ -409,21 +409,21 @@ Before calling any API in **Display**, you must use [getAllDisplays()](#displayg
| id | number | Yes| No| ID of the display. The value must be an integer. |
| name | string | Yes| No| Name of the display.|
| alive | boolean | Yes| No| Whether the display is alive.|
| state | [DisplayState](#displaystate) | Yes| No| State of the display.|
| refreshRate | number | Yes| No| Refresh rate of the display. The value must be an integer. |
| rotation | number | Yes| No| Screen rotation angle of the display.<br>The value **0** indicates that the screen of the display rotates by 0°.<br>The value **1** indicates that the screen of the display rotates by 90°.<br>The value **2** indicates that the screen of the display rotates by 180°.<br>The value **3** indicates that the screen of the display rotates by 270°.|
| width | number | Yes| No| Width of the display, in pixels.|
| height | number | Yes| No| Height of the display, in pixels.|
| densityDPI | number | Yes| No| Screen density of the display, that is, the number of dots per inch. Generally, the value is **160** or **480**.|
| densityPixels | number | Yes| No| Logical density of the display, which is a scaling coefficient independent of the pixel unit. Generally, the value is **1** or **3**.|
| scaledDensity | number | Yes| No| Scaling factor for fonts displayed on the display. Generally, the value is the same as that of **densityPixels**.|
| xDPI | number | Yes| No| Exact physical dots per inch of the screen in the horizontal direction.|
| yDPI | number | Yes| No| Exact physical dots per inch of the screen in the vertical direction.|
| width | number | Yes| No| Width of the display, in pixels. The value must be an integer. |
| height | number | Yes| No| Height of the display, in pixels. The value must be an integer. |
| densityDPI | number | Yes| No| Screen density of the display, that is, the number of dots per inch. The value must be a floating point number. Generally, the value is **160.0** or **480.0**. |
| densityPixels | number | Yes| No| Logical density of the display, which is a scaling coefficient independent of the pixel unit. The value must be a floating point number. Generally, the value is **1.0** or **3.0**. |
| scaledDensity | number | Yes| No| Scaling factor for fonts displayed on the display. The value must be a floating point number. Generally, the value is the same as that of **densityPixels**. |
| xDPI | number | Yes| No| Exact physical dots per inch of the screen in the horizontal direction. The value must be a floating point number. |
| yDPI | number | Yes| No| Exact physical dots per inch of the screen in the vertical direction. The value must be a floating point number. |
| eventType | string | Yes | Event type.<br>- **connect**: an event indicating that the screen is connected.<br>- **disconnect**: an event indicating that the screen is disconnected.<br>- **change**: an event indicating that the screen state changes.|
| callback | Callback<number> | Yes | Callback used to return the screen ID. |
| callback | Callback<number> | Yes | Callback used to return the screen ID, which is an integer. |
**Example**
...
...
@@ -126,7 +126,7 @@ Unsubscribes from events related to the screen state.
| eventType | string | Yes | Event type.<br>- **connect**: an event indicating that the screen is connected.<br>- **disconnect**: an event indicating that the screen is disconnected.<br>- **change**: an event indicating that the screen state changes.|
| callback | Callback<number> | No | Callback used to return the screen ID. |
| callback | Callback<number> | No | Callback used to return the screen ID, which is an integer. |
**Example**
...
...
@@ -152,9 +152,9 @@ Sets the screen to the expanded mode. This API uses an asynchronous callback to
| screenId | number | Yes | Screen ID. The value must be an integer. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the virtual screen is destroyed, **err** is **undefined**; otherwise, **err** is an error object.|
**Error codes**
...
...
@@ -464,7 +464,7 @@ Destroys a virtual screen. This API uses a promise to return the result.
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ---------- |
| screenId | number | Yes | Screen ID.|
| screenId | number | Yes | Screen ID. The value must be an integer.|
**Return value**
...
...
@@ -509,7 +509,7 @@ Sets the surface for a virtual screen. This API uses an asynchronous callback to
| screenId | number | Yes | Screen ID. The value must be an integer. |
| surfaceId | string | Yes | Surface ID. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the virtual screen surface is successfully set, **err** is **undefined**; otherwise, **err** is an error object.|
...
...
@@ -553,7 +553,7 @@ Sets the surface for a virtual screen. This API uses a promise to return the res
| Name | Type | Mandatory| Description |
| --------- | ------ | ---- | ------------- |
| screenId | number | Yes | Screen ID. |
| screenId | number | Yes | Screen ID. The value must be an integer. |
| surfaceId | string | Yes | Surface ID.|
**Return value**
...
...
@@ -711,9 +711,9 @@ Defines the parameters for expanding a screen.
| id | number | Yes | No | Screen ID. The value must be an integer. |
| parent | number | Yes | No | ID of the group to which a screen belongs. The value must be an integer. |
| supportedModeInfo | Array<[ScreenModeInfo](#screenmodeinfo)> | Yes | No | Mode set supported by the screen. |
| activeModeIndex | number | Yes | No | Index of the active screen mode. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware.|
| activeModeIndex | number | Yes | No | Index of the active screen mode. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware. The value must be an integer.|
| modeIndex | number | Yes | Index of the mode to set. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware.|
| modeIndex | number | Yes | Index of the mode to set. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware. The value must be an integer.|
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the active mode is successfully set, **err** is **undefined**; otherwise, **err** is an error object.|
**Error codes**
...
...
@@ -871,7 +871,7 @@ Sets the active mode of the screen. This API uses a promise to return the result
| Name | Type | Mandatory| Description |
| --------- | ------ | ---- | ---------- |
| modeIndex | number | Yes | Index of the mode to set. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware.|
| modeIndex | number | Yes | Index of the mode to set. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware. The value must be an integer.|
**Return value**
...
...
@@ -912,8 +912,8 @@ Sets the pixel density of the screen. This API uses an asynchronous callback to
| densityDpi | number | Yes | Pixel density. The value must be an integer in the range [80, 640]. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the pixel density is successfully set, **err** is **undefined**; otherwise, **err** is an error object.|
**Error codes**
...
...
@@ -950,8 +950,8 @@ Sets the pixel density of the screen. This API uses a promise to return the resu
| screenRect | [Rect](#rect) | No | Region of the screen to capture. If this parameter is null, the full screen will be captured. |
| imageSize | [Size](#size) | No | Size of the screen region to capture. If this parameter is null, the full screen will be captured. |
| rotation | number | No | Rotation angle of the screenshot. Currently, the value can be **0** only. The default value is **0**. |
| displayId<sup>8+</sup> | number | No | ID of the [display](js-apis-display.md#display) device on which the screen region is to be captured.|
| rotation | number | No | Rotation angle of the screenshot. Currently, the value can be **0** only. The default value is **0**. The value must be an integer. |
| displayId<sup>8+</sup> | number | No | ID of the [display](js-apis-display.md#display) device on which the screen region is to be captured. The value must be an integer.|
## Rect
...
...
@@ -37,10 +37,10 @@ Describes the region of the screen to capture.
Sends a message to the worker thread. The data type of the message must be sequenceable. For details about the sequenceable data types, see [More Information](#more-information).
Used by the host thread to send a message to the worker thread by transferring object ownership.
| message | Object | Yes | Message to be sent to the worker thread. |
| transfer | ArrayBuffer[] | Yes | An **ArrayBuffer** object can be transferred. The value **null** should not be passed in the array.|
| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the worker thread. After the transfer, the objects are available only in the worker thread. The array cannot be null.|
**Error codes**
...
...
@@ -181,7 +181,7 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
Sends a message to the worker thread. The data type of the message must be sequenceable. For details about the sequenceable data types, see [More Information](#more-information).
Used by the host thread to send a message to the worker thread by transferring object ownership or copying data.
| message | Object | Yes | Message to be sent to the worker thread. |
| options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The default value is **undefined**.|
| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the worker thread and becomes unavailable in the host thread. The objects are available only in the worker thread.<br>If this parameter is not specified, the default value **undefined** is used, and information is transferred to the worker thread by copying data.|
**Error codes**
...
...
@@ -214,7 +214,7 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
| message | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the host thread. After the transfer, the objects are available only in the host thread. The array cannot be null.|
**Error codes**
...
...
@@ -867,18 +867,18 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
| message | Object | Yes | Message to be sent to the host thread. |
| options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The default value is **undefined**.|
| message | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the host thread and becomes unavailable in the worker thread. The objects are available only in the host thread.<br>If this parameter is not specified, the default value **undefined** is used, and information is transferred to the host thread by copying data.|
**Error codes**
...
...
@@ -915,18 +915,18 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
Sends a message to the worker thread. The data type of the message must be sequenceable. For details about the sequenceable data types, see [More Information](#more-information).
Used by the host thread to send a message to the worker thread by transferring object ownership.
> **NOTE**<br>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.postMessage<sup>9+</sup>](#postmessage9) instead.
...
...
@@ -1268,14 +1268,14 @@ Sends a message to the worker thread. The data type of the message must be seque
| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the worker thread. After the transfer, the objects are available only in the worker thread. The array cannot be null.|
Sends a message to the worker thread. The data type of the message must be sequenceable. For details about the sequenceable data types, see [More Information](#more-information).
Used by the host thread to send a message to the worker thread by transferring object ownership or copying data.
> **NOTE**<br>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.postMessage<sup>9+</sup>](#postmessage9-1) instead.
...
...
@@ -1296,13 +1296,13 @@ Sends a message to the worker thread. The data type of the message must be seque
| message | Object | Yes | Message to be sent to the worker thread. |
| options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The default value is **undefined**.|
| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the worker thread and becomes unavailable in the host thread. The objects are available only in the worker thread.<br>If this parameter is not specified, the default value **undefined** is used, and information is transferred to the worker thread by copying data.|
| messageObject | Object | Yes | Message to be sent to the host thread. |
| messageObject | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
| transfer| Transferable[] | Yes | Currently, this parameter is not supported. |
| message | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the host thread. After the transfer, the objects are available only in the host thread. The array cannot be null.|
Sends a message to the host thread from the worker thread.
Used by the worker thread to send a message to the host thread by transferring object ownership or copying data.
> **NOTE**<br>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope<sup>9+</sup>.postMessage<sup>9+</sup>](#postmessage9-3).
...
...
@@ -1767,28 +1767,28 @@ Sends a message to the host thread from the worker thread.
| message | Object | Yes | Message to be sent to the host thread. |
| options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The default value is **undefined**.|
| message | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).|
| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the host thread and becomes unavailable in the worker thread. The objects are available only in the host thread.<br>If this parameter is not specified, the default value **undefined** is used, and information is transferred to the host thread by copying data.|
Specifies the object whose ownership needs to be transferred during data transfer. The object must be **ArrayBuffer**.
Defines the object for which the ownership is to be transferred during data transfer. The object must be an **ArrayBuffer** instance. After the ownership is transferred, the object becomes unavailable in the sender and can be used only in the receiver.
workerPort.postMessage(obj2);// No serialization error occurs when passing obj2.
}
workerPort.onmessageerror=function(e){
console.log("worker.js onmessageerror");
console.log("worker.ts onmessageerror");
}
workerPort.onerror=function(e){
console.log("worker.js onerror");
console.log("worker.ts onerror");
}
```
### Memory Model
The worker thread is implemented based on the actor model. In the worker interaction process, the JS main thread can create multiple worker threads, each of which are isolated and transfer data through sequentialization. They complete computing tasks and return the result to the main thread.
The worker thread is implemented based on the actor model. In the worker interaction process, the JS main thread can create multiple worker threads, each of which are isolated and transfer data through serialization. They complete computing tasks and return the result to the main thread.
Each actor concurrently processes tasks of the main thread. For each actor, there is a message queue and a single-thread execution module. The message queue receives requests from the main thread and other actors; the single-thread execution module serially processes requests, sends requests to other actors, and creates new actors. These isolated actors use the asynchronous mode and can run concurrently.
### Precautions
- Currently, a maximum of seven workers can co-exist.
- Currently, a maximum of eight workers can co-exist.
- In API version 8 and earlier versions, when the number of **Worker** instances exceeds the upper limit, the error "Too many workers, the number of workers exceeds the maximum." is thrown.
- Since API version 9, when the number of **Worker** instances exceeds the upper limit, the business error "Worker initialization failure, the number of workers exceeds the maximum" is thrown.
- Since API version 9, when the number of **Worker** instances exceeds the upper limit, the business error "Worker initialization failure, the number of workers exceeds the maximum." is thrown.
- To proactively destroy a worker thread, you can call **terminate()** or **parentPort.close()** of the newly created **Worker** instance.
- Since API version 9, if a **Worker** instance in a non-running state (such as destroyed or being destroyed) calls an API, a business error is thrown.
- Creating and terminating worker threads consume performance. Therefore, you are advised to manage available workers and reuse them.
- Do not use both **new worker.Worker** and **new worker.ThreadWorker** to create a **Worker** project. Otherwise, **Worker** functions abnormally. Since API version 9, you are advised to use [new worker.ThreadWorker](#constructor9). In API version 8 and earlier versions, you are advised to use [new worker.Worker](#constructordeprecated).
- When creating a **Worker** project, do not import any UI construction method (such as .ets file) to the worker thread file (for example, **worker.ts** used in this document). Otherwise, the worker module becomes invalid. To check whether any UI construction method has been imported, decompress the generated HAP file, find **worker.js** in the directory where the worker thread is created, and search for the keyword **View** globally. If the keyword exists, a UI construction method has been packaged in **worker.js**. If this is your case, change the directory level of **src** in the statement **import "xxx" from src** in the worker thread file.
- A maximum of 16 MB data can be transferred during inter-thread communication.
## Sample Code
> **NOTE**<br>
...
...
@@ -2111,15 +2112,10 @@ Each actor concurrently processes tasks of the main thread. For each actor, ther
### FA Model
```js
// main.js (The following assumes that the workers directory and pages directory are at the same level.)
// Main thread (The following assumes that the workers directory and pages directory are at the same level.)