1e41f4b71Sopenharmony_ci# Worker简介
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciWorker主要作用是为应用程序提供一个多线程的运行环境,可满足应用程序在执行过程中与主线程分离,在后台线程中运行一个脚本进行耗时操作,极大避免类似于计算密集型或高延迟的任务阻塞主线程的运行。具体接口信息及使用方法详情请见[Worker](../reference/apis-arkts/js-apis-worker.md)。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci## Worker运作机制
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci**图1** Worker运作机制示意图
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci![worker](figures/worker.png)
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci创建Worker的线程称为宿主线程(不一定是主线程,工作线程也支持创建Worker子线程),Worker自身的线程称为Worker子线程(或Actor线程、工作线程)。每个Worker子线程与宿主线程拥有独立的实例,包含基础设施、对象、代码段等,因此每个Worker启动存在一定的内存开销,需要限制Worker的子线程数量。Worker子线程和宿主线程之间的通信是基于消息传递的,Worker通过序列化机制与宿主线程之间相互通信,完成命令及数据交互。
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## Worker注意事项
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci- 创建Worker时,有手动和自动两种创建方式,手动创建Worker线程目录及文件时,还需同步进行相关配置,详情请参考[创建Worker的注意事项](#创建worker的注意事项)。
18e41f4b71Sopenharmony_ci- 使用Worker能力时,构造函数中传入的Worker线程文件的路径在不同版本有不同的规则,详情请参见[文件路径注意事项](#文件路径注意事项)。
19e41f4b71Sopenharmony_ci- Worker创建后需要手动管理生命周期,且最多同时运行的Worker子线程数量为64个,详情请参见[生命周期注意事项](#生命周期注意事项)。
20e41f4b71Sopenharmony_ci- 由于不同线程中上下文对象是不同的,因此Worker线程只能使用线程安全的库,例如UI相关的非线程安全库不能使用。
21e41f4b71Sopenharmony_ci- 序列化传输的数据量大小限制为16MB。
22e41f4b71Sopenharmony_ci- 使用Worker模块时,需要在主线程中注册onerror接口,否则当Worker线程出现异常时会发生jscrash问题。
23e41f4b71Sopenharmony_ci- 不支持跨HAP使用Worker线程文件。
24e41f4b71Sopenharmony_ci- 创建Worker对象时仅允许加载本模块下存在的Worker线程文件,不支持加载其他模块的Worker线程文件。若依赖其他模块提供的Worker功能,需要将Worker实现的整套逻辑封装到方法中,将方法导出后供其他模块使用。
25e41f4b71Sopenharmony_ci- 引用HAR/HSP前,需要先配置对HAR/HSP的依赖,详见[引用共享包](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-har-import-V5)。
26e41f4b71Sopenharmony_ci- 不支持在Worker工作线程中使用[AppStorage](../quick-start/arkts-appstorage.md)。
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci### 创建Worker的注意事项
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ciWorker线程文件需要放在"{moduleName}/src/main/ets/"目录层级之下,否则不会被打包到应用中。有手动和自动两种创建Worker线程目录及文件的方式。
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci- 手动创建:开发者手动创建相关目录及文件,此时需要配置build-profile.json5的相关字段信息,Worker线程文件才能确保被打包到应用中。
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci  Stage模型:
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci  ```json
38e41f4b71Sopenharmony_ci  "buildOption": {
39e41f4b71Sopenharmony_ci    "sourceOption": {
40e41f4b71Sopenharmony_ci      "workers": [
41e41f4b71Sopenharmony_ci        "./src/main/ets/workers/worker.ets"
42e41f4b71Sopenharmony_ci      ]
43e41f4b71Sopenharmony_ci    }
44e41f4b71Sopenharmony_ci  }
45e41f4b71Sopenharmony_ci  ```
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci  FA模型:
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci  ```json
50e41f4b71Sopenharmony_ci  "buildOption": {
51e41f4b71Sopenharmony_ci    "sourceOption": {
52e41f4b71Sopenharmony_ci      "workers": [
53e41f4b71Sopenharmony_ci        "./src/main/ets/MainAbility/workers/worker.ets"
54e41f4b71Sopenharmony_ci      ]
55e41f4b71Sopenharmony_ci    }
56e41f4b71Sopenharmony_ci  }
57e41f4b71Sopenharmony_ci  ```
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci- 自动创建:DevEco Studio支持一键生成Worker,在对应的{moduleName}目录下任意位置,点击鼠标右键 > New > Worker,即可自动生成Worker的模板文件及配置信息,无需再手动在build-profile.json5中进行相关配置。
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci### 文件路径注意事项
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci  当使用Worker模块具体功能时,均需先构造Worker实例对象,其构造函数与API版本相关,且构造函数需要传入Worker线程文件的路径(scriptURL)。
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci```ts
67e41f4b71Sopenharmony_ci// 导入模块
68e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS';
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci// API 9及之后版本使用:
71e41f4b71Sopenharmony_ciconst worker1: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/MyWorker.ets');
72e41f4b71Sopenharmony_ci// API 8及之前版本使用:
73e41f4b71Sopenharmony_ciconst worker2: worker.Worker = new worker.Worker('entry/ets/workers/MyWorker.ets');
74e41f4b71Sopenharmony_ci```
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci#### Stage模型下的文件路径规则
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci构造函数中的scriptURL要求如下:
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci- scriptURL的组成包含 {moduleName}/ets 和相对路径 relativePath。
81e41f4b71Sopenharmony_ci- relativePath是Worker线程文件相对于"{moduleName}/src/main/ets/"目录的相对路径。
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci1) 加载Ability中Worker线程文件场景
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci加载Ability中的worker线程文件,加载路径规则:{moduleName}/ets/{relativePath}。
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci```ts
88e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS';
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci// worker线程文件所在路径:"entry/src/main/ets/workers/worker.ets"
91e41f4b71Sopenharmony_ciconst workerStage1: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/worker.ets');
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci// worker线程文件所在路径:"phone/src/main/ets/ThreadFile/workers/worker.ets"
94e41f4b71Sopenharmony_ciconst workerStage2: worker.ThreadWorker = new worker.ThreadWorker('phone/ets/ThreadFile/workers/worker.ets');
95e41f4b71Sopenharmony_ci```
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci2) 加载[HSP](../quick-start/in-app-hsp.md)中Worker线程文件场景
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci加载HSP中worker线程文件,加载路径规则:{moduleName}/ets/{relativePath}。
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci```ts
102e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS';
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci// worker线程文件所在路径: "hsp/src/main/ets/workers/worker.ets"
105e41f4b71Sopenharmony_ciconst workerStage3: worker.ThreadWorker = new worker.ThreadWorker('hsp/ets/workers/worker.ets');
106e41f4b71Sopenharmony_ci```
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci3) 加载[HAR](../quick-start/har-package.md)中Worker线程文件场景
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci加载HAR中worker线程文件存在以下两种情况:
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci- @标识路径加载形式:所有种类的模块加载本地HAR中的Worker线程文件,加载路径规则:@{moduleName}/ets/{relativePath}。
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci- 相对路径加载形式:本地HAR加载该包内的Worker线程文件,加载路径规则:创建Worker对象所在文件与Worker线程文件的相对路径。
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci>**说明:**
117e41f4b71Sopenharmony_ci>
118e41f4b71Sopenharmony_ci>当开启useNormalizedOHMUrl(即将工程目录中与entry同级别的应用级build-profile.json5文件中strictMode属性的useNormalizedOHMUrl字段配置为true)或HAR包会被打包成三方包使用时,则HAR包中使用Worker仅支持通过相对路径的加载形式创建。
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci```ts
121e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS';
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci// @标识路径加载形式:
124e41f4b71Sopenharmony_ci// worker线程文件所在路径: "har/src/main/ets/workers/worker.ets"
125e41f4b71Sopenharmony_ciconst workerStage4: worker.ThreadWorker = new worker.ThreadWorker('@har/ets/workers/worker.ets');
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci// 相对路径加载形式:
128e41f4b71Sopenharmony_ci// worker线程文件所在路径: "har/src/main/ets/workers/worker.ets"
129e41f4b71Sopenharmony_ci// 创建Worker对象的文件所在路径:"har/src/main/ets/components/mainpage/MainPage.ets"
130e41f4b71Sopenharmony_ciconst workerStage5: worker.ThreadWorker = new worker.ThreadWorker('../../workers/worker.ets');
131e41f4b71Sopenharmony_ci```
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci#### FA模型下的文件路径规则
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci  构造函数中的scriptURL为:Worker线程文件与"{moduleName}/src/main/ets/MainAbility"的相对路径。
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci```ts
138e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS';
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci// 主要说明以下三种场景:
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci// 场景1: Worker线程文件所在路径:"{moduleName}/src/main/ets/MainAbility/workers/worker.ets"
143e41f4b71Sopenharmony_ciconst workerFA1: worker.ThreadWorker = new worker.ThreadWorker("workers/worker.ets", {name:"first worker in FA model"});
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci// 场景2: Worker线程文件所在路径:"{moduleName}/src/main/ets/workers/worker.ets"
146e41f4b71Sopenharmony_ciconst workerFA2: worker.ThreadWorker = new worker.ThreadWorker("../workers/worker.ets");
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci// 场景3: Worker线程文件所在路径:"{moduleName}/src/main/ets/MainAbility/ThreadFile/workers/worker.ets"
149e41f4b71Sopenharmony_ciconst workerFA3: worker.ThreadWorker = new worker.ThreadWorker("ThreadFile/workers/worker.ets");
150e41f4b71Sopenharmony_ci```
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci### 生命周期注意事项
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci- Worker的创建和销毁耗费性能,建议开发者合理管理已创建的Worker并重复使用。Worker空闲时也会一直运行,因此当不需要Worker时,可以调用[terminate()](../reference/apis-arkts/js-apis-worker.md#terminate9)接口或[close()](../reference/apis-arkts/js-apis-worker.md#close9)方法主动销毁Worker。若Worker处于已销毁或正在销毁等非运行状态时,调用其功能接口,会抛出相应的错误。
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci- Worker的数量由内存管理策略决定,设定的内存阈值为1.5GB和设备物理内存的60%中的较小者。在内存允许的情况下,系统最多可以同时运行64个Worker。如果尝试创建的Worker数量超出这一上限,系统将抛出错误:“Worker initialization failure, the number of workers exceeds the maximum.”。实际运行的Worker数量会根据当前内存使用情况动态调整。一旦所有Worker和主线程的累积内存占用超过了设定的阈值,系统将触发内存溢出(OOM)错误,导致应用程序崩溃。
160