openharmony 鸿蒙 taskpool-vs-worker

2023-10-30 浏览 (697)

Comparison Between TaskPool and Worker

TaskPool and Worker provide a multithreaded runtime environment for applications, allowing them to handle time-consuming computing tasks or resource intensive tasks, preventing tasks from blocking the host thread, and improving system performance and resource utilization.

This topic compares TaskPool and Worker based on their implementation characteristics and suitable use cases.

Implementation Characteristics Comparison

Table 1 Comparison between TaskPool and Worker in implementation characteristics

ItemTaskPoolWorker
Memory modelThreads are isolated from each other, and memory is not shared.Threads are isolated from each other, and memory is not shared.
Parameter passing mechanismThe structured clone algorithm is used for serialization and deserialization.
ArrayBuffer, SharedArrayBuffer, and Sendable are supported.
The structured clone algorithm is used for serialization and deserialization.
ArrayBuffer, SharedArrayBuffer, and Sendable are supported.
Parameter passingParameters are directly passed without being encapsulated.The message object should be encapsulated as a unique parameter.
Method invocationDirectly pass and call methods decorated with @Concurrent.Messages are parsed in the Worker thread to invoke the corresponding methods.
Return valueResults are returned asynchronously by default.Messages must be proactively sent and parsed by calling onmessage() for value assignment.
Lifecycle managementTaskPool automatically manages its lifecycle, without considering task load.You need to manage the number and lifecycle of Worker threads.
Maximum number of tasksThe number is automatically managed, rather than being manually configured.A maximum of 64 Worker threads can run simultaneously in the same process. The actual number is determined by the process memory.
Maximum task duration3 minutes (excluding time spent on Promise and async/await calls, such as network downloads and file read/write operations). There is no upper limit on the duration of continuous tasks.There is no limit.
Task priority settingSetting the task priority is supported.Since API version 18, the Worker thread priority can be configured.
Task cancellationSupported.Not supported.
Thread reuseSupported.Not supported.
Delayed task executionSupported.Not supported.
Task dependency settingSupported.Not supported.
Serial queueSupported.Not supported.
Task groupSupported.Not supported.
Periodic taskSupported.Not supported.
Asynchronous queueSupported.Not supported.

Use Case Comparison

Both TaskPool and Worker support multi-threaded concurrency. TaskPool worker threads are bound to the system scheduling priority and support load balancing (automatic scaling). In contrast, Worker threads need to be created and destroyed manually by developers, incurring certain costs for creation and management. Therefore, TaskPool is recommended in most scenarios.

Worker applies to scenarios where threads need to be occupied for an extended period and the thread lifecycle needs to be managed manually by developers. TaskPool applies to scenarios where relatively independent tasks are executed, and there is no need to focus on thread lifecycle when tasks run in threads.

Recommended Scenarios for Worker

In the following scenarios, tasks usually need to run for a long time or depend on the thread context, making Worker the suitable choice:

  • Tasks with a runtime exceeding 3 minutes

    (The 3-minute duration mentioned here does not include the time consumed by Promise and async/await asynchronous calls, such as I/O tasks like network downloads and file read/write operations):

    For example, use Worker for a background CPU-intensive prediction algorithm that runs for an hour.

    For details about the scenario example, see Resident Task Development.

  • A set of related synchronous tasks

    For example, in scenarios where handles need to be created and used, each newly created handle is unique and must be persistently retained to ensure the correct execution of subsequent operations. Such scenarios are suitable for Worker.

    For details, see Using Worker for Interdependent Synchronous Tasks.

Recommended Scenarios for Worker

In the following scenarios, tasks are typically relatively independent and have higher requirements for scheduling, cancellation, or management capabilities, making TaskPool the suitable choice:

  • Tasks requiring priority setting

    In versions earlier than API version 18, Worker does not support setting scheduling priorities, so TaskPool is necessary.

    Since API version 18, Worker supports priority settings, allowing you to choose between TaskPool and Worker based on your use case and task characteristics.

    For example, in a histogram rendering scenario, the histogram data calculated in the background is used for foreground UI display (which impacts user experience), and the task is relatively independent. Therefore, TaskPool is recommended.

  • Tasks requiring frequent cancellation

    For tasks that need to be canceled often, such as in a Gallery viewing scenario where images on either side of the current image are cached, TaskPool is recommended to efficiently manage the cancellation of a cache task when swiping to the next image.

  • Numerous or dispersed tasks

    For applications with multiple modules containing numerous time-consuming tasks, you are advised to use TaskPool instead of Worker for load management.

    For details about the scenario example, see Batch Database Operations.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 multi-thread-concurrency-overview

openharmony 鸿蒙 arkts-condition-variable-introduction

openharmony 鸿蒙 long-time-task-overview

openharmony 鸿蒙 batch-database-operations-guide

openharmony 鸿蒙 container-object

openharmony 鸿蒙 bytecode-obfuscation-guide

openharmony 鸿蒙 bytecode-obfuscation-questions

openharmony 鸿蒙 napi-define-sendable-object

openharmony 鸿蒙 arkts-lazy-import

openharmony 鸿蒙 independent-time-consuming-task

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/4de0f7354c5f400394f160692865c129