• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Thread Model (Stage Model)
2
3A thread is the basic unit for the operating system to perform computing and scheduling. It is an execution flow within a [process](./process-model-stage.md) and shares the resources of the process. A process can contain multiple threads.
4
5## Thread Type
6There are three types of threads in the stage model:
7- Main thread
8  - Draws the UI.
9  - Manages the [ArkTS engine](../arkts-utils/arkts-runtime-overview.md) instance of the main thread so that multiple UIAbility components can run on it.
10  - Manages [ArkTS engine](../arkts-utils/arkts-runtime-overview.md) instances of other threads, for example, using **TaskPool** to create or cancel tasks, and starting and terminating **Worker** threads.
11  - Distributes interaction events.
12  - Processes application code callbacks (event processing and lifecycle management).
13  - Receives messages sent by the **TaskPool** and **Worker** threads.
14- [TaskPool Thread](../reference/apis-arkts/js-apis-taskpool.md)
15
16  Used for time-consuming operations and provides APIs for setting the scheduling priority and load balancing. It is recommended.
17- [Worker Thread](../reference/apis-arkts/js-apis-worker.md)
18
19  Used for time-consuming operations and supports inter-thread communication.
20For details about the operation mechanism, communication method, and usage of **TaskPool** and **Worker**, see [Comparison Between TaskPool and Worker](../arkts-utils/taskpool-vs-worker.md).
21
22![thread-model-stage](figures/thread-model-stage.png)
23
24> **NOTE**
25>
26> - **TaskPool** manages the number of threads that can be created and their lifecycle. However, you need to maintain the lifecycle of [Worker](../arkts-utils/worker-introduction.md) threads.
27> - Multiple components can exist in the same thread. For example, both UIAbility and UI components exist in the main thread. In the stage model, [EventHub](#using-eventhub-for-intra-thread-communication) is used for data communication.
28> - To view thread information about an application process, run the **hdc shell** command to enter the shell CLI of the device, and then run the **ps -p *\<pid>* -T command**, where *\<pid>* indicates the [process ID](process-model-stage.md) of the application.
29
30## Using EventHub for Intra-Thread Communication
31
32[EventHub](../reference/apis-ability-kit/js-apis-inner-application-eventHub.md) provides APIs for sending and processing events in threads, including subscribing to, unsubscribing from, and triggering events. [Using EventHub for Data Synchronization](uiability-data-sync-with-ui.md#using-eventhub-for-data-synchronization) describes the development procedure by using data synchronization between the UIAbility component and the UI as an example.
33