• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Comparison Between TaskPool and Worker
2
3
4**TaskPool** and **Worker** provide a multithread running environment for applications to process time-consuming computing tasks or resource intensive tasks, preventing these tasks from blocking the main thread. This maximizes system utilization, reduces overall resource consumption, and improves overall system performance.
5
6## Implementation Feature Comparison
7
8**Table 1** Comparison between TaskPool and Worker in implementation features
9
10| Item| TaskPool | Worker |
11| -------- | -------- | -------- |
12| Memory model| Threads are isolated from each other, and memory is not shared.| Threads are isolated from each other, and memory is not shared.|
13| Parameter passing mechanism| The structured clone algorithm is used for serialization and deserialization.<br>ArrayBuffer and SharedArrayBuffer are used for parameter passing and sharing.| The structured clone algorithm is used for serialization and deserialization.<br>ArrayBuffer and SharedArrayBuffer are used for parameter passing and sharing.|
14| Parameter passing| Parameters are passed in without being encapsulated.| Only one parameter can be carried in a message object. Therefore, you must encapsulate excess parameters.|
15| Method invocation| Methods are directly passed in and called.| Methods are called after corresponding messages have been passed in and parsed in the worker thread.|
16| Return value| A value is returned by default after asynchronous calling.| Messages proactively sent must be parsed and assigned by calling **onmessage()**.|
17| Lifecycle| The task pool manages its own lifecycle, without considering the load.| You are required to manage the number and lifecycle of worker threads.|
18| Maximum number of tasks| The number is automatically managed, rather than being manually configured.| A maximum of eight worker threads can run simultaneously in the same process.|
19| Maximum task execution duration| 3 minutes (excluding the time used for Promise or async/await asynchronous call, for example, the time consumed by I/O tasks such as network download and file read/write)| There is no restriction.|
20| Task priority setting| Supported.| Not supported.|
21| Task cancellation| Supported.| Not supported.|
22| Thread reuse| Supported.| Not supported.|
23| Delayed task execution| Supported.| Not supported.|
24| Setting task dependencies| Supported.| Not supported.|
25| Serial queue| Supported.| Not supported.|
26| Task group| Supported.| Not supported.|
27
28
29## Use Case Comparison
30
31Both **TaskPool** and **Worker** support multithread concurrency. **TaskPool** worker threads are bound to the system scheduling priority and support load balancing (automatic scaling). **Worker** threads are manually created and do not support scheduling priority setting. Therefore, **TaskPool** provides better performance than **Worker** and is recommended in most scenarios.
32
33**TaskPool** is oriented to independent tasks. These tasks are executed in threads and automatically reclaimed by the system if they are running for more than 3 minutes. Therefore, you do not need to care about the thread lifecycle. On the contrary, **Worker** is oriented to threads and supports thread execution for a long time. You need to manage the thread lifecycle.
34
35Common use cases are as follows:
36
37- Use **Worker** for a task that runs for more than 3 minutes (excluding the time used for Promise or async/await asynchronous call, for example, I/O tasks such as network download and file read/write). For example, use **Worker** for a 1-hour prediction algorithm training job in the background.
38
39- Use **Worker** for a series of associated synchronous tasks. For example, in scenarios where handles are used, if different handles are created each time and they must be stored permanently for subsequent operation, use **Worker**.
40
41- Use **TaskPool** for a task for which the priority needs to be set. For example, in the histogram rendering scenario in Gallery, histogram data calculated in the background is used for UI display on the foreground. Therefore, histogram data calculation must be treated with high priority. In this case, use **TaskPool**.
42
43- Use **TaskPool** for a task that is subject to cancellation at any time. For example, in the large image browsing scenario in Gallery, two images before and after the current image are cached. When the user swipes one side, the image cache task on the other side needs to be canceled. In this case, use **TaskPool**.
44
45- Use **TaskPool** for a large number of tasks or tasks with scattered scheduling points. For example, a large application with multiple modules may have multiple time-consuming tasks, and it is inconvenient to use multiple worker threads to manage load. In this case, **TaskPool** is recommended.
46