• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Multithread Concurrency Overview
2
3
4## Overview
5
6Concurrency models are used to implement concurrent tasks in different scenarios. Common concurrency models are classified into shared memory models and message passing models.
7
8A typical message passing model is actor. It provides a relatively high degree of concurrency while eliminating a series of complex and occasional issues caused by locks. For these reasons, ArkTS chooses the actor model.
9
10Due to the memory isolation feature of the actor model, cross-thread serialization is required.
11
12
13## Data Transfer Objects
14
15Data objects that can be transferred are classified into the following types: [common objects](#common-objects), [transferable objects](#transferable-objects), [shared objects](#shared-objects), and [native binding objects](#native-binding-objects).
16
17
18### Common Objects
19
20The structured clone algorithm is used for serialization of common objects. This algorithm recursively transfers an object by clone. It supports more object types than other serialization algorithms.
21
22The following object types are supported: basic types except Symbol, Date, String, RegExp, Array, Map, Set, Object (simple objects only, for example, objects created using **{}** or **new Object**), ArrayBuffer, and typedArray. (Note that only attributes can be transferred for common objects. Prototypes and methods cannot be transferred.)
23
24
25### Transferable Objects
26
27Transferable objects are serialized through address transfer. It transfers the ownership of an object of the ArrayBuffer type, rather than the content in it. After the ownership is transferred, the object becomes unavailable in the sender and can be used only in the receiver.
28
29
30```js
31// Define a transferable object.
32let buffer = new ArrayBuffer(100);
33```
34
35
36### Shared Objects
37
38A shared object is of the **SharedArrayBuffer** type, has a fixed length, and can store any type of data including numbers and strings.
39
40An object of the SharedArrayBuffer type can be transferred between multiple threads. The objects before and after the transfer point to the same memory block, achieving memory sharing.
41
42If multiple operations are simultaneously performed to modify data stored in an object of the SharedArrayBuffer type, you must use atomics to ensure data synchronization. Atomics ensure that the current operation is complete before the next operation starts.
43
44
45```js
46// Define a shared object, which uses atomics to ensure data synchronization.
47let sharedBuffer = new SharedArrayBuffer(1024);
48```
49
50
51### Native Binding Objects
52
53Native binding objects are provided by the system. They are bound to underlying system services and enables direct access to these services.
54
55Currently, native bound objects that support serialization include [Context](../application-models/application-context-stage.md) and [RemoteObject](../reference/apis/js-apis-rpc.md#remoteobject).
56
57The **Context** object provides the context information about an application component. It provides a way to access system services and resources so that the application component can interact with the system. For details about how to obtain context information, see [Context (Stage Model)](../application-models/application-context-stage.md).
58
59The **RemoteObject** object implements remote communication. It transfers the reference of an object between processes so that these processes can share the status and methods of the object. The service provider must inherit this class. For details about how to create a **RemoteObject** object, see [RemoteObject](../reference/apis/js-apis-rpc.md#remoteobject).
60
61## TaskPool and Worker
62
63ArkTS provides two multithread concurrency capabilities: **TaskPool** and **Worker**, which differ in their implementation features and use cases. For details, see [Comparison Between TaskPool and Worker](taskpool-vs-worker.md).
64