Intermediate: Inter Thread Communication in Harmony OS

Lokesh Suryan
4 min readJul 2, 2021

--

Introduction

When you need to process a time-consuming operation. For example, executing a download task in the current thread without blocking the thread, try out the EventHandler to achieve efficient communications between different threads in HarmonyOS. You can use EventRunner to create another thread to run time-consuming tasks without blocking the current thread. In this way, all types of tasks can run more smoothly and effectively in different threads. For example, you can use EventHandler in the main thread to create a child thread to download an image. The EventHandler will notify the main thread when the image is downloaded in child thread, and the main thread can update the UI display.

Limitations and Constraints

  • During inter-thread communication, the EventHandler can be bound only to the threads created by the EventRunner. When creating an EventRunner instance, ensure that the creation is successful. An EventHandler can be bounded to threads created by an EventRunner instance only when the EventRunner instance is not null.
  • An EventHandler can be bounded only one EventRunner at a time. However, an EventRunner can be bounded to multiple EventHandler instances at the same time.

When to Use

Development Scenarios of EventHandler

The EventHandler dispatches InnerEvent instances or Runnable tasks to other threads for processing, including the following situations:

  • An InnerEvent instance needs to be dispatched to a new thread and to be processed based on the priority and delay time. The priority of an InnerEvent can be IMMEDIATE, HIGH, LOW, or IDLE, and the delay time can be specified.
  • A Runnable task needs to be dispatched to a new thread and to be processed based on the priority and delay time. The priority of a Runnable task can be IMMEDIATE, HIGH, LOW, or IDLE, and the delay time can be specified.
  • An event needs to be dispatched from a new thread to the original thread for processing.

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 Templet.
  • 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.

5. Development Procedure.

Add the below code in MainAbilitySlice.java

Add the below code in 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 Inter Thread Communication button. It will execute an image download time-consuming operation task in the current thread without blocking the thread.

Pros: You can perform time consuming operation without blocking current thread.

Cons: If you trying to execute time consuming operation without

2. Click on‘Banking System’ button, it will communicate between two thread.

Pros: You can communicate between two threads. Only single thread to access the shared resource.

Cons: Increase the waiting time of the thread. Create performance problem.

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 Inter Thread Management in Harmony OS. EventHandler mainly used for time-consuming operation task in current thread without blocking the thread, try out the EventHandler to achieve efficient communications between different threads in HarmonyOS.

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:

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

--

--

No responses yet