# @ohos.worker (Starting the Worker)
The Worker thread is an independent thread running in parallel with the main thread. The thread that creates the Worker thread is referred to as the 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.
With the Worker module, you can provide a multithreaded environment for an application, so that the application can perform a time-consuming operation in a background thread. This greatly prevents a computing-intensive or high-latency task from blocking the running of the host thread. A Worker instance will not be proactively destroyed once it is created. It consumes resources to keep running. Therefore, you should call the API to terminate it in a timely manner.
The Context object of the Worker thread is different from that of the UI main thread. The Worker thread does not support UI operations.
For details about the precautions for using Worker, see [Precautions for Worker](../../arkts-utils/worker-introduction.md#precautions-for-worker).
> **NOTE**
>
> 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.
## Modules to Import
```ts
import { worker } from '@kit.ArkTS';
```
## Attributes
**System capability**: SystemCapability.Utils.Lang
| Name | Type | Readable| Writable| Description |
| --------------------------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ |
| workerPort9+ | [ThreadWorkerGlobalScope](#threadworkerglobalscope9) | Yes | Yes | Object of the Worker thread used to communicate with the host thread. **Atomic service API**: This API can be used in atomic services since API version 11. |
| parentPort(deprecated) | [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscopedeprecated) | Yes | Yes | Object of the Worker thread used to communicate with the host thread.
This attribute is supported since API version 7 and deprecated since API version 9.
You are advised to use **workerPort9+** instead.|
## WorkerOptions
Provides options that can be set for the Worker instance to create.
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Read-only| Optional| Description|
| ---- | -------- | ---- | ---- | -------------- |
| type | 'classic' \| 'module' | Yes | Yes| Mode in which the Worker instance executes the script. The **module** type is not supported yet. The default value is **classic**.
**Atomic service API**: This API can be used in atomic services since API version 11.|
| name | string | Yes | Yes| Name of the Worker thread. The default value is **undefined**.
**Atomic service API**: This API can be used in atomic services since API version 11.|
| shared | boolean | Yes | Yes| Whether sharing of the Worker instance is enabled. Currently, sharing is not supported.
**Atomic service API**: This API can be used in atomic services since API version 12.|
## ThreadWorker9+
Before using the following APIs, you must create a ThreadWorker instance. The ThreadWorker class inherits from [WorkerEventTarget](#workereventtarget9).
### constructor9+
constructor(scriptURL: string, options?: WorkerOptions)
A constructor used to create a ThreadWorker instance.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| --------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| scriptURL | string | Yes | URL of the Worker thread file.
For details about the rules, see [Precautions for File URLs](../../arkts-utils/worker-introduction.md#precautions-for-file-urls).|
| options | [WorkerOptions](#workeroptions) | No | Options that can be set for the Worker instance. |
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message|
| -------- | -------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200003 | Worker initialization failed. |
| 10200007 | The worker file path is invalid. |
**Example**
The following code snippet shows how to load the Worker thread file of the ability in the stage model. For details about how to use the library to load the Worker thread file, see [Precautions for File URLs](../../arkts-utils/worker-introduction.md#precautions-for-file-urls).
```ts
import { worker } from '@kit.ArkTS';
// Two scenarios are involved.
// Scenario 1: URL of the Worker thread file: "entry/src/main/ets/workers/worker.ets"
const workerStageModel01 = new worker.ThreadWorker('entry/ets/workers/worker.ets', {name:"first worker in Stage model"});
// Scenario 2: URL of the Worker thread file: "phone/src/main/ets/ThreadFile/workers/worker.ets"
const workerStageModel02 = new worker.ThreadWorker('phone/ets/ThreadFile/workers/worker.ets');
```
### postMessage9+
postMessage(message: Object, transfer: ArrayBuffer[]): void
Sends a message from the host thread to the Worker thread by transferring object ownership.
**System capability**: SystemCapability.Utils.Lang
**Atomic service API**: This API can be used in atomic services since API version 11.
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
| 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**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ----------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | The Worker instance is not running. |
| 10200006 | An exception occurred during serialization. |
**Example**
```ts
// Worker.ets
import { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS';
// Create an object in the Worker thread for communicating with the host thread.
const workerPort = worker.workerPort
// The Worker thread receives information from the host thread.
workerPort.onmessage = (e: MessageEvents): void => {
// data carries the information sent by the host thread.
let data: number = e.data;
// Write data to the received buffer.
const view = new Int8Array(data).fill(3);
// The Worker thread sends information to the host thread.
workerPort.postMessage(view);
}
// Trigger a callback when an error occurs in the Worker thread.
workerPort.onerror = (err: ErrorEvent) => {
console.log("worker.ets onerror" + err.message);
}
```
```ts
// Index.ets
import { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
// Create a Worker instance in the host thread.
const workerInstance = new worker.ThreadWorker("entry/ets/workers/Worker.ets");
// The host thread transfers information to the Worker thread.
const buffer = new ArrayBuffer(8);
workerInstance.postMessage(buffer, [buffer]);
// The host thread receives information from the Worker thread.
workerInstance.onmessage = (e: MessageEvents): void => {
// data carries the information sent by the Worker thread.
let data: number = e.data;
console.info("main thread data is " + data);
// Terminate the Worker instance.
workerInstance.terminate();
}
// Call onexit().
workerInstance.onexit = (code) => {
console.log("main thread terminate");
}
workerInstance.onerror = (err: ErrorEvent) => {
console.log("main error message " + err.message);
}
})
}
.width('100%')
.height('100%')
}
}
}
```
### postMessage9+
postMessage(message: Object, options?: PostMessageOptions): void
Sends a message from the host thread to the Worker thread by transferring object ownership or copying data.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| 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.
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**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ----------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | The Worker instance is not running. |
| 10200006 | An exception occurred during serialization. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.postMessage("hello world");
let buffer = new ArrayBuffer(8);
workerInstance.postMessage(buffer, [buffer]);
```
### postMessageWithSharedSendable12+
postMessageWithSharedSendable(message: Object, transfer?: ArrayBuffer[]): void
Sends a message from the host thread to the Worker thread. In the message, a sendable object is passed by reference, and a non-sendable object is passed by serialization.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| --------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| message | Object | Yes | Data to be sent to the Worker thread. The data object must be sequenceable or sendable. For details about the supported sequenceable types, see [Sequenceable Data Types](#sequenceable-data-types). For details about the supported sendable types, see [Sendable Data Types](../../arkts-utils/arkts-sendable.md#sendable-data-types).|
| transfer | ArrayBuffer[] | No | 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. The default value is an empty array.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ----------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | The Worker instance is not running. |
| 10200006 | An exception occurred during serialization. |
**Example**
```ts
// index.ets
// Create a SendableObject instance and pass it to the Worker thread through the host thread.
import { worker } from '@kit.ArkTS';
import { SendableObject } from './sendable'
const workerInstance = new worker.ThreadWorker("entry/ets/workers/Worker.ets");
let object: SendableObject = new SendableObject();
workerInstance.postMessageWithSharedSendable(object);
```
```ts
// sendable.ets
// Define SendableObject.
@Sendable
export class SendableObject {
a:number = 45;
}
```
```ts
// The worker file path is entry/src/main/ets/workers/Worker.ets.
// Worker.ets
// Receive and access the data passed from the host thread to the Worker thread.
import { SendableObject } from '../pages/sendable'
import { worker, ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@kit.ArkTS';
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
workerPort.onmessage = (e: MessageEvents) => {
let obj: SendableObject = e.data;
console.info("sendable obj is: " + obj.a);
}
```
### on9+
on(type: string, listener: WorkerEventListener): void
Adds an event listener for the Worker thread. This API provides the same functionality as [addEventListener9+](#addeventlistener9).
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------------------------------- | ---- | ---------------------- |
| type | string | Yes | Type of the event to listen for. |
| listener | [WorkerEventListener](#workereventlistener9) | Yes| Callback to invoke when an event of the specified type occurs.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | Worker instance is not running. |
| 10200005 | The invoked API is not supported in workers. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.on("alert", ()=>{
console.log("alert listener callback");
})
```
### once9+
once(type: string, listener: WorkerEventListener): void
Adds an event listener for the Worker thread and removes the event listener after it is invoked once.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------------------------------- | ---- | ---------------------- |
| type | string | Yes | Type of the event to listen for. |
| listener | [WorkerEventListener](#workereventlistener9) | Yes| Callback to invoke when an event of the specified type occurs.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | Worker instance is not running. |
| 10200005 | The invoked API is not supported in workers. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.once("alert", ()=>{
console.log("alert listener callback");
})
```
### off9+
off(type: string, listener?: WorkerEventListener): void
Removes an event listener for the Worker thread. This API provides the same functionality as [removeEventListener9+](#removeeventlistener9).
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------------------------------- | ---- | ---------------------------- |
| type | string | Yes | Type of the event for which the event listener is to be removed. |
| listener | [WorkerEventListener](#workereventlistener9) | No| Callback to invoke when the listener is removed.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | Worker instance is not running. |
| 10200005 | The invoked API is not supported in workers. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
// Use on, once, or addEventListener to add a listener for the "alert" event, and use off to remove the listener.
workerInstance.off("alert");
```
### registerGlobalCallObject11+
registerGlobalCallObject(instanceName: string, globalCallObject: Object): void
Registers an object with the ThreadWorker instance of the host thread. In this way, the methods of the object can be called in the Worker thread through [callGlobalCallObjectMethod](#callglobalcallobjectmethod11).
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
| instanceName | string | Yes | Key used for registration, based on which the registered object is identified during method calling.|
| globalCallObject | Object | Yes | Object to register. The ThreadWorker instance holds a strong reference to the object.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ----------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | Worker instance is not running. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
class TestObj {
private message : string = "this is a message from TestObj"
public getMessage() : string {
return this.message;
}
public getMessageWithInput(str : string) : string {
return this.message + " with input: " + str;
}
}
let registerObj = new TestObj();
// Register registerObj with the ThreadWorker instance.
workerInstance.registerGlobalCallObject("myObj", registerObj);
workerInstance.postMessage("start worker")
```
### unregisterGlobalCallObject11+
unregisterGlobalCallObject(instanceName?: string): void
Unregisters an object with the ThreadWorker instance of the host thread. This API releases the strong reference between the ThreadWorker instance and the target object. No error is reported if no object is matched.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
| instanceName | string | No | Key used for registration. If this parameter is left blank, all registered objects registered in the ThreadWorker instance are unregistered.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ----------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | Worker instance is not running. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
class TestObj {
private message : string = "this is a message from TestObj"
public getMessage() : string {
return this.message;
}
public getMessageWithInput(str : string) : string {
return this.message + " with input: " + str;
}
}
let registerObj = new TestObj();
workerInstance.registerGlobalCallObject("myObj", registerObj);
// Unregister the object.
workerInstance.unregisterGlobalCallObject("myObj");
// Unregister all objects from the ThreadWorker instance.
//workerInstance.unregisterGlobalCallObject();
workerInstance.postMessage("start worker")
```
### terminate9+
terminate(): void
Terminates the Worker thread to stop it from receiving messages.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Error codes**
For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------- |
| 10200004 | The Worker instance is not running. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.terminate();
```
### onexit9+
onexit?: (code: number) => void
Called when the Worker thread exits. The event handler is executed in the host thread. In the callback function, the **code** value is of the number type, where the value **1** indicates abnormal exit and **0** indicates normal exit.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
| 10200004 | The Worker instance is not running. |
| 10200005 | The called API is not supported in the worker thread. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.onexit = (code) => {
console.log("onexit");
}
// onexit is executed in either of the following ways:
// Main thread
workerInstance.terminate();
// Worker thread
//workerPort.close()
```
### onerror9+
onerror?: (err: ErrorEvent) => void
Called when an exception occurs during worker execution. The event handler is executed in the host thread. In the callback function, the **err** type is [ErrorEvent](#errorevent), indicating the received abnormal data.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
| 10200004 | The Worker instance is not running. |
| 10200005 | The called API is not supported in the worker thread. |
**Example**
```ts
import { worker, ErrorEvent } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.onerror = (err: ErrorEvent) => {
console.log("onerror" + err.message);
}
```
### onmessage9+
onmessage?: (event: MessageEvents) => void
Called when the host thread receives a message sent by the Worker thread through **workerPort.postMessage**. The event handler is executed in the host thread. In the callback function, the **event** type is [MessageEvents](#messageevents9), indicating the received message data.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
| 10200004 | The Worker instance is not running. |
| 10200005 | The called API is not supported in the worker thread. |
**Example**
```ts
import { worker, MessageEvents } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.onmessage = (e: MessageEvents): void => {
// e: MessageEvents. The usage is as follows:
// let data = e.data;
console.log("onmessage");
}
```
### onmessageerror9+
onmessageerror?: (event: MessageEvents) => void
Called when the Worker thread receives a message that cannot be serialized. The event handler is executed in the host thread. In the callback function, the **event** type is [MessageEvents](#messageevents9), indicating the received message data.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
| 10200004 | The Worker instance is not running. |
| 10200005 | The called API is not supported in the worker thread. |
**Example**
```ts
import { worker, MessageEvents } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.onmessageerror = (err: MessageEvents) => {
console.log("onmessageerror");
}
```
### addEventListener9+
addEventListener(type: string, listener: WorkerEventListener): void
Adds an event listener for the Worker thread. This API provides the same functionality as [on9+](#on9).
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------------------------------- | ---- | ---------------- |
| type | string | Yes | Type of the event to listen for.|
| listener | [WorkerEventListener](#workereventlistener9) | Yes | Callback to invoke when an event of the specified type occurs. |
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | Worker instance is not running. |
| 10200005 | The invoked API is not supported in workers. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
```
### removeEventListener9+
removeEventListener(type: string, callback?: WorkerEventListener): void
Removes an event listener for the Worker thread. This API provides the same functionality as [off9+](#off9).
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------------------------------- | ---- | ---------------------------- |
| type | string | Yes | Type of the event for which the event listener is to be removed. |
| callback | [WorkerEventListener](#workereventlistener9) | No| Callback to invoke when the listener is removed.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | Worker instance is not running. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
workerInstance.removeEventListener("alert");
```
### dispatchEvent9+
dispatchEvent(event: Event): boolean
Dispatches the event defined for the Worker thread.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | --------------- | ---- | ---------------- |
| event | [Event](#event) | Yes | Event to dispatch.|
**Return value**
| Type | Description |
| ------- | ------------------------------- |
| boolean | Returns **true** if the event is dispatched; returns **false** otherwise.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | Worker instance is not running. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is not supported yet.
```
The **dispatchEvent** API can be used together with the **on**, **once**, and **addEventListener** APIs. The sample code is as follows:
```ts
import { worker, MessageEvents } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
// Usage 1:
workerInstance.on("alert_on", ()=>{
console.log("alert listener callback");
})
workerInstance.once("alert_once", ()=>{
console.log("alert listener callback");
})
workerInstance.addEventListener("alert_add", ()=>{
console.log("alert listener callback");
})
// The event listener created by once is removed after being executed once.
workerInstance.dispatchEvent({type:"alert_once", timeStamp:0}); // timeStamp is not supported yet.
// The event listener created by on will not be proactively deleted.
workerInstance.dispatchEvent({type:"alert_on", timeStamp:0});
workerInstance.dispatchEvent({type:"alert_on", timeStamp:0});
// The event listener created by addEventListener will not be proactively deleted.
workerInstance.dispatchEvent({type:"alert_add", timeStamp:0});
workerInstance.dispatchEvent({type:"alert_add", timeStamp:0});
// Usage 2:
// The event type can be customized, and the special types "message", "messageerror", and "error" exist.
// When type = "message", the event handler defined by onmessage will also be executed.
// When type = "messageerror", the event handler defined by onmessageerror will also be executed.
// When type = "error", the event handler defined by onerror will also be executed.
// removeEventListener or off can be used to remove an event listener that is created by addEventListener, on, or once.
workerInstance.addEventListener("message", ()=>{
console.log("message listener callback");
})
workerInstance.onmessage = (e: MessageEvents): void => {
console.log("onmessage : message listener callback");
}
// When dispatchEvent is called to distribute the "message" event, the callback passed in addEventListener and onmessage will be invoked.
workerInstance.dispatchEvent({type:"message", timeStamp:0});
```
### removeAllListener9+
removeAllListener(): void
Removes all event listeners for the Worker thread.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Error codes**
For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------- |
| 10200004 | Worker instance is not running. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
workerInstance.removeAllListener();
```
## WorkerEventTarget9+
Processes worker listening events.
### addEventListener9+
addEventListener(type: string, listener: WorkerEventListener): void
Adds an event listener for the Worker thread. This API provides the same functionality as [on9+](#on9).
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------------------------------- | ---- | ---------------- |
| type | string | Yes | Type of the event to listen for.|
| listener | [WorkerEventListener](#workereventlistener9) | Yes | Callback to invoke when an event of the specified type occurs. |
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | The Worker instance is not running. |
| 10200005 | The called API is not supported in the worker thread. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
```
### removeEventListener9+
removeEventListener(type: string, callback?: WorkerEventListener): void
Removes an event listener for the Worker thread. This API provides the same functionality as [off9+](#off9).
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------------------------------- | ---- | ---------------------------- |
| type | string | Yes | Type of the event for which the event listener is to be removed. |
| callback | [WorkerEventListener](#workereventlistener9) | No| Callback to invoke when the listener is removed.|
**Error codes**
For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | The Worker instance is not running. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
workerInstance.removeEventListener("alert");
```
### dispatchEvent9+
dispatchEvent(event: Event): boolean
Dispatches the event defined for the Worker thread.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | --------------- | ---- | ---------------- |
| event | [Event](#event) | Yes | Event to dispatch.|
**Return value**
| Type | Description |
| ------- | ------------------------------- |
| boolean | Returns **true** if the event is dispatched successfully; returns **false** otherwise.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | The Worker instance is not running. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is not supported yet.
```
The **dispatchEvent** API can be used together with the **on**, **once**, and **addEventListener** APIs. The sample code is as follows:
```ts
import { worker, MessageEvents } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
// Usage 1:
workerInstance.on("alert_on", ()=>{
console.log("alert listener callback");
})
workerInstance.once("alert_once", ()=>{
console.log("alert listener callback");
})
workerInstance.addEventListener("alert_add", ()=>{
console.log("alert listener callback");
})
// The event listener created by once is removed after being executed once.
workerInstance.dispatchEvent({type:"alert_once", timeStamp:0}); // timeStamp is not supported yet.
// The event listener created by on will not be proactively deleted.
workerInstance.dispatchEvent({type:"alert_on", timeStamp:0});
workerInstance.dispatchEvent({type:"alert_on", timeStamp:0});
// The event listener created by addEventListener will not be proactively deleted.
workerInstance.dispatchEvent({type:"alert_add", timeStamp:0});
workerInstance.dispatchEvent({type:"alert_add", timeStamp:0});
// Usage 2:
// The event type can be customized, and the special types "message", "messageerror", and "error" exist.
// When type = "message", the event handler defined by onmessage will also be executed.
// When type = "messageerror", the event handler defined by onmessageerror will also be executed.
// When type = "error", the event handler defined by onerror will also be executed.
// removeEventListener or off can be used to remove an event listener that is created by addEventListener, on, or once.
workerInstance.addEventListener("message", ()=>{
console.log("message listener callback");
})
workerInstance.onmessage = (e: MessageEvents): void => {
console.log("onmessage : message listener callback");
}
// When dispatchEvent is called to distribute the "message" event, the callback passed in addEventListener and onmessage will be invoked.
workerInstance.dispatchEvent({type:"message", timeStamp:0});
```
### removeAllListener9+
removeAllListener(): void
Removes all event listeners for the Worker thread.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Error codes**
For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------- |
| 10200004 | The Worker instance is not running. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
workerInstance.removeAllListener();
```
## ThreadWorkerGlobalScope9+
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 **ThreadWorkerGlobalScope** class inherits from [GlobalScope9+](#globalscope9).
### postMessage9+
postMessage(messageObject: Object, transfer: ArrayBuffer[]): void;
Sends a message from the Worker thread to the host thread by transferring object ownership.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
| 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 | 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**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ----------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | The Worker instance is not running. |
| 10200006 | An exception occurred during serialization. |
**Example**
```ts
// Main thread
import { worker, MessageEvents } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.postMessage("hello world");
workerInstance.onmessage = (e: MessageEvents): void => {
console.log("receive data from worker.ets");
}
```
```ts
// worker.ets
import { worker, MessageEvents } from '@kit.ArkTS';
const workerPort = worker.workerPort;
workerPort.onmessage = (e: MessageEvents): void => {
let buffer = new ArrayBuffer(8);
workerPort.postMessage(buffer, [buffer]);
}
```
### postMessage9+
postMessage(messageObject: Object, options?: PostMessageOptions): void
Sends a message from the Worker thread to the host thread by transferring object ownership or copying data.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| 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).|
| 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.
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**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ----------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | The Worker instance is not running. |
| 10200006 | An exception occurred during serialization. |
**Example**
```ts
// Main thread
import { worker, MessageEvents } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.postMessage("hello world");
workerInstance.onmessage = (e: MessageEvents): void => {
console.log("receive data from worker.ets");
}
```
```ts
// worker.ets
import { worker, MessageEvents } from '@kit.ArkTS';
const workerPort = worker.workerPort;
workerPort.onmessage = (e: MessageEvents): void => {
workerPort.postMessage("receive data from main thread");
}
```
### postMessageWithSharedSendable12+
postMessageWithSharedSendable(message: Object, transfer?: ArrayBuffer[]): void
Sends a message from the Worker thread to the host thread. In the message, a sendable object is passed by reference, and a non-sendable object is passed by serialization.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| --------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| message | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable or sendable. For details about the supported sequenceable types, see [Sequenceable Data Types](#sequenceable-data-types). For details about the supported sendable types, see [Sendable Data Types](../../arkts-utils/arkts-sendable.md#sendable-data-types).|
| transfer | ArrayBuffer[] | No | 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. The default value is an empty array.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ----------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | The Worker instance is not running. |
| 10200006 | An exception occurred during serialization. |
**Example**
```ts
// The worker file path is entry/src/main/ets/workers/Worker.ets.
// Worker.ets
// Create a SendableObject instance and pass it to the host thread through the Worker thread.
import { SendableObject } from '../pages/sendable'
import { worker, ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@kit.ArkTS';
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
workerPort.onmessage = (e: MessageEvents) => {
let object: SendableObject = new SendableObject();
workerPort.postMessageWithSharedSendable(object);
}
```
```ts
// sendable.ets
// Define SendableObject.
@Sendable
export class SendableObject {
a:number = 45;
}
```
```ts
// Index.ets
// Receive the data passed from the Worker thread to the host thread and access its properties.
import { worker, MessageEvents } from '@kit.ArkTS';
import { SendableObject } from './sendable'
const workerInstance = new worker.ThreadWorker("entry/ets/workers/Worker.ets");
workerInstance.postMessage(1);
workerInstance.onmessage = (e: MessageEvents) => {
let obj: SendableObject = e.data;
console.info("sendable index obj is: " + obj.a);
}
```
### callGlobalCallObjectMethod11+
callGlobalCallObjectMethod(instanceName: string, methodName: string, timeout: number, ...args: Object[]): Object
Calls a method of an object registered with the host thread. This API is called by the Worker thread. The invoking is synchronous for the Worker thread and asynchronous for the host thread. The return value is transferred through serialization.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| instanceName | string | Yes | Key used for registration. It is used to search for the object in the host thread.|
| methodName | string | Yes| Name of the method to call. Note that the method cannot be modified by async or generator, or return results asynchronously by using the asynchronous mechanism at the bottom layer. Otherwise, an exception is thrown.|
| timeout | number | Yes| Maximum duration that the current synchronous invoking waits, in ms. The value is an integer ranging from 1 to 5000. The value **0** means that the 5000 ms duration is used.|
| args | Object[] | No| Array of parameters in the method.|
**Return value**
| Type | Description |
| ------------------------------------- | ------------------------------- |
| Object | Return value of the method in the host thread. The return value must be serializable. For details, see [Sequenceable Data Types](#sequenceable-data-types).|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ----------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | Worker instance is not running. |
| 10200006 | An exception occurred during serialization. |
| 10200019 | The globalCallObject is not registered. |
| 10200020 | The method to be called is not callable or is an async method or a generator. |
| 10200021 | The global call exceeds the timeout. |
**Example**
```ts
// worker.ets
import { worker, MessageEvents } from '@kit.ArkTS';
const workerPort = worker.workerPort;
workerPort.onmessage = (e: MessageEvents): void => {
try {
// The method to call does not carry an input parameter.
let res : string = workerPort.callGlobalCallObjectMethod("myObj", "getMessage", 0) as string;
console.info("worker:", res) // worker: this is a message from TestObj
} catch (error) {
// Exception handling.
console.error("worker: error code is " + error.code + " error message is " + error.message);
}
try {
// The method to call carries input parameters.
let res : string = workerPort.callGlobalCallObjectMethod("myObj", "getMessageWithInput", 0, "hello there!") as string;
console.info("worker:", res) //worker: this is a message from TestObj with input: hello there!
} catch (error) {
// Exception handling.
console.error("worker: error code is " + error.code + " error message is " + error.message);
}
}
```
### close9+
close(): void
Terminates the Worker thread to stop it from receiving messages.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Error codes**
For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | ------------------------------- |
| 10200004 | The Worker instance is not running. |
**Example**
```ts
// Main thread
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
```
```ts
// worker.ets
import { worker, MessageEvents } from '@kit.ArkTS';
const workerPort = worker.workerPort;
workerPort.onmessage = (e: MessageEvents): void => {
workerPort.close()
}
```
### onmessage9+
onmessage?: (this: ThreadWorkerGlobalScope, ev: MessageEvents) => void
Called when the Worker thread receives a message sent by the host thread through **postMessage**. The event handler is executed in the Worker thread. In the callback function, **this** indicates the caller's [ThreadWorkerGlobalScope](#threadworkerglobalscope9), and the **ev** type is [MessageEvents](#messageevents9), indicating the received message data.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. |
| 10200004 | The Worker instance is not running. |
| 10200005 | The called API is not supported in the worker thread. |
**Example**
```ts
// Main thread
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.postMessage("hello world");
```
```ts
// worker.ets
import { worker, MessageEvents } from '@kit.ArkTS';
const workerPort = worker.workerPort;
workerPort.onmessage = (e: MessageEvents): void => {
console.log("receive main thread message");
}
```
### onmessageerror9+
onmessageerror?: (this: ThreadWorkerGlobalScope, ev: MessageEvents) => void
Called when the Worker thread receives a message that cannot be deserialized. The event handler is executed in the Worker thread. In the callback function, **this** indicates the caller's [ThreadWorkerGlobalScope](#threadworkerglobalscope9), and the **ev** type is [MessageEvents](#messageevents9), indicating the received message data.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. |
| 10200004 | The Worker instance is not running. |
| 10200005 | The called API is not supported in the worker thread. |
**Example**
```ts
// Main thread
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
```
```ts
// worker.ets
import { worker, MessageEvents } from '@kit.ArkTS';
const workerPort = worker.workerPort;
workerPort.onmessageerror = (err: MessageEvents) => {
console.log("worker.ets onmessageerror");
}
```
## WorkerEventListener9+
Implements event listening.
### (event: Event)9+
(event: Event): void | Promise<void>
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | --------------- | ---- | -------------- |
| event | [Event](#event) | Yes | Event class for the callback to invoke.|
**Return value**
| Type | Description |
| ------------------------------------- | ------------------------------- |
| void \| Promise<void> | Returns no value or returns a **Promise**.|
**Error codes**
For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200004 | Worker instance is not running. |
| 10200005 | The invoked API is not supported in workers. |
**Example**
```ts
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
```
## GlobalScope9+
Implements the running environment of the Worker thread. The **GlobalScope** class inherits from [WorkerEventTarget](#workereventtarget9).
### Attributes
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
| Name| Type | Readable| Writable| Description |
| ---- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------- |
| name | string | Yes | No | Worker instance specified when there is a new Worker instance.|
| self | [GlobalScope](#globalscope9) & typeof globalThis | Yes | No | GlobalScope itself. |
### onerror9+
onerror?: (ev: ErrorEvent) => void
Called when an exception occurs during worker execution. The event handler is executed in the Worker thread. In the callback function, the **ev** type is [ErrorEvent](#errorevent), indicating the received abnormal data.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
// Main thread
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ets")
```
```ts
// worker.ets
import { worker, ErrorEvent } from '@kit.ArkTS';
const workerPort = worker.workerPort
workerPort.onerror = (err: ErrorEvent) => {
console.log("worker.ets onerror" + err.message)
}
```
## MessageEvents9+
Holds the data transferred between Worker threads.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description |
| ---- | ---- | ---- | ---- | ------------------ |
| data | any | Yes | No | Data transferred between threads.|
## MessageType7+
type MessageType = 'message' | 'messageerror';
Defines the message type.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
| Type | Description |
| ---- | ------------------ |
| 'message' | The message type is message, fixed at **'message'**.|
| 'messageerror' | The message type is messageerror, fixed at **'messageerror'**.|
## Worker(deprecated)
Before using the following APIs, you must create a Worker instance. The **Worker** class inherits from [EventTarget](#eventtargetdeprecated).
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker9+](#threadworker9) instead.
### constructor(deprecated)
constructor(scriptURL: string, options?: WorkerOptions)
A constructor used to create a Worker instance.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.constructor9+](#constructor9) instead.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| --------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| scriptURL | string | Yes | URL of the Worker thread file. For details about the rules, see [Precautions for File URLs](../../arkts-utils/worker-introduction.md#precautions-for-file-urls).|
| options | [WorkerOptions](#workeroptions) | No | Options that can be set for the Worker instance. |
**Example**
The following code snippet shows how to load the Worker thread file of the ability in the stage model. For details about how to use the library to load the Worker thread file, see [Precautions for File URLs](../../arkts-utils/worker-introduction.md#precautions-for-file-urls).
```ts
import { worker } from '@kit.ArkTS';
// Two scenarios are involved.
// Scenario 1: URL of the Worker thread file: "entry/src/main/ets/workers/worker.ets"
const workerStageModel01 = new worker.ThreadWorker('entry/ets/workers/worker.ets', {name:"first worker in Stage model"});
// Scenario 2: URL of the Worker thread file: "phone/src/main/ets/ThreadFile/workers/worker.ets"
const workerStageModel02 = new worker.ThreadWorker('phone/ets/ThreadFile/workers/worker.ets');
```
### postMessage(deprecated)
postMessage(message: Object, transfer: ArrayBuffer[]): void
Sends a message from the host thread to the Worker thread by transferring object ownership.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.postMessage9+](#postmessage9) instead.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
| 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.|
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
let buffer = new ArrayBuffer(8);
workerInstance.postMessage(buffer, [buffer]);
```
### postMessage(deprecated)
postMessage(message: Object, options?: PostMessageOptions): void
Sends a message from the host thread to the Worker thread by transferring object ownership or copying data.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.postMessage9+](#postmessage9-1) instead.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| 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.
If this parameter is not specified, the default value **undefined** is used, and information is transferred to the Worker thread by copying data.|
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.postMessage("hello world");
let buffer = new ArrayBuffer(8);
workerInstance.postMessage(buffer, [buffer]);
```
### on(deprecated)
on(type: string, listener: EventListener): void
Adds an event listener for the Worker thread. This API provides the same functionality as [addEventListener(deprecated)](#addeventlistenerdeprecated).
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.on9+](#on9) instead.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ----------------------------------------- | ---- | ---------------- |
| type | string | Yes | Type of the event to listen for.|
| listener | [EventListener](#eventlistenerdeprecated) | Yes | Callback to invoke when an event of the specified type occurs. |
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.on("alert", ()=>{
console.log("alert listener callback");
})
```
### once(deprecated)
once(type: string, listener: EventListener): void
Adds an event listener for the Worker thread and removes the event listener after it is invoked once.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.once9+](#once9) instead.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ----------------------------------------- | ---- | ---------------- |
| type | string | Yes | Type of the event to listen for.|
| listener | [EventListener](#eventlistenerdeprecated) | Yes | Callback to invoke when an event of the specified type occurs. |
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.once("alert", ()=>{
console.log("alert listener callback");
})
```
### off(deprecated)
off(type: string, listener?: EventListener): void
Removes an event listener for the Worker thread. This API provides the same functionality as [removeEventListener(deprecated)](#removeeventlistenerdeprecated).
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.off9+](#off9) instead.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ----------------------------------------- | ---- | -------------------- |
| type | string | Yes | Type of the event for which the event listener is to be removed.|
| listener | [EventListener](#eventlistenerdeprecated) | No | Callback to invoke when the listener is removed.|
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
// Use on, once, or addEventListener to add a listener for the "alert" event, and use off to remove the listener.
workerInstance.off("alert");
```
### terminate(deprecated)
terminate(): void
Terminates the Worker thread to stop it from receiving messages.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.terminate9+](#terminate9) instead.
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.terminate();
```
### onexit(deprecated)
onexit?: (code: number) => void
Called when the Worker thread exits. The event handler is executed in the host thread. In the callback function, the **code** value is of the number type, where the value **1** indicates abnormal exit and **0** indicates normal exit.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.onexit9+](#onexit9) instead.
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.onexit = (code) => {
console.log("onexit");
}
// onexit is executed in either of the following ways:
// Main thread
workerInstance.terminate();
// Worker thread
//parentPort.close()
```
### onerror(deprecated)
onerror?: (err: ErrorEvent) => void
Called when an exception occurs during worker execution. The event handler is executed in the host thread. In the callback function, the **err** type is [ErrorEvent](#errorevent), indicating the received abnormal data.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.onerror9+](#onerror9) instead.
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
import { worker, ErrorEvent } from '@kit.ArkTS';
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.onerror = (err: ErrorEvent) => {
console.log("onerror" + err.message);
}
```
### onmessage(deprecated)
onmessage?: (event: MessageEvent) => void
Called when the host thread receives a message sent by the Worker thread through **workerPort.postMessage**. The event handler is executed in the host thread. In the callback function, the **event** type is [MessageEvent](#messageeventt), indicating the received message data.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.onmessage9+](#onmessage9) instead.
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.onmessage = (): void => {
console.log("onmessage");
}
```
### onmessageerror(deprecated)
onmessageerror?: (event: MessageEvent) => void
Called when the Worker thread receives a message that cannot be serialized. The event handler is executed in the host thread. In the callback function, the **event** type is [MessageEvent](#messageeventt), indicating the received message data.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.onmessageerror9+](#onmessageerror9) instead.
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.onmessageerror = (err) => {
console.log("onmessageerror");
}
```
## EventTarget(deprecated)
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [WorkerEventTarget9+](#workereventtarget9) instead.
### addEventListener(deprecated)
addEventListener(type: string, listener: EventListener): void
Adds an event listener for the Worker thread. This API provides the same functionality as [on(deprecated)](#ondeprecated).
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [addEventListener9+](#addeventlistener9) instead.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ----------------------------------------- | ---- | ---------------- |
| type | string | Yes | Type of the event to listen for.|
| listener | [EventListener](#eventlistenerdeprecated) | Yes | Callback to invoke when an event of the specified type occurs. |
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
```
### removeEventListener(deprecated)
removeEventListener(type: string, callback?: EventListener): void
Removes an event listener for the Worker thread. This API provides the same functionality as [off(deprecated)](#offdeprecated).
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [removeEventListener9+](#removeeventlistener9) instead.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ----------------------------------------- | ---- | ------------------------ |
| type | string | Yes | Type of the event for which the event listener is to be removed.|
| callback | [EventListener](#eventlistenerdeprecated) | No | Callback to invoke when the listener is removed.|
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
workerInstance.removeEventListener("alert");
```
### dispatchEvent(deprecated)
dispatchEvent(event: Event): boolean
Dispatches the event defined for the Worker thread.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [dispatchEvent9+](#dispatchevent9) instead.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | --------------- | ---- | ---------------- |
| event | [Event](#event) | Yes | Event to dispatch.|
**Return value**
| Type | Description |
| ------- | ------------------------------- |
| boolean | Returns **true** if the event is dispatched successfully; returns **false** otherwise.|
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is not supported yet.
```
The **dispatchEvent** API can be used together with the **on**, **once**, and **addEventListener** APIs. The sample code is as follows:
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
// Usage 1:
workerInstance.on("alert_on", ()=>{
console.log("alert listener callback");
})
workerInstance.once("alert_once", ()=>{
console.log("alert listener callback");
})
workerInstance.addEventListener("alert_add", ()=>{
console.log("alert listener callback");
})
// The event listener created by once is removed after being executed once.
workerInstance.dispatchEvent({type:"alert_once", timeStamp:0}); // timeStamp is not supported yet.
// The event listener created by on will not be proactively deleted.
workerInstance.dispatchEvent({type:"alert_on", timeStamp:0});
workerInstance.dispatchEvent({type:"alert_on", timeStamp:0});
// The event listener created by addEventListener will not be proactively deleted.
workerInstance.dispatchEvent({type:"alert_add", timeStamp:0});
workerInstance.dispatchEvent({type:"alert_add", timeStamp:0});
// Usage 2:
// The event type can be customized, and the special types "message", "messageerror", and "error" exist.
// When type = "message", the event handler defined by onmessage will also be executed.
// When type = "messageerror", the event handler defined by onmessageerror will also be executed.
// When type = "error", the event handler defined by onerror will also be executed.
// removeEventListener or off can be used to remove an event listener that is created by addEventListener, on, or once.
workerInstance.addEventListener("message", ()=>{
console.log("message listener callback");
})
workerInstance.onmessage = function() {
console.log("onmessage : message listener callback");
}
// When dispatchEvent is called to distribute the "message" event, the callback passed in addEventListener and onmessage will be invoked.
workerInstance.dispatchEvent({type:"message", timeStamp:0});
```
### removeAllListener(deprecated)
removeAllListener(): void
Removes all event listeners for the Worker thread.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [removeAllListener9+](#removealllistener9) instead.
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
workerInstance.removeAllListener();
```
## DedicatedWorkerGlobalScope(deprecated)
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. This class inherits from [WorkerGlobalScope](#workerglobalscopedeprecated).
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope9+](#threadworkerglobalscope9) instead.
### postMessage(deprecated)
postMessage(messageObject: Object, transfer: Transferable[]): void
Sends a message from the Worker thread to the host thread by transferring object ownership.
> **NOTE**
> This API is deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope9+.postMessage9+](#postmessage9-2).
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| 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. |
### postMessage9+
postMessage(messageObject: Object, transfer: ArrayBuffer[]): void
Sends a message from the Worker thread to the host thread by transferring object ownership.
> **NOTE**
>
> The **DedicatedWorkerGlobalScope** class is deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope9+.postMessage9+](#postmessage9-2).
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
| 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 | 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.|
**Example**
```ts
// Main thread
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.postMessage("hello world");
workerInstance.onmessage = (): void => {
// let data = e.data;
console.log("receive data from worker.ets");
}
```
```ts
// worker.ets
import { worker } from '@kit.ArkTS';
const workerPort = worker.workerPort;
workerPort.onmessage = (): void => {
// let data = e.data;
let buffer = new ArrayBuffer(5)
workerPort.postMessage(buffer, [buffer]);
}
```
### postMessage(deprecated)
postMessage(messageObject: Object, options?: PostMessageOptions): void
Sends a message from the Worker thread to the host thread by transferring object ownership or copying data.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope9+.postMessage9+](#postmessage9-3).
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name | Type | Mandatory| Description |
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| 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).|
| 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.
If this parameter is not specified, the default value **undefined** is used, and information is transferred to the host thread by copying data.|
**Example**
```ts
// Main thread
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.Worker("entry/ets/workers/worker.ets");
workerInstance.postMessage("hello world");
workerInstance.onmessage = (): void => {
console.log("receive data from worker.ets");
}
```
```ts
// worker.ets
import { ErrorEvent, MessageEvents, worker } from '@kit.ArkTS';
const parentPort = worker.parentPort;
parentPort.onmessage = (e: MessageEvents) => {
parentPort.postMessage("receive data from main thread");
}
```
### close(deprecated)
close(): void
Terminates the Worker thread to stop it from receiving messages.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope9+.close9+](#close9).
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
// Main thread
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.Worker("workers/worker.ets");
```
```ts
// worker.ets
import { worker } from '@kit.ArkTS';
const parentPort = worker.parentPort;
parentPort.onmessage = (): void => {
parentPort.close()
}
```
### onmessage(deprecated)
onmessage?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void
Called when the Worker thread receives a message sent by the host thread through **postMessage**. The event handler is executed in the Worker thread. In the callback function, **this** indicates the caller's [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscopedeprecated), and the **ev** type is [MessageEvent](#messageeventt), indicating the received message data.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope9+.onmessage9+](#onmessage9-1).
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
// Main thread
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.postMessage("hello world");
```
```ts
// worker.ets
import { worker } from '@kit.ArkTS';
const parentPort = worker.parentPort;
parentPort.onmessage = (): void => {
console.log("receive main thread message");
}
```
### onmessageerror(deprecated)
onmessageerror?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void
Called when the Worker thread receives a message that cannot be deserialized. The event handler is executed in the Worker thread. In the callback function, **this** indicates the caller's [DedicatedWorkerGlobalScope](#threadworkerglobalscope9), and the **ev** type is [MessageEvent](#dedicatedworkerglobalscopedeprecated), indicating the received message data.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope9+.onmessageerror9+](#onmessageerror9-1).
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
// Main thread
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.Worker("workers/worker.ets");
```
```ts
// worker.ets
import { worker } from '@kit.ArkTS';
const parentPort = worker.parentPort;
parentPort.onmessageerror = () => {
console.log("worker.ets onmessageerror")
}
```
## PostMessageOptions
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.
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
| Name | Type | Readable| Writable| Description |
| -------- | -------- | ---- | ---- | --------------------------------- |
| transfer | Object[] | Yes | Yes | **ArrayBuffer** array used to transfer the ownership. The array cannot be **null**.|
## Event
Defines the event.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
| Name | Type | Readable| Writable| Description |
| --------- | ------ | ---- | ---- | -------------------------------------------- |
| type | string | Yes | No | Type of the event. |
| timeStamp | number | Yes | No | Timestamp (accurate to millisecond) when the event is created. This parameter is not supported yet.|
## EventListener(deprecated)
Implements event listening.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [WorkerEventListener9+](#workereventlistener9) instead.
### (evt: Event)(deprecated)
(evt: Event): void | Promise<void>
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [(event:Event)9+](#event-event9) instead.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | --------------- | ---- | -------------- |
| evt | [Event](#event) | Yes | Event class for the callback to invoke.|
**Return value**
| Type | Description |
| ------------------------------------- | ------------------------------- |
| void \| Promise<void> | Returns no value or returns a **Promise**.|
**Example**
```ts
const workerInstance = new worker.Worker("workers/worker.ets");
workerInstance.addEventListener("alert", ()=>{
console.log("alert listener callback");
})
```
## ErrorEvent
Provides detailed information about the exception that occurs during worker execution. The **ErrorEvent** class inherits from [Event](#event).
**Atomic service API**: This API can be used in atomic services since API version 11.
**System capability**: SystemCapability.Utils.Lang
| Name | Type | Readable| Writable| Description |
| -------- | ------ | ---- | ---- | -------------------- |
| message | string | Yes | No | Information about the exception.|
| filename | string | Yes | No | File where the exception is located.|
| lineno | number | Yes | No | Serial number of the line where the exception is located. |
| colno | number | Yes | No | Serial number of the column where the exception is located. |
| error | Object | Yes | No | Type of the exception. |
## MessageEvent\
Holds the data transferred between Worker threads.
**Atomic service API**: This API can be used in atomic services since API version 12.
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description |
| ---- | ---- | ---- | ---- | ------------------ |
| data | T | Yes | No | Data transferred between threads.|
## WorkerGlobalScope(deprecated)
Implements the running environment of the Worker thread. The **WorkerGlobalScope** class inherits from [EventTarget](#eventtargetdeprecated).
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [GlobalScope9+](#globalscope9) instead.
### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type | Readable| Writable| Description |
| ---- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------- |
| name | string | Yes | No | Worker instance specified when there is a new Worker instance.|
| self | [WorkerGlobalScope](#workerglobalscopedeprecated) & typeof globalThis | Yes | No | WorkerGlobalScope. |
### onerror(deprecated)
onerror?: (ev: ErrorEvent) => void
Called when an exception occurs during worker execution. The event handler is executed in the Worker thread. In the callback function, the **ev** type is [ErrorEvent](#errorevent), indicating the received abnormal data.
> **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [GlobalScope9+.onerror9+](#onerror9-1).
**System capability**: SystemCapability.Utils.Lang
**Example**
```ts
// Main thread
import { worker } from '@kit.ArkTS';
const workerInstance = new worker.Worker("workers/worker.ets")
```
```ts
// worker.ets
import { worker, ErrorEvent } from '@kit.ArkTS';
const parentPort = worker.parentPort
parentPort.onerror = (err: ErrorEvent) => {
console.log("worker.ets onerror" + err.message)
}
```
## More Information
### Sequenceable Data Types
The following object types are supported: basic types except Symbol, Date, String, RegExp, Array, Map, Set, Object (simple objects only, for example, objects created using **{}** or **new Object**), ArrayBuffer, and typedArray. (Note that only attributes can be transferred for common objects. Prototypes and methods cannot be transferred.)
Exception: When an object created through a custom class is passed, no serialization error occurs. However, the attributes (such as Function) of the custom class cannot be passed through serialization.
> **NOTE**
> An FA project of API version 9 is used as an example.
```ts
// Main thread
import { worker, MessageEvents } from '@kit.ArkTS';
const workerInstance = new worker.ThreadWorker("workers/worker.ets");
workerInstance.postMessage("message from main thread to worker");
workerInstance.onmessage = (d: MessageEvents): void => {
// When the Worker thread passes obj2, data contains obj2, excluding the Init or SetName method.
let data: string = d.data;
}
```
```ts
// worker.ets
import { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS';
const workerPort = worker.workerPort;
class MyModel {
name = "undefined"
Init() {
this.name = "MyModel"
}
}
workerPort.onmessage = (d: MessageEvents): void => {
console.log("worker.ets onmessage");
let data: string = d.data;
let func1 = () => {
console.log("post message is function");
}
// workerPort.postMessage(func1); A serialization error occurs when passing func1.
let obj2 = new MyModel();
workerPort.postMessage(obj2); // No serialization error occurs when passing obj2.
}
workerPort.onmessageerror = () => {
console.log("worker.ets onmessageerror");
}
workerPort.onerror = (err: ErrorEvent) => {
console.log("worker.ets onerror" + err.message);
}
```
### Memory Model
The Worker thread is implemented based on the actor model. In the Worker interaction process, the JS host 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 host thread.
Each actor concurrently processes tasks of the host thread. For each actor, there is a message queue and a single-thread execution module. The message queue receives requests from the host 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.
## Sample Code
> **NOTE**
>
> Only the FA model is supported in API version 8 and earlier versions. If you want to use API version 8 or earlier, change the API for constructing the Worker instance and the API for creating an object in the Worker thread for communicating with the host thread.
### FA Model
> The following uses API version 9 as an example.
```ts
// Main thread (The following assumes that the workers directory and pages directory are at the same level.)
import { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS';
// Create a Worker instance in the host thread.
const workerInstance = new worker.ThreadWorker("workers/worker.ets");
// The host thread transfers information to the Worker thread.
const buffer = new ArrayBuffer(8);
workerInstance.postMessage(buffer, [buffer]);
// The host thread receives information from the Worker thread.
workerInstance.onmessage = (e: MessageEvents): void => {
// data carries the information sent by the Worker thread.
let data: string = e.data;
console.log("main thread onmessage");
// Terminate the Worker instance.
workerInstance.terminate();
}
// Call onexit().
workerInstance.onexit = (code) => {
console.log("main thread terminate");
}
workerInstance.onerror = (err: ErrorEvent) => {
console.log("main error message " + err.message);
}
```
```ts
// worker.ets
import { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS';
// Create an object in the Worker thread for communicating with the host thread.
const workerPort = worker.workerPort
// The Worker thread receives information from the host thread.
workerPort.onmessage = (e: MessageEvents): void => {
// data carries the information sent by the host thread.
let data: number = e.data;
const view = new Int8Array(data).fill(3);
console.log("worker.ets onmessage");
// The Worker thread sends information to the host thread.
workerPort.postMessage(view);
}
// Trigger a callback when an error occurs in the Worker thread.
workerPort.onerror = (err: ErrorEvent) => {
console.log("worker.ets onerror");
}
```
Add the following configuration to the module-level **entry/build-profile.json5** file:
```json
"buildOption": {
"sourceOption": {
"workers": [
"./src/main/ets/entryability/workers/worker.ets"
]
}
}
```
### Stage Model
> The following uses API version 12 as an example.
```ts
// Index.ets
import { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
// Create a Worker instance in the host thread.
const workerInstance = new worker.ThreadWorker("entry/ets/workers/Worker.ets");
// The host thread transfers information to the Worker thread.
const buffer = new ArrayBuffer(8);
workerInstance.postMessage(buffer);
// The host thread receives information from the Worker thread.
workerInstance.onmessage = (e: MessageEvents): void => {
// data carries the information sent by the Worker thread.
let data: number = e.data;
console.info("main thread data is " + data);
// Terminate the Worker instance.
workerInstance.terminate();
}
// Call onexit().
workerInstance.onexit = (code) => {
console.log("main thread terminate");
}
workerInstance.onerror = (err: ErrorEvent) => {
console.log("main error message " + err.message);
}
})
}
.width('100%')
.height('100%')
}
}
}
```
```ts
// Worker.ets
import { worker, MessageEvents, ErrorEvent } from '@kit.ArkTS';
// Create an object in the Worker thread for communicating with the host thread.
const workerPort = worker.workerPort
// The Worker thread receives information from the host thread.
workerPort.onmessage = (e: MessageEvents): void => {
// data carries the information sent by the host thread.
let data: number = e.data;
// Write data to the received buffer.
const view = new Int8Array(data).fill(3);
// The Worker thread sends information to the host thread.
workerPort.postMessage(view);
}
// Trigger a callback when an error occurs in the Worker thread.
workerPort.onerror = (err: ErrorEvent) => {
console.log("worker.ets onerror" + err.message);
}
```
Add the following configuration to the module-level **entry/build-profile.json5** file:
```json
"buildOption": {
"sourceOption": {
"workers": [
"./src/main/ets/workers/Worker.ets"
]
}
}
```