Beginner: Thread Management in Harmony OS

Lokesh Suryan
6 min readJun 25, 2021

--

Introduction

Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will cover Thread Management with Java in Harmony OS.

Thread is a lightweight process allows a program to operate more efficiently by doing multiple things at the same time. Threads can be used to perform complicated tasks in the background without interrupting the main program.

The system creates a main thread for an application at runtime. The main thread is created or deleted in accordance with the application, so it is regarded as the core thread for an application. All UI-specific operations, such as UI display and update, are running in the main thread. Therefore, the main thread is also called the UI thread. By default, all operations of an application run in the main thread. If there are time-consuming tasks required by the application, such as downloading files and querying the database, you can create other threads to execute such tasks.

When to Use

If an application contains complex service logic, you may need to create multiple threads to execute various tasks, which causes complex interactions between tasks and threads. This may result in more complicated code and higher maintenance cost. To avoid such issues, you can utilize TaskDispatcher to optimize the dispatch of different tasks.

Available APIs

TaskDispatcher is the basic API for Ability instances to dispatch tasks, and it hides the implementation details of the thread where the task is located. TaskDispatcher. By default, tasks running in the UI thread have higher priorities, and tasks without the need of any results to return usually have lower priorities.

We have multiple type of TaskDispatcher major type of task dispatcher are as follows.

1. GlobalTaskDispatcher

The global task dispatcher is obtained by an ability by calling getGlobalTaskDispatcher().

2. ParallelTaskDispatcher

The parallel task dispatcher is created and returned by an ability by calling createParallelTaskDispatcher().

3. SerialTaskDispatcher

The serial task dispatcher is created and returned by an ability by calling createSerialTaskDispatcher().

4. SpecTaskDispatcher

The dedicated task dispatcher is dedicated to a specific thread, which currently refers to the UI thread. Tasks in the UI thread are dispatched using the UITaskDispatcher.

Development Overview

You need to install DevEcho studio IDE and I assume that you have prior knowledge about the Harmony OS and java.

Hardware Requirements

  • A computer (desktop or laptop) running Windows 10.
  • A Huawei phone (with the USB cable), which is used for debugging.

Software Requirements

  • Java JDK installation package.
  • DevEcho studio installed.
  • HMS Core (APK) 4.X or later.

Follows the steps.

1. Create Unity Project.

  • Open DevEcho studio.
  • Click NEW Project, select a Project Template.
  • Select ability template and click Next as per below image.
  • Enter Project and Package Name and click on Finish.

2. Once you have created the project, DevEco Studio will automatically sync it with Gradle files. Find the below image after synchronization is successful.

3. Update Permission and app version in config.json file as per your requirement, otherwise retain the default values.

4. Create New Ability as follows.

4. Development Procedure

Create new Ability MainAbilitySlice.java

Create a layout file under entry > src > main > resources > base > layout ability_main.xml

6. To build apk and run in device, choose Build > Generate Key and CSR Build for Hap(s)\ APP(s) or Build and Run into connected device, follow the steps.

Result

  1. Click on UI Thread|SpecTaskDispatcher Button. It’s bound to the main thread of an application and send result back to main thread and update UI as per below screen.

Pros:- Its will update result in UI thread.Tasks in the UI thread are dispatched using the UITaskDispatcher.
For example if you want fetch data form server and update in UI then you can use this method.
Cons:- If you trying to update UI other than UI thread its will throw “attempt to update UI in non-UI thread” exception.

2. Click on Global Task Dispatcher Button. Its will navigate into other screen, then click on respective button you can separate result.

3. Click on Sync Dispach Button the syncDispatch method dispatches a task synchronously and waits for the task execution in the current thread. The current thread remains blocked until the execution result is returned. As per below result.

Pros :- The syncDispatch method will execute all task synchronously. The current thread remains blocked until the execution result is returned. All synchronized blocks synchronized on the same object can only have one thread executing inside them at a time.

Cons:- If syncDispatch is used incorrectly, a deadlock will occur.

4.Click on delayDispatch button the applyDispatch executes a specified task on multiple times.

Pros:- The delayDispatch method asynchronously dispatches a task with delay and proceeds to the next operation immediately. You can delay your task as per your requirement.

For example if you want execute a method A after 10 second of method B then you can use this delayDispatcher and complete your task easily.

Cons:- If delayDispatch is used incorrectly, its will block your script or can throw ANR.

pros:- The applyDispatch executes a specified task multiple times. As per your requirement you can use applyDispatch method to execute a task multiple time like as fetching data from list of data from database or server.

Cons:- If applyDispatch is used incorrectly, its will execute your script infinitely and can block your UI and other resources

Tips and Tricks

  • Always use the latest version of DevEcho Studio.
  • Use Harmony Device Simulator from HVD section.

Conclusion

In this article, we have learnt Thread Management in Harmony OS. If an application contains complex service logic, you may need to create multiple threads to execute various tasks, which causes intricate interactions between tasks and threads. This may result in more complicated code and higher maintenance cost. To avoid such issues, you can utilize TaskDispatcher to optimize the dispatch of different tasks.

Thanks for reading the article, please do like and comment your queries or suggestions.

References

Harmony OS: https://www.harmonyos.com/en/develop/?ha_source=hms1

Harmony OS Thread Management: https://developer.harmonyos.com/en/docs/documentation/doc-guides/thread-mgmt-overview-0000000000032127?ha_source=hms1

Original Source: https://forums.developer.huawei.com/forumPortal/en/topic/0202599750795440276?ha_source=hms1

--

--

No responses yet