1e41f4b71Sopenharmony_ci# @ohos.worker (启动一个Worker)(系统接口) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciWorker是与主线程并行的独立线程。创建Worker的线程称之为宿主线程,Worker自身的线程称之为Worker线程。创建Worker传入的url文件在Worker线程中执行,可以处理耗时操作但不可以直接操作UI。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciWorker主要作用是为应用程序提供一个多线程的运行环境,可满足应用程序在执行过程中与主线程分离,在后台线程中运行一个脚本操作耗时操作,极大避免类似于计算密集型或高延迟的任务阻塞主线程的运行。由于Worker一旦被创建则不会主动被销毁,若不处于任务状态一直运行,在一定程度上会造成资源的浪费,应及时关闭空闲的Worker。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciWorker的上下文对象和主线程的上下文对象是不同的,Worker线程不支持UI操作。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciWorker使用过程中的相关注意点请查[Worker注意事项](../../arkts-utils/worker-introduction.md#worker注意事项) 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci> **说明:** 12e41f4b71Sopenharmony_ci> 13e41f4b71Sopenharmony_ci> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14e41f4b71Sopenharmony_ci> 15e41f4b71Sopenharmony_ci> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.worker (启动一个Worker)](js-apis-worker.md)。 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## 导入模块 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci```ts 20e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 21e41f4b71Sopenharmony_ci``` 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## RestrictedWorker<sup>11+</sup> 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciRestrictedWorker类继承[ThreadWorker<sup>9+</sup>](js-apis-worker.md#threadworker9),具有ThreadWorker中所有的方法。 26e41f4b71Sopenharmony_ciRestrictedWorker主要作用是提供受限的Worker线程运行环境,该线程运行环境中只允许导入Worker模块,不允许导入其他API。 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci### constructor<sup>11+</sup> 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ciconstructor(scriptURL: string, options?: WorkerOptions) 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ciRestrictedWorker构造函数。使用以下方法前,均需先构造RestrictedWorker实例。 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**系统接口:** 此接口为系统接口。 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**参数:** 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 43e41f4b71Sopenharmony_ci| --------- | ------------------------------- | ---- | ------------------------------------------------------------ | 44e41f4b71Sopenharmony_ci| scriptURL | string | 是 | Worker线程文件的路径,路径规则详细参考[文件路径注意事项](../../arkts-utils/worker-introduction.md#文件路径注意事项)。 | 45e41f4b71Sopenharmony_ci| options | [WorkerOptions](js-apis-worker.md#workeroptions) | 否 | RestrictedWorker构造的选项。 | 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**错误码:** 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 52e41f4b71Sopenharmony_ci| -------- | -------- | 53e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 54e41f4b71Sopenharmony_ci| 10200003 | Worker initialization failure. | 55e41f4b71Sopenharmony_ci| 10200007 | The worker file patch is invalid path. | 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**示例:** 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci此处以在Stage模型中Ability加载Worker文件为例,使用Library加载Worker线程文件的场景参考[文件路径注意事项](../../arkts-utils/worker-introduction.md#文件路径注意事项)。 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci受限的Worker线程文件只允许导入Worker模块,不允许导入任何其他API,以下为示例代码: 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci```ts 64e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci// 主要说明以下两种场景: 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci// 场景1: worker文件所在路径:"entry/src/main/ets/workers/worker.ets" 69e41f4b71Sopenharmony_ciconst workerStageModel01 = new worker.RestrictedWorker('entry/ets/workers/worker.ets', {name:"first worker in Stage model"}); 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci// 场景2: worker文件所在路径:"phone/src/main/ets/ThreadFile/workers/worker.ets" 72e41f4b71Sopenharmony_ciconst workerStageModel02 = new worker.RestrictedWorker('phone/ets/ThreadFile/workers/worker.ets'); 73e41f4b71Sopenharmony_ci``` 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci```ts 76e41f4b71Sopenharmony_ci// 受限worker线程文件 77e41f4b71Sopenharmony_ciimport { worker, MessageEvents } from '@kit.ArkTS'; 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci//import { process } from '@kit.ArkTS'; // 受限Worker线程内不允许导入除了worker之外的API。 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ciconst workerPort = worker.workerPort; 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ciworkerPort.onmessage = (e : MessageEvents) : void => { 84e41f4b71Sopenharmony_ci console.info("worker:: This is worker thread.") 85e41f4b71Sopenharmony_ci //console.info("worker:: worker tid: " + process.tid) // 执行process.tid,主线程会有对应的TypeError报出。 86e41f4b71Sopenharmony_ci} 87e41f4b71Sopenharmony_ci```