diff --git a/UseSendable/README.md b/UseSendable/README.md index b89783281e8592ca088d845291cc9f45e7648622..cbac7396c151819d83692aff0337aca58b4e791b 100644 --- a/UseSendable/README.md +++ b/UseSendable/README.md @@ -1,14 +1,23 @@ -# 应用并发设计 +# 应用并发设计同源代码工程 ## 介绍 -本示例为应用并发设计同源代码块。 +本示例为应用并发设计同源代码工程,包含最佳实践文档中包含的推荐使用方法的样例代码。工程本身不具备实际功能,开发者请直接阅读文档结合源码来理解多设备功能开发。 -## 使用说明 -本示例为应用并发设计同源代码块,如需加载相应页面,需要把[EntryAbility.ets](entry/src/main/ets/entryability/EntryAbility.ets)里windowStage.loadContent()第一个参数换成对应页面的url。 +## 效果预览 + +不涉及。 ## 工程目录 ``` +├──entry/src/main/cpp +│ ├──common +│ │ └──log_common.h +│ ├──types/libndkDrawing +│ │ ├──Index.d.ts +│ │ └──oh-package.json5 +│ ├──CMakeLists.txt +│ └──native_bridge.cpp // Native侧暴露绘制接口nativeOnDraw供前端调用 ├──entry/src/main/ets // 代码区 │ ├──entryability │ │ └──EntryAbility.ets // 程序入口 @@ -24,15 +33,18 @@ │ │ ├──ConcurrentTaskManagement5.ets // 任务延时调度 │ │ ├──Demo.ets │ │ ├──freezeObj.ts -│ │ ├──InterthreadCommunication1.ets // 跨语言多线程通信(C++与ArkTS) -│ │ ├──InterthreadCommunication2.ets // 线程间模块共享(单例模式) -│ │ ├──InterthreadCommunication3.ets // 线程间模块共享(单例模式) -│ │ └──InterthreadCommunication4.ets // 线程间不可变数据共享 +│ │ ├──InterThreadCommunication1.ets // 跨语言多线程通信(C++与ArkTS) +│ │ ├──InterThreadCommunication2.ets // 线程间模块共享(单例模式) +│ │ ├──InterThreadCommunication3.ets // 线程间模块共享(单例模式) +│ │ └──InterThreadCommunication4.ets // 线程间不可变数据共享 │ └──workers │ └──Worker.ets // 线程间不可变数据共享 └──entry/src/main/resources // 应用资源目录 ``` +## 使用说明 +不涉及 + ## 相关权限 不涉及。 diff --git a/UseSendable/entry/src/main/ets/entryability/EntryAbility.ets b/UseSendable/entry/src/main/ets/entryability/EntryAbility.ets index 0b432ab688f6a7553b06daa4d78e0cc7b56cec46..ed0dbf30a46b68f1b77419f864b947de439fcfbe 100644 --- a/UseSendable/entry/src/main/ets/entryability/EntryAbility.ets +++ b/UseSendable/entry/src/main/ets/entryability/EntryAbility.ets @@ -34,7 +34,7 @@ export default class EntryAbility extends UIAbility { // Main window is created, set main page for this ability hilog.info(DOMAIN, TAG, FORMAT, 'Ability onWindowStageCreate'); - windowStage.loadContent('pages/InterthreadCommunication2', (err) => { + windowStage.loadContent('pages/Index', (err) => { if (err.code) { hilog.error(DOMAIN, TAG, FORMAT, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; diff --git a/UseSendable/entry/src/main/ets/pages/Index.ets b/UseSendable/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..837a1e56fd947e6890e1506ed6d3ded1f935ef6d --- /dev/null +++ b/UseSendable/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { InterThreadCommunication1 } from './InterThreadCommunication1'; +import { InterThreadCommunication2 } from './InterThreadCommunication2'; +import { InterThreadCommunication3 } from './InterThreadCommunication3'; + +const DOMAIN = 0x0000; +const TAG = 'Index'; + +@Entry +@Component +struct Index { + private pageStack: NavPathStack = new NavPathStack(); + + @Styles + buttonStyles() { + .width('100%') + .margin({ + bottom: 12 + }) + } + + @Builder + pageMap(pageName: string): void { + if (pageName === 'InterThreadCommunication1') { + InterThreadCommunication1() + } else if (pageName === 'InterThreadCommunication2') { + InterThreadCommunication2() + } else if (pageName === 'InterThreadCommunication3') { + InterThreadCommunication3() + } + } + + build() { + Navigation(this.pageStack) { + Column() { + Button('跨语言多线程通信(C++与ArkTS)') + .buttonStyles() + .onClick(() => { + this.pageStack.pushDestinationByName('InterThreadCommunication1', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + + Button('线程间模块共享(单例模式)') + .width('100%') + .buttonStyles() + .onClick(() => { + this.pageStack.pushDestinationByName('InterThreadCommunication2', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + + Button('线程间模块共享(单例模式)') + .width('100%') + .buttonStyles() + .onClick(() => { + this.pageStack.pushDestinationByName('InterThreadCommunication3', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + left: 24 + }) + } + .title($r('app.string.home_title')) + .navDestination(this.pageMap) + .hideToolBar(true) + } +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/InterthreadCommunication1.ets b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication1.ets similarity index 64% rename from UseSendable/entry/src/main/ets/pages/InterthreadCommunication1.ets rename to UseSendable/entry/src/main/ets/pages/InterThreadCommunication1.ets index c60819d4f754197c5b0f4316cac6c4cfed982979..4be6ab975af643d380fdde2e21eec67f3edb8235 100644 --- a/UseSendable/entry/src/main/ets/pages/InterthreadCommunication1.ets +++ b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication1.ets @@ -21,27 +21,41 @@ import { hilog } from '@kit.PerformanceAnalysisKit'; import nativeModule from 'libentry.so'; // [StartExclude inter_thread_communication1] const DOMAIN = 0x0000; -const TAG = 'InterthreadCommunication1'; +const TAG = 'InterThreadCommunication1'; const FORMAT = '%{public}s'; // [EndExclude inter_thread_communication1] -@Entry @Component -struct InterthreadCommunication1 { +export struct InterThreadCommunication1 { build() { - Button('click me') - .onClick(() => { - nativeModule.nativeCall((a: number) => { - hilog.info(DOMAIN, TAG, FORMAT, 'Received data from thread-function: %{public}d', a); - }) + NavDestination() { + Column() { + Button('click me') + .width('100%') + .onClick(() => { + nativeModule.nativeCall((a: number) => { + hilog.info(DOMAIN, TAG, FORMAT, 'Received data from thread-function: %{public}d', a); + }) + }) + } + // [StartExclude inter_thread_communication1] + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .alignItems(HorizontalAlign.Center) + .padding({ + right: 24, + bottom: 12, + left: 24 }) + // [EndExclude inter_thread_communication1] + } } } // [End inter_thread_communication1] @Component -struct TestCode { +export struct TestCode { // [Start index_btn] - // Index.ets build() { Button('click me') .onClick(() => { @@ -51,6 +65,4 @@ struct TestCode { }) } // [End index_btn] -} - -TestCode() \ No newline at end of file +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/InterthreadCommunication2.ets b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication2.ets similarity index 89% rename from UseSendable/entry/src/main/ets/pages/InterthreadCommunication2.ets rename to UseSendable/entry/src/main/ets/pages/InterThreadCommunication2.ets index 5e95b9db8aa864cb5b8f6917c37cd32e61f8459d..0f58486ab05d744d89cb91080488b6b5ff9f91a0 100644 --- a/UseSendable/entry/src/main/ets/pages/InterthreadCommunication2.ets +++ b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication2.ets @@ -17,7 +17,6 @@ * 最佳实践: 应用并发设计 线程间通信 线程间模块共享(单例模式) (方案一) */ // [Start store] -// Index.ets import { taskpool } from '@kit.ArkTS'; import singleton from 'libentry.so'; import { hilog } from '@kit.PerformanceAnalysisKit'; @@ -45,13 +44,13 @@ function store(a: number, b: number, c: number) { hilog.info(0x0000, 'TAG', '%{public}s', 'set size is ' + size + ' after store'); } -@Entry @Component -struct Index { +export struct InterThreadCommunication2 { build() { - Row() { + NavDestination() { Column() { Button('TestSingleton') + .width('100%') .onClick(() => { let address = singleton.getAddress(); hilog.info(DOMAIN, TAG, FORMAT, `host thread address is ${address}`); @@ -74,9 +73,18 @@ struct Index { }); }) } + // [StartExclude store] .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .alignItems(HorizontalAlign.Center) + .padding({ + right: 24, + bottom: 12, + left: 24 + }) + // [EndExclude store] } - .height('100%') } } // [End store] \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/InterthreadCommunication3.ets b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication3.ets similarity index 86% rename from UseSendable/entry/src/main/ets/pages/InterthreadCommunication3.ets rename to UseSendable/entry/src/main/ets/pages/InterThreadCommunication3.ets index 4059bca3421c26dc649ea23f55cb1f932f4894de..0772d1084138bb2e3d1737d0b79a5a91286988ca 100644 --- a/UseSendable/entry/src/main/ets/pages/InterthreadCommunication3.ets +++ b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication3.ets @@ -20,10 +20,10 @@ import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { taskpool } from '@kit.ArkTS'; -import { Demo } from './demo'; +import { Demo } from './Demo'; // [StartExclude taskpool] const DOMAIN = 0x0000; -const TAG = 'InterthreadCommunication3'; +const TAG = 'InterThreadCommunication3'; const FORMAT = '%{public}s'; // [EndExclude taskpool] @Concurrent @@ -43,10 +43,12 @@ async function executeTaskPool(): Promise { executeTaskPool(); // [End initsingleton] -@Entry @Component -struct Index { +export struct InterThreadCommunication3 { build() { - Text('Sendable实现线程间模块共享(单例模式)') + NavDestination() { + Text('Sendable实现线程间模块共享(单例模式)') + .margin({ top: 24 }) + } } } \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/InterthreadCommunication4.ets b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication4.ets similarity index 100% rename from UseSendable/entry/src/main/ets/pages/InterthreadCommunication4.ets rename to UseSendable/entry/src/main/ets/pages/InterThreadCommunication4.ets index d4a64c3de9ed46704389305b1beafeb524dfc09a..ef71a0877f31932b3af13c7db5447758ace0d93e 100644 --- a/UseSendable/entry/src/main/ets/pages/InterthreadCommunication4.ets +++ b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication4.ets @@ -17,9 +17,9 @@ * 最佳实践: 应用并发设计 线程间通信 线程间不可变数据共享 */ // [Start globalconfig] +import { hilog } from '@kit.PerformanceAnalysisKit'; import { worker } from '@kit.ArkTS'; import { freezeObj } from './freezeObj'; -import { hilog } from '@kit.PerformanceAnalysisKit'; // [StartExclude globalconfig] const DOMAIN = 0x0000; const TAG = 'ConcurrencyCapabilitySelection2'; diff --git a/UseSendable/entry/src/main/ets/workers/Worker.ets b/UseSendable/entry/src/main/ets/workers/Worker.ets index 69a22bfe459c1a2fa673759e7cd8a3450e5527d7..fcc6627ffb5f2ff88e585eb35d0ff3c4ad163932 100644 --- a/UseSendable/entry/src/main/ets/workers/Worker.ets +++ b/UseSendable/entry/src/main/ets/workers/Worker.ets @@ -20,7 +20,7 @@ // The worker file path is: entry/ets/workers/Worker.ets // Worker.ets import { MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; -import { GlobalConfig } from '../pages/InterthreadCommunication4'; +import { GlobalConfig } from '../pages/InterThreadCommunication4'; import { hilog } from '@kit.PerformanceAnalysisKit'; const workerPort: ThreadWorkerGlobalScope = worker.workerPort; diff --git a/UseSendable/entry/src/main/resources/base/element/string.json b/UseSendable/entry/src/main/resources/base/element/string.json index f94595515a99e0c828807e243494f57f09251930..5973ea126f6a098beee50917de41911730a8aff7 100644 --- a/UseSendable/entry/src/main/resources/base/element/string.json +++ b/UseSendable/entry/src/main/resources/base/element/string.json @@ -10,7 +10,11 @@ }, { "name": "EntryAbility_label", - "value": "label" + "value": "UseSendable" + }, + { + "name": "home_title", + "value": "UseSendable Homologous Engineering" } ] } \ No newline at end of file diff --git a/UseSendable/entry/src/main/resources/base/profile/main_pages.json b/UseSendable/entry/src/main/resources/base/profile/main_pages.json index 0addf20f9f9b1a97a01180ec249301b72f5cba20..1898d94f58d6128ab712be2c68acc7c98e9ab9ce 100644 --- a/UseSendable/entry/src/main/resources/base/profile/main_pages.json +++ b/UseSendable/entry/src/main/resources/base/profile/main_pages.json @@ -1,7 +1,5 @@ { "src": [ - "pages/InterthreadCommunication1", - "pages/InterthreadCommunication2", - "pages/InterthreadCommunication3" + "pages/Index" ] } diff --git a/UseSendable/entry/src/main/resources/en_US/element/string.json b/UseSendable/entry/src/main/resources/en_US/element/string.json index f94595515a99e0c828807e243494f57f09251930..5973ea126f6a098beee50917de41911730a8aff7 100644 --- a/UseSendable/entry/src/main/resources/en_US/element/string.json +++ b/UseSendable/entry/src/main/resources/en_US/element/string.json @@ -10,7 +10,11 @@ }, { "name": "EntryAbility_label", - "value": "label" + "value": "UseSendable" + }, + { + "name": "home_title", + "value": "UseSendable Homologous Engineering" } ] } \ No newline at end of file diff --git a/UseSendable/entry/src/main/resources/zh_CN/element/string.json b/UseSendable/entry/src/main/resources/zh_CN/element/string.json index 597ecf95e61d7e30367c22fe2f8638008361b044..aa260ead9fe1539b350f8a5af5cca960bd882dfb 100644 --- a/UseSendable/entry/src/main/resources/zh_CN/element/string.json +++ b/UseSendable/entry/src/main/resources/zh_CN/element/string.json @@ -10,7 +10,11 @@ }, { "name": "EntryAbility_label", - "value": "label" + "value": "UseSendable" + }, + { + "name": "home_title", + "value": "应用并发设计同源代码工程" } ] } \ No newline at end of file