1# Serialization Types Supported by TaskPool and Worker
2
3Both TaskPool and Worker are developed on the actor model. Due to the memory isolation feature of the actor model, cross-thread serialization is required for the execution of multithreaded tasks and the transmission of results. Data objects that can be transferred are classified into the following types: [common objects](#common-objects), [transferable objects](#transferable-objects), [shared objects](#shared-objects), [native binding objects](#native-binding-objects), and [sendable objects](#sendable-objects).
4
5
6## Common Objects
7
8The 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.
9
10The following object types are supported:
11
12- Basic types except Symbol
13
14- Date
15
16- String
17
18- RegExp
19
20- Array
21
22- Map
23
24- Set
25
26- Object (simple objects only, for example, objects created using **{}** or **new Object**)
27
28  Note that only attributes can be transferred for common objects. Prototypes and methods cannot be transferred.
29
30- ArrayBuffer
31
32- typedArray
33
34
35## Transferable Objects
36
37Transferable objects are ArrayBuffer-type objects whose ownership (but not its content) can be transferred. They are serialized through address transfer. After the ownership of an object is transferred, it becomes unavailable in the sender and can be used only in the receiver.
38
39
40```ts
41// Define a transferable object.
42let buffer: ArrayBuffer = new ArrayBuffer(100);
43```
44
45
46## Shared Objects
47
48A shared object is of the SharedArrayBuffer type, has a fixed length, and can store any type of data including numbers and strings.
49
50An 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.
51
52If multiple operations are simultaneously performed to modify data stored in an object of the SharedArrayBuffer type, you must use atomic operations to ensure data synchronization. Atomic operations ensure that the current operation is complete before the next operation starts.
53
54
55```ts
56import { taskpool } from '@kit.ArkTS';
57
58@Concurrent
59function transferAtomics(arg1: Int32Array) {
60  console.info("wait begin::");
61  // Use Atomics to perform operations.
62  let res = Atomics.wait(arg1, 0, 0, 3000);
63  return res;
64}
65
66// Define an object that can be shared.
67let sab: SharedArrayBuffer = new SharedArrayBuffer(20);
68let int32 = new Int32Array(sab);
69let task: taskpool.Task = new taskpool.Task(transferAtomics, int32);
70taskpool.execute(task).then((res) => {
71  console.info("this res is: " + res);
72});
73setTimeout(() => {
74  Atomics.notify(int32, 0, 1);
75}, 1000);
76```
77
78
79## Native Binding Objects
80
81Native binding objects are provided by the system. They are bound to underlying system services and enable direct access to these services.
82
83Currently, native bound objects that support serialization include [Context](../application-models/application-context-stage.md), [RemoteObject](../reference/apis-ipc-kit/js-apis-rpc.md#remoteobject), and [PixelMap](../reference/apis-image-kit/js-apis-image.md#pixelmap7).
84
85The **Context** object provides the context information of 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).
86
87The **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-ipc-kit/js-apis-rpc.md#remoteobject).
88
89The **PixelMap** object can read or write image data and obtain image information. It is usually used to display images in applications or systems. For details about how to create a **PixelMap** object, see the [API Reference](../reference/apis-image-kit/js-apis-image.md#imagecreatepixelmap8).
90
91
92## Sendable Objects
93
94Sendable objects, decorated by the ArkTS decorator [@Sendable](arkts-sendable.md), can be transferred between threads. Sendable objects support two transfer modes: reference transfer and serialized transfer. For details about the supported Sendable types, see [Sendable Data Types](arkts-sendable.md#sendable-data-types).
95
96
97```ts
98// Use @Sendable to decorate a class object.
99@Sendable
100class TestClass {
101  name: string = "TestClass ";
102  num: number = 5;
103  printName() {
104    console.info("sendable: this class name is: " + this.name);
105  }
106}
107```
108