1# @ohos.worker (Starting the Worker) (System API)
2
3The 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.
4
5With 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.
6
7The Context object of the Worker thread is different from that of the UI main thread. The Worker thread does not support UI operations.
8
9For details about the precautions for using Worker, see [Precautions for Worker](../../arkts-utils/worker-introduction.md#precautions-for-worker).
10
11> **NOTE**
12>
13> - 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.
14>
15> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.worker (Starting the Worker)](js-apis-worker.md).
16
17## Modules to Import
18
19```ts
20import { worker } from '@kit.ArkTS';
21```
22
23## RestrictedWorker<sup>11+</sup>
24
25The RestrictedWorker class inherits [ThreadWorker<sup>9+</sup>](js-apis-worker.md#threadworker9) and supports all APIs in ThreadWorker.
26
27RestrictedWorker provides a restricted environment for running the Worker thread. In this environment, only the Worker module can be imported.
28
29### constructor<sup>11+</sup>
30
31constructor(scriptURL: string, options?: WorkerOptions)
32
33A constructor used to create a RestrictedWorker instance. Before using the following APIs, you must create a RestrictedWorker instance.
34
35**Atomic service API**: This API can be used in atomic services since API version 12.
36
37**System API**: This is a system API.
38
39**System capability**: SystemCapability.Utils.Lang
40
41**Parameters**
42
43| Name   | Type                           | Mandatory| Description                                                        |
44| --------- | ------------------------------- | ---- | ------------------------------------------------------------ |
45| 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).|
46| options   | [WorkerOptions](js-apis-worker.md#workeroptions) | No  | Options that can be set for the RestrictedWorker instance.                                          |
47
48**Error codes**
49
50For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
51
52| ID| Error Message|
53| -------- | -------- |
54| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
55| 10200003 | Worker initialization failure. |
56| 10200007 | The worker file patch is invalid path. |
57
58**Example**
59
60The 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).
61
62Only the Worker module can be imported to the restricted Worker thread file. Other APIs cannot be imported. The following is sample code:
63
64```ts
65import { worker } from '@kit.ArkTS';
66
67// Two scenarios are involved.
68
69// Scenario 1: URL of the Worker thread file: "entry/src/main/ets/workers/worker.ets"
70const workerStageModel01 = new worker.RestrictedWorker('entry/ets/workers/worker.ets', {name:"first worker in Stage model"});
71
72// Scenario 2: URL of the Worker thread file: "phone/src/main/ets/ThreadFile/workers/worker.ets"
73const workerStageModel02 = new worker.RestrictedWorker('phone/ets/ThreadFile/workers/worker.ets');
74```
75
76```ts
77// Restricted Worker thread file
78import { worker, MessageEvents } from '@kit.ArkTS';
79
80//import { process } from '@kit.ArkTS'; // Only worker APIs can be imported to the restricted Worker thread file.
81
82const workerPort = worker.workerPort;
83
84workerPort.onmessage = (e : MessageEvents) : void => {
85  console.info("worker:: This is worker thread.")
86  // console.info("worker:: worker tid: "+ process.tid) // Run process.tid. The host thread reports the corresponding error.
87}
88```
89