This article aims to deeply explore the technical details of the Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices. It mainly serves as a vehicle for technical sharing and communication. Mistakes and omissions are inevitable. Colleagues are welcome to put forward valuable opinions and questions so that we can make progress together. This article is original content, and any form of reprint must indicate the source and the original author.
Sendable is a mechanism in ArkTS used to achieve the passing of data between concurrent instances. Sendable data can be safely shared among multiple threads, avoiding data race problems and improving the efficiency of concurrent programming.Introduction to the Sendable Protocol
The Sendable protocol defines the sharable object system and its specification constraints in ArkTS. Data that complies with the Sendable protocol (hereinafter referred to as Sendable data) can be passed between concurrent instances in ArkTS.
Characteristics of Sendable Data:
- Serializable: Sendable data can be serialized for passing between concurrent instances.
- Thread-safe: When Sendable data is passed between concurrent instances, the thread safety of the data is ensured.
- Sharing or Copying: When Sendable data is passed between concurrent instances, it can be passed by reference or by copy. ### Reference and Copy Passing of Sendable Data in Multiple Threads Reference Passing:
- When Sendable data is passed between concurrent instances, its reference is copied.
- Concurrent instances can modify the content of the Sendable data, but these modifications will not affect other concurrent instances. Copy Passing:
- When Sendable data is passed between concurrent instances, its content is copied.
- Concurrent instances can modify the content of the Sendable data, but these modifications will not affect the original data of other concurrent instances. ### Choice between Reference Passing and Copy Passing The choice between reference passing and copy passing depends on your specific requirements:
- Reference Passing: It is suitable for situations where the amount of data is small and concurrent instances need to share the data.
- Copy Passing: It is suitable for situations where the amount of data is large and concurrent instances need to operate on the data independently. ### Code Implementation of Creating and Transmitting Sendable Data The following is a simple example demonstrating how to create Sendable data and pass it into the TaskPool:
import { taskpool } from '@kit.ArkTS';
@Sendable
class MyData {
public value: number = 0;
constructor(value: number) {
this.value = value;
}
}
async function processData(data: MyData) {
data.value += 1;
console.log(data.value);
}
async function main() {
const data = new MyData(10);
const task = new taskpool.Task(processData, data);
await taskpool.execute(task);
}
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(async () => {
await main();
})
.width('100%');
}
.height('100%');
}
}
This code defines a component named Index
and displays a text message "Hello World" in the component. Clicking the button will execute the main
function, which creates a Sendable object and passes it into the TaskPool. After the task is completed, the modified data value will be output on the console.
Comparison between Sendable Data Passing Mechanism and Ordinary Data Passing Mechanism
Characteristic | Sendable Data Passing | Ordinary Data Passing |
---|---|---|
Serializable | Supported | Supported |
Thread-safe | Supported | Not Supported |
Sharing or Copying | Supported | Supported |
Passing Efficiency | High | Low |
Summary
Through the above introduction, you can understand the passing mechanism of Sendable data in the HarmonyOS system. Sendable data can be safely passed between concurrent instances, avoiding data race problems and improving the efficiency of concurrent programming. Hope this article can help you master the concurrent programming techniques in the HarmonyOS system and develop better HarmonyOS applications.
Top comments (0)