> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A Worker thread is an independent thread running in parallel with the main thread. The thread that creates the Worker thread is referred to as a host thread. The URL file passed in during Worker creation is executed in the Worker thread. The Worker thread can process time-consuming operations, but cannot directly operate the UI.
| parentPort | [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscope) | Yes| Yes| Object of the worker thread used to communicate with the host thread.|
| parentPort | [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscope) | Yes | Yes | Object of the Worker thread used to communicate with the host thread.|
## WorkerOptions
Provides options that can be set for the worker to create.
Provides options that can be set for the **Worker** instance to create.
| scriptURL | string | Yes| URL of the script to be executed by the worker. The script is stored in the **workers** directory, which is in the same directory as the **pages** directory of the new DevEco Studio project. If the **workers** directory does not exist, you need to create it.|
| options | [WorkerOptions](#workeroptions) | No| Options that can be set for the worker.|
| scriptURL | string | Yes | URL of the script to be executed by the Worker thread. The script is stored in the **workers** directory, which is in the same directory as the **pages** directory of the new DevEco Studio project. If the **workers** directory does not exist, you need to create it.|
| options | [WorkerOptions](#workeroptions) | No | Options that can be set for the **Worker** instance. |
**Return value**
| Type| Description|
| -------- | -------- |
| worker | Returns the **Worker** instance created; returns **undefined** if the **Worker** instance fails to be created.|
| message | Object | Yes| Data to be sent to the worker.|
| options | [PostMessageOptions](#postmessageoptions) | No| **ArrayBuffer** instances that can be transferred. The **transferList** array cannot contain **null**.|
| message | Object | Yes | Data to be sent to the Worker thread. |
| options | [PostMessageOptions](#postmessageoptions) | No| **ArrayBuffer** instances that can be transferred. The **transferList** array cannot contain **null**.|
**Example**
...
...
@@ -81,6 +87,7 @@ Sends a message to the worker thread. The data is transferred using the structur
const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.postMessage("hello world");
```
```
const workerInstance= new worker.Worker("workers/worker.js");
Defines the event handler to be called when the host thread receives a message created by itself and sent by the worker through the **parentPort.postMessage**. The event handler is executed in the host thread.
Defines the event handler to be called when the host thread receives a message created by itself and sent by the Worker thread through the **parentPort.postMessage**. The event handler is executed in the host thread.
Defines the event handler to be called when the worker receives a message that cannot be serialized. The event handler is executed in the host thread.
Defines the event handler to be called when the Worker thread receives a message that cannot be serialized. The event handler is executed in the host thread.
Implements communication between the worker thread and the host thread. The **postMessage** API is used to send messages to the host thread, and the **close** API is used to terminate the worker thread. The **DedicatedWorkerGlobalScope** class inherits from [WorkerGlobalScope](#workerglobalscope).
Implements communication between the Worker thread and the host thread. The **postMessage** API is used to send messages to the host thread, and the **close** API is used to terminate the Worker thread. The **DedicatedWorkerGlobalScope** class inherits from [WorkerGlobalScope](#workerglobalscope).
| message | Object | Yes| Data to be sent to the worker.|
| options | [PostMessageOptions](#postmessageoptions) | No| **ArrayBuffer** instances that can be transferred. The **transferList** array cannot contain **null**.|
| message | Object | Yes | Data to be sent to the Worker thread. |
| options | [PostMessageOptions](#postmessageoptions) | No| **ArrayBuffer** instances that can be transferred. The **transferList** array cannot contain **null**.|
Defines the event handler to be called when the worker thread receives a message sent by the host thread through **worker.postMessage**. The event handler is executed in the worker thread.
Defines the event handler to be called when the Worker thread receives a message sent by the host thread through **worker.postMessage**. The event handler is executed in the Worker thread.
Defines the event handler to be called when the worker receives a message that cannot be deserialized. The event handler is executed in the worker thread.
Defines the event handler to be called when the Worker thread receives a message that cannot be deserialized. The event handler is executed in the Worker thread.