• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Freezing Sendable Objects
2
3Sendable objects can be frozen, making them read-only and preventing any additions, deletions, or modifications to their properties. Once frozen, these objects can be safely accessed across multiple concurrent instances without the need for locks. This is achieved using the [Object.freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) method.
4
5> **NOTE**
6>
7> The **Object.freeze** API cannot be used in .ets files.
8
9## Usage Example
10
111. Encapsulate the **Object.freeze** method in a TS file.
12
13   ```ts
14   // helper.ts
15   export function freezeObj(obj: any) {
16     Object.freeze(obj);
17   }
18   ```
19   <!-- @[provide_encapsulate_method](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/SendableObjectRelated/entry/src/main/ets/managers/helper.ts) -->
20
212. Call the **freeze** method to freeze an object and send it to a child thread.
22
23   ```ts
24   // Index.ets
25   import { freezeObj } from './helper';
26   import { worker } from '@kit.ArkTS';
27
28   @Sendable
29   export class GlobalConfig {
30     // Configuration properties and methods
31     init() {
32       // Initialization logic
33       freezeObj(this) // Freeze the object after the initialization is complete.
34     }
35   }
36
37   @Entry
38   @Component
39   struct Index {
40     build() {
41       Column() {
42         Text("Sendable freezeObj Test")
43           .id('HelloWorld')
44           .fontSize(50)
45           .fontWeight(FontWeight.Bold)
46           .onClick(() => {
47             let gConfig = new GlobalConfig();
48             gConfig.init();
49             const workerInstance = new worker.ThreadWorker('entry/ets/workers/Worker.ets', { name: "Worker1" });
50             workerInstance.postMessage(gConfig);
51           })
52       }
53       .height('100%')
54       .width('100%')
55     }
56   }
57   ```
58   <!-- @[freeze_obj_send_child_thread](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/SendableObjectRelated/entry/src/main/ets/managers/SendableFreeze.ets) -->
59
603. Perform operations on the frozen object directly in the child thread without locking.
61
62   ```ts
63   // Worker.ets
64   import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
65   import { GlobalConfig } from '../pages/Index';
66
67   const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
68   workerPort.onmessage = (e: MessageEvents) => {
69     let gConfig: GlobalConfig = e.data;
70     // Use the gConfig object.
71   }
72   ```
73   <!-- @[directly_operate_obj](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/SendableObjectRelated/entry/src/main/ets/workers/Worker.ets) -->
74