diff --git a/UseSendable/README.md b/UseSendable/README.md index cbac7396c151819d83692aff0337aca58b4e791b..cf5ce732e8564abf7b17aeea434b13cc4a8196c7 100644 --- a/UseSendable/README.md +++ b/UseSendable/README.md @@ -43,7 +43,7 @@ ``` ## 使用说明 -不涉及 +不涉及。 ## 相关权限 不涉及。 diff --git a/UseSendable/entry/src/main/ets/entryability/EntryAbility.ets b/UseSendable/entry/src/main/ets/entryability/EntryAbility.ets index ed0dbf30a46b68f1b77419f864b947de439fcfbe..097098abe7df2557e45a659835b4d938f024d09a 100644 --- a/UseSendable/entry/src/main/ets/entryability/EntryAbility.ets +++ b/UseSendable/entry/src/main/ets/entryability/EntryAbility.ets @@ -40,6 +40,8 @@ export default class EntryAbility extends UIAbility { return; } hilog.info(DOMAIN, TAG, FORMAT, 'Succeeded in loading the content.'); + // Immersive Adaptation + this.immersionFuc(windowStage); }); } @@ -57,4 +59,23 @@ export default class EntryAbility extends UIAbility { // Ability has back to background hilog.info(DOMAIN, TAG, FORMAT, 'Ability onBackground'); } -} + + /** + * Page immersion. + */ + immersionFuc(windowStage: window.WindowStage): void { + try { + let windowClass: window.Window = windowStage.getMainWindowSync(); + windowClass.setWindowLayoutFullScreen(true); + let navigationBarArea: window.AvoidArea = + windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); + let area: window.AvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); + AppStorage.setOrCreate('naviIndicatorHeight', + windowClass.getUIContext().px2vp(navigationBarArea.bottomRect.height)); + AppStorage.setOrCreate('statusBarHeight', windowClass.getUIContext().px2vp(area.topRect.height)); + AppStorage.setOrCreate('windowClass', windowClass); + } catch (error) { + hilog.info(DOMAIN, TAG, FORMAT, `immersionFuc failed, error code=${error.code}, message=${error.message}`); + } + } +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/ConcurrencyCapabilitySelection1.ets b/UseSendable/entry/src/main/ets/pages/ConcurrencyCapabilitySelection1.ets index cdd671ce5146f30e6665159284a91eaf34fa6860..891244d4f754da07aaa5bb6fa6d4c16daee05c06 100644 --- a/UseSendable/entry/src/main/ets/pages/ConcurrencyCapabilitySelection1.ets +++ b/UseSendable/entry/src/main/ets/pages/ConcurrencyCapabilitySelection1.ets @@ -20,19 +20,57 @@ import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { taskpool } from '@kit.ArkTS'; + // [StartExclude taskpool] const DOMAIN = 0x0000; const TAG = 'ConcurrencyCapabilitySelection2'; const FORMAT = '%{public}s'; + // [EndExclude taskpool] @Concurrent async function foo(a: number, b: number) { return a + b; } -taskpool.execute(foo, 1, 2).then((ret: Object) => { // Result processing - hilog.info(DOMAIN, TAG, FORMAT, 'Return:' + ret); -}).catch((err: BusinessError) => { - hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); -}); -// [End taskpool] \ No newline at end of file +function executeTaskPool() { + taskpool.execute(foo, 1, 2).then((ret: Object) => { // Result processing + hilog.info(DOMAIN, TAG, FORMAT, 'Return:' + ret); + }).catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); + }); +} + +// [End taskpool] + +@Component +export struct ConcurrencyCapabilitySelection1 { + @Styles + buttonStyles() { + .width('100%') + .height(40) + } + + build() { + NavDestination() { + Column() { + Button($r('app.string.time_consuming_task_title')) + .buttonStyles() + .margin({ + bottom: 16 + }) + .onClick(() => { + executeTaskPool(); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + bottom: 16, + left: 24 + }) + } + .title($r('app.string.time_consuming_task_title')) + } +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/ConcurrencyCapabilitySelection2.ets b/UseSendable/entry/src/main/ets/pages/ConcurrencyCapabilitySelection2.ets index d9cee2895a45cd7bc678fd1dd74cd499e707fa8a..87125a77851bf4ec2ce6b57acd362e7af721d3b8 100644 --- a/UseSendable/entry/src/main/ets/pages/ConcurrencyCapabilitySelection2.ets +++ b/UseSendable/entry/src/main/ets/pages/ConcurrencyCapabilitySelection2.ets @@ -17,9 +17,9 @@ * 最佳实践: 应用并发设计 并发能力选择 长时任务并发执行场景 */ // [Start execute_taskpool] -import { taskpool } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; +import { taskpool } from '@kit.ArkTS'; // [StartExclude taskpool] const DOMAIN = 0x0000; const TAG = 'ConcurrencyCapabilitySelection2'; @@ -48,6 +48,37 @@ function executeTaskPool() { hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); }); } +// [End execute_taskpool] -executeTaskPool(); -// [End execute_taskpool] \ No newline at end of file +@Component +export struct ConcurrencyCapabilitySelection2 { + @Styles + buttonStyles() { + .width('100%') + .height(40) + } + + build() { + NavDestination() { + Column() { + Button($r('app.string.long_term_task_title')) + .buttonStyles() + .margin({ + bottom: 16 + }) + .onClick(() => { + executeTaskPool(); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + bottom: 16, + left: 24 + }) + } + .title($r('app.string.long_term_task_title')) + } +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement1.ets b/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement1.ets index c2201a9b9eaa4698d20c936a620b67a53402bbb6..9dd099b619800d7db9a40473c21eb022e23cec43 100644 --- a/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement1.ets +++ b/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement1.ets @@ -70,4 +70,36 @@ async function seqRunner() { hilog.info(DOMAIN, TAG, FORMAT, 'SequenceRunner: result is ' + result); } // [End seq_runner] -seqRunner(); \ No newline at end of file + +@Component +export struct ConcurrentTaskManagement1 { + @Styles + buttonStyles() { + .width('100%') + .height(40) + } + + build() { + NavDestination() { + Column() { + Button($r('app.string.multitasking_title')) + .buttonStyles() + .margin({ + bottom: 16 + }) + .onClick(() => { + seqRunner(); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + bottom: 16, + left: 24 + }) + } + .title($r('app.string.multitasking_title')) + } +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement2.ets b/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement2.ets index ef670f6ffed85b61ebe32b5e94796b52fb7f5fec..0298060c682cf126c6d9f69f2149cc07e671860c 100644 --- a/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement2.ets +++ b/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement2.ets @@ -41,33 +41,69 @@ function updateSAB(args: Uint32Array) { return 0; } -let sab = new SharedArrayBuffer(20); -let typedArray = new Uint32Array(sab); -let task1 = new taskpool.Task(updateSAB, typedArray); -let task2 = new taskpool.Task(updateSAB, typedArray); -let task3 = new taskpool.Task(updateSAB, typedArray); -try { - task1.addDependency(task2); - task2.addDependency(task3); -} catch (err) { - hilog.error(DOMAIN, TAG, FORMAT, `sendData failed. Cause code: ${err.code},message: ${err.message}`); +function executeTaskPool() { + let sab = new SharedArrayBuffer(20); + let typedArray = new Uint32Array(sab); + let task1 = new taskpool.Task(updateSAB, typedArray); + let task2 = new taskpool.Task(updateSAB, typedArray); + let task3 = new taskpool.Task(updateSAB, typedArray); + try { + task1.addDependency(task2); + task2.addDependency(task3); + } catch (err) { + hilog.error(DOMAIN, TAG, FORMAT, `sendData failed. Cause code: ${err.code},message: ${err.message}`); + } + + taskpool.execute(task1).then((res: object) => { + hilog.info(DOMAIN, TAG, FORMAT, 'taskpool:: execute task1 res: ' + res); + }).catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); + }); + + taskpool.execute(task2).then((res: object) => { + hilog.info(DOMAIN, TAG, FORMAT, 'taskpool:: execute task2 res: ' + res); + }).catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); + }); + + taskpool.execute(task3).then((res: object) => { + hilog.info(DOMAIN, TAG, FORMAT, 'taskpool:: execute task3 res: ' + res); + }).catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); + }); } -taskpool.execute(task1).then((res: object) => { - hilog.info(DOMAIN, TAG, FORMAT, 'taskpool:: execute task1 res: ' + res); -}).catch((err: BusinessError) => { - hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); -}); +// [End updatesab] -taskpool.execute(task2).then((res: object) => { - hilog.info(DOMAIN, TAG, FORMAT, 'taskpool:: execute task2 res: ' + res); -}).catch((err: BusinessError) => { - hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); -}); +@Component +export struct ConcurrentTaskManagement2 { + @Styles + buttonStyles() { + .width('100%') + .height(40) + } -taskpool.execute(task3).then((res: object) => { - hilog.info(DOMAIN, TAG, FORMAT, 'taskpool:: execute task3 res: ' + res); -}).catch((err: BusinessError) => { - hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); -}); -// [End updatesab] \ No newline at end of file + build() { + NavDestination() { + Column() { + Button($r('app.string.tree_like_dependency_title')) + .buttonStyles() + .margin({ + bottom: 16 + }) + .onClick(() => { + executeTaskPool(); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + bottom: 16, + left: 24 + }) + } + .title($r('app.string.tree_like_dependency_title')) + } +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement3.ets b/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement3.ets index f00c2ea5892c604ab8f38c327bd95a59acb2b7e2..ff2f9cffeee5f3adf264228a34c5d505398eeed2 100644 --- a/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement3.ets +++ b/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement3.ets @@ -17,9 +17,9 @@ * 最佳实践: 应用并发设计 并发任务管理 多任务同步等待结果(任务组) */ // [Start image_processing] -import { taskpool } from '@kit.ArkTS'; -import { hilog } from '@kit.PerformanceAnalysisKit'; import { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { taskpool } from '@kit.ArkTS'; // [StartExclude image_processing] const DOMAIN = 0x0000; @@ -50,11 +50,48 @@ function histogramStatistic(pixelBuffer: ArrayBuffer): void { hilog.error(DOMAIN, TAG, FORMAT, `addTask failed. Cause code: ${err.code},message: ${err.message}`); } } - taskpool.execute(taskGroup, taskpool.Priority.HIGH).then((res: Object[]): void | Promise => { - // Result data processing - hilog.info(DOMAIN, TAG, FORMAT, `res:${res}`); - }).catch((error: BusinessError) => { + try { + taskpool.execute(taskGroup, taskpool.Priority.HIGH).then((res: Object[]): void | Promise => { + // Result data processing + hilog.info(DOMAIN, TAG, FORMAT, `res:${res}`); + }).catch((error: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, `taskpool excute error: ${error}`); + }); + } catch (error) { hilog.error(DOMAIN, TAG, FORMAT, `taskpool excute error: ${error}`); - }); + } } -// [End image_processing] \ No newline at end of file +// [End image_processing] + +@Component +export struct ConcurrentTaskManagement3 { + @Styles + buttonStyles() { + .width('100%') + .height(40) + } + + build() { + NavDestination() { + Column() { + Button($r('app.string.task_set_title')) + .buttonStyles() + .margin({ + bottom: 16 + }) + .onClick(() => { + histogramStatistic(new ArrayBuffer(32)); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + bottom: 16, + left: 24 + }) + } + .title($r('app.string.task_set_title')) + } +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement4.ets b/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement4.ets index 4e3c08a20b7748d04076ff1d7b2837fbdb58f4fe..cab001075ce198e0e05c369db6aa81451c0981c6 100644 --- a/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement4.ets +++ b/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement4.ets @@ -17,16 +17,16 @@ * 最佳实践: 应用并发设计 并发任务管理 多任务优先级调度 */ // [Start execcolorinfo] +import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { taskpool } from '@kit.ArkTS'; -import { BusinessError } from '@kit.BasicServicesKit'; // [StartExclude execcolorinfo] const DOMAIN = 0x0000; const TAG = 'ConcurrentTaskManagement4'; const FORMAT = '%{public}s'; // [EndExclude execcolorinfo] -function exec(bufferArray: ArrayBuffer): void { +function executeTaskPool(bufferArray: ArrayBuffer): void { taskpool.execute(execColorInfo, bufferArray, taskpool.Priority.HIGH).catch((error: BusinessError) => { hilog.error(DOMAIN, TAG, FORMAT, `taskpool excute error: ${error}`); }); @@ -45,4 +45,37 @@ async function execColorInfo(bufferArray: ArrayBuffer): Promise { } return newBufferArr; } -// [End execcolorinfo] \ No newline at end of file +// [End execcolorinfo] + +@Component +export struct ConcurrentTaskManagement4 { + @Styles + buttonStyles() { + .width('100%') + .height(40) + } + + build() { + NavDestination() { + Column() { + Button($r('app.string.priority_scheduling_title')) + .buttonStyles() + .margin({ + bottom: 16 + }) + .onClick(() => { + executeTaskPool(new ArrayBuffer(16)); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + bottom: 16, + left: 24 + }) + } + .title($r('app.string.priority_scheduling_title')) + } +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement5.ets b/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement5.ets index 77ca85dfa2d5b6aaea8363fa983e6652836c3c9e..6e87b37f8ef656e991e8b12373d9ccaedc0997f8 100644 --- a/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement5.ets +++ b/UseSendable/entry/src/main/ets/pages/ConcurrentTaskManagement5.ets @@ -17,9 +17,9 @@ * 最佳实践: 应用并发设计 并发任务管理 任务延时调度 */ // [Start quick_start] +import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { taskpool } from '@kit.ArkTS'; -import { BusinessError } from '@kit.BasicServicesKit'; // [StartExclude quick_start] const DOMAIN = 0x0000; @@ -32,13 +32,48 @@ function concurrentTask(num: number): number { return num; } -// create a task -let task: taskpool.Task = new taskpool.Task(concurrentTask, 100); -// Delayed execution of task -taskpool.executeDelayed(3000, task, taskpool.Priority.HIGH).then((value: Object) => { - // Processing delayed task results - hilog.info(DOMAIN, TAG, FORMAT, 'taskpool result: ' + value); -}).catch((err: BusinessError) => { - hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); -}); -// [End quick_start] \ No newline at end of file +function executeTaskPool() { + // create a task + let task: taskpool.Task = new taskpool.Task(concurrentTask, 100); + // Delayed execution of task + taskpool.executeDelayed(3000, task, taskpool.Priority.HIGH).then((value: Object) => { + // Processing delayed task results + hilog.info(DOMAIN, TAG, FORMAT, 'taskpool result: ' + value); + }).catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); + }); +} +// [End quick_start] + +@Component +export struct ConcurrentTaskManagement5 { + @Styles + buttonStyles() { + .width('100%') + .height(40) + } + + build() { + NavDestination() { + Column() { + Button($r('app.string.task_delay_scheduling_title')) + .buttonStyles() + .margin({ + bottom: 16 + }) + .onClick(() => { + executeTaskPool(); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + bottom: 16, + left: 24 + }) + } + .title($r('app.string.task_delay_scheduling_title')) + } +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/ets/pages/Index.ets b/UseSendable/entry/src/main/ets/pages/Index.ets index 837a1e56fd947e6890e1506ed6d3ded1f935ef6d..76214544c40de92e1ed44488517ca2e7ecdb32e5 100644 --- a/UseSendable/entry/src/main/ets/pages/Index.ets +++ b/UseSendable/entry/src/main/ets/pages/Index.ets @@ -12,81 +12,233 @@ * 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 { BusinessError } from '@kit.BasicServicesKit'; +import { ConcurrencyCapabilitySelection1 } from './ConcurrencyCapabilitySelection1'; +import { ConcurrencyCapabilitySelection2 } from './ConcurrencyCapabilitySelection2'; +import { ConcurrentTaskManagement1 } from './ConcurrentTaskManagement1'; +import { ConcurrentTaskManagement2 } from './ConcurrentTaskManagement2'; +import { ConcurrentTaskManagement3 } from './ConcurrentTaskManagement3'; +import { ConcurrentTaskManagement4 } from './ConcurrentTaskManagement4'; +import { ConcurrentTaskManagement5 } from './ConcurrentTaskManagement5'; import { InterThreadCommunication1 } from './InterThreadCommunication1'; import { InterThreadCommunication2 } from './InterThreadCommunication2'; import { InterThreadCommunication3 } from './InterThreadCommunication3'; +import { InterThreadCommunication4 } from './InterThreadCommunication4'; const DOMAIN = 0x0000; const TAG = 'Index'; +const FORMAT = '%{public}s'; @Entry @Component struct Index { private pageStack: NavPathStack = new NavPathStack(); + private scroller: Scroller = new Scroller(); + + getResourceString(resource: Resource): string { + let resourceString: string = ''; + try { + resourceString = this.getUIContext().getHostContext()?.resourceManager.getStringSync(resource.id) as string; + } catch (error) { + hilog.error(DOMAIN, TAG, FORMAT, `getResourceString error. Cause code: ${error.code},message: ${error.message}`); + } + return resourceString; + } @Styles buttonStyles() { .width('100%') - .margin({ - bottom: 12 - }) + .height(40) } @Builder pageMap(pageName: string): void { - if (pageName === 'InterThreadCommunication1') { + if (pageName === 'ConcurrencyCapabilitySelection1') { + ConcurrencyCapabilitySelection1() + } else if (pageName === 'ConcurrencyCapabilitySelection2') { + ConcurrencyCapabilitySelection2() + } else if (pageName === 'ConcurrentTaskManagement1') { + ConcurrentTaskManagement1() + } else if (pageName === 'ConcurrentTaskManagement2') { + ConcurrentTaskManagement2() + } else if (pageName === 'ConcurrentTaskManagement3') { + ConcurrentTaskManagement3() + } else if (pageName === 'ConcurrentTaskManagement4') { + ConcurrentTaskManagement4() + } else if (pageName === 'ConcurrentTaskManagement5') { + ConcurrentTaskManagement5() + } else if (pageName === 'InterThreadCommunication1') { InterThreadCommunication1() } else if (pageName === 'InterThreadCommunication2') { InterThreadCommunication2() } else if (pageName === 'InterThreadCommunication3') { InterThreadCommunication3() + } else if (pageName === 'InterThreadCommunication4') { + InterThreadCommunication4() } } 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}`); - }); - }) + Scroll(this.scroller) { + Column() { + Button($r('app.string.time_consuming_task_title')) + .buttonStyles() + .margin({ + bottom: 12 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('ConcurrencyCapabilitySelection1', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + + Button($r('app.string.long_term_task_title')) + .buttonStyles() + .margin({ + bottom: 12 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('ConcurrencyCapabilitySelection2', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + + Button($r('app.string.multitasking_title')) + .buttonStyles() + .margin({ + bottom: 12 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('ConcurrentTaskManagement1', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + + Button($r('app.string.tree_like_dependency_title')) + .buttonStyles() + .margin({ + bottom: 12 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('ConcurrentTaskManagement2', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + + Button($r('app.string.task_set_title')) + .buttonStyles() + .margin({ + bottom: 12 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('ConcurrentTaskManagement3', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + + Button($r('app.string.priority_scheduling_title')) + .buttonStyles() + .margin({ + bottom: 12 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('ConcurrentTaskManagement4', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + + Button($r('app.string.task_delay_scheduling_title')) + .buttonStyles() + .margin({ + bottom: 12 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('ConcurrentTaskManagement5', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `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($r('app.string.multithreaded_communication_title')) + .buttonStyles() + .margin({ + bottom: 12 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('InterThreadCommunication1', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `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}`); - }); - }) + Button($r('app.string.singleton_pattern_title')) + .buttonStyles() + .margin({ + bottom: 12 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('InterThreadCommunication2', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + Button($r('app.string.singleton_solution_two_title')) + .buttonStyles() + .margin({ + bottom: 12 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('InterThreadCommunication3', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + Button($r('app.string.immutable_data_sharing_title')) + .buttonStyles() + .margin({ + bottom: 32 + }) + .onClick(() => { + this.pageStack.pushDestinationByName('InterThreadCommunication4', null, true) + .catch((err: BusinessError) => { + hilog.error(DOMAIN, TAG, FORMAT, + `pushDestinationByName failed. Code: ${err.code}, message: ${err.message}`); + }); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + left: 24 + }) } .width('100%') .height('100%') - .justifyContent(FlexAlign.End) - .padding({ - right: 24, - left: 24 - }) + .scrollBar(BarState.Off) } - .title($r('app.string.home_title')) + .margin({ top: 28 }) + .title('UseSendable') .navDestination(this.pageMap) .hideToolBar(true) } diff --git a/UseSendable/entry/src/main/ets/pages/InterThreadCommunication1.ets b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication1.ets index 4be6ab975af643d380fdde2e21eec67f3edb8235..e87eb91a8623a5571e614ff82d49e766df075bc2 100644 --- a/UseSendable/entry/src/main/ets/pages/InterThreadCommunication1.ets +++ b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication1.ets @@ -29,7 +29,7 @@ export struct InterThreadCommunication1 { build() { NavDestination() { Column() { - Button('click me') + Button($r('app.string.multithreaded_communication_title')) .width('100%') .onClick(() => { nativeModule.nativeCall((a: number) => { @@ -44,25 +44,12 @@ export struct InterThreadCommunication1 { .alignItems(HorizontalAlign.Center) .padding({ right: 24, - bottom: 12, + bottom: 32, left: 24 }) // [EndExclude inter_thread_communication1] } + .title($r('app.string.multithreaded_communication_title')) } } -// [End inter_thread_communication1] - -@Component -export struct TestCode { - // [Start index_btn] - build() { - Button('click me') - .onClick(() => { - nativeModule.nativeCall((a: number) => { - hilog.info(DOMAIN, TAG, FORMAT, 'Received data from thread-function: %{public}d', a); - }) - }) - } - // [End index_btn] -} \ No newline at end of file +// [End inter_thread_communication1] \ 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 index 0f58486ab05d744d89cb91080488b6b5ff9f91a0..e9b59431d499fed217e7b9f0e9f9510c83e43ad4 100644 --- a/UseSendable/entry/src/main/ets/pages/InterThreadCommunication2.ets +++ b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication2.ets @@ -17,10 +17,10 @@ * 最佳实践: 应用并发设计 线程间通信 线程间模块共享(单例模式) (方案一) */ // [Start store] +import { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; import { taskpool } from '@kit.ArkTS'; import singleton from 'libentry.so'; -import { hilog } from '@kit.PerformanceAnalysisKit'; -import { BusinessError } from '@kit.BasicServicesKit'; // [StartExclude store] const DOMAIN = 0x0000; @@ -49,7 +49,7 @@ export struct InterThreadCommunication2 { build() { NavDestination() { Column() { - Button('TestSingleton') + Button($r('app.string.singleton_pattern_title')) .width('100%') .onClick(() => { let address = singleton.getAddress(); @@ -80,11 +80,12 @@ export struct InterThreadCommunication2 { .alignItems(HorizontalAlign.Center) .padding({ right: 24, - bottom: 12, + bottom: 32, left: 24 }) // [EndExclude store] } + .title($r('app.string.singleton_pattern_title')) } } // [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 index 0772d1084138bb2e3d1737d0b79a5a91286988ca..24caafa9fd6a7b50dd35aff61ba96264eff52887 100644 --- a/UseSendable/entry/src/main/ets/pages/InterThreadCommunication3.ets +++ b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication3.ets @@ -39,16 +39,38 @@ async function executeTaskPool(): Promise { hilog.error(DOMAIN, TAG, FORMAT, `taskpool execute error. Cause code: ${err.code},message: ${err.message}`); }); } - -executeTaskPool(); // [End initsingleton] @Component export struct InterThreadCommunication3 { + + @Styles + buttonStyles() { + .width('100%') + .height(40) + } + build() { NavDestination() { - Text('Sendable实现线程间模块共享(单例模式)') - .margin({ top: 24 }) + Column() { + Button($r('app.string.singleton_solution_two_title')) + .buttonStyles() + .margin({ + bottom: 16 + }) + .onClick(() => { + executeTaskPool(); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + bottom: 16, + left: 24 + }) } + .title($r('app.string.singleton_solution_two_title')) } } \ 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 index ef71a0877f31932b3af13c7db5447758ace0d93e..7516e8772607c40f32cdab0291ec2d1b157c43e1 100644 --- a/UseSendable/entry/src/main/ets/pages/InterThreadCommunication4.ets +++ b/UseSendable/entry/src/main/ets/pages/InterThreadCommunication4.ets @@ -34,12 +34,47 @@ export class GlobalConfig { } } -try { - let globalConfig = new GlobalConfig(); - globalConfig.init(); - const workerInstance = new worker.ThreadWorker('entry/ets/workers/Worker.ets`', { name: 'Worker1' }); - workerInstance.postMessage(globalConfig); -} catch (err) { - hilog.error(DOMAIN, TAG, FORMAT, `postMessage failed. Cause code: ${err.code},message: ${err.message}`); +function executeTaskPool() { + try { + let globalConfig = new GlobalConfig(); + globalConfig.init(); + const workerInstance = new worker.ThreadWorker('entry/ets/workers/Worker.ets`', { name: 'Worker1' }); + workerInstance.postMessage(globalConfig); + } catch (err) { + hilog.error(DOMAIN, TAG, FORMAT, `postMessage failed. Cause code: ${err.code},message: ${err.message}`); + } } -// [End globalconfig] \ No newline at end of file +// [End globalconfig] + +@Component +export struct InterThreadCommunication4 { + @Styles + buttonStyles() { + .width('100%') + .height(40) + } + + build() { + NavDestination() { + Column() { + Button($r('app.string.immutable_data_sharing_title')) + .buttonStyles() + .margin({ + bottom: 16 + }) + .onClick(() => { + executeTaskPool(); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.End) + .padding({ + right: 24, + bottom: 16, + left: 24 + }) + } + .title($r('app.string.immutable_data_sharing_title')) + } +} \ No newline at end of file diff --git a/UseSendable/entry/src/main/resources/base/element/string.json b/UseSendable/entry/src/main/resources/base/element/string.json index 5973ea126f6a098beee50917de41911730a8aff7..03f32d9b3c7e24927fb7fb730c549bc06e7f9055 100644 --- a/UseSendable/entry/src/main/resources/base/element/string.json +++ b/UseSendable/entry/src/main/resources/base/element/string.json @@ -15,6 +15,50 @@ { "name": "home_title", "value": "UseSendable Homologous Engineering" + }, + { + "name": "time_consuming_task_title", + "value": "Scenarios for concurrent execution of time-consuming tasks" + }, + { + "name": "long_term_task_title", + "value": "Scenario of concurrent execution of long-duration tasks" + }, + { + "name": "multitasking_title", + "value": "Multi-tasking concurrent execution (serial sequential dependency)" + }, + { + "name": "tree_like_dependency_title", + "value": "Multi-task concurrent execution (tree-like dependency)" + }, + { + "name": "task_set_title", + "value": "Multi-task synchronous waiting for results (task group)" + }, + { + "name": "priority_scheduling_title", + "value": "Multi-task priority scheduling" + }, + { + "name": "task_delay_scheduling_title", + "value": "Task Delay Scheduling" + }, + { + "name": "multithreaded_communication_title", + "value": "Cross-language multithreaded communication (C++ and ArkTS)" + }, + { + "name": "singleton_pattern_title", + "value": "Inter-thread module sharing (Singleton pattern) (Solution 1)" + }, + { + "name": "singleton_solution_two_title", + "value": "Module sharing among threads (Singleton pattern)" + }, + { + "name": "immutable_data_sharing_title", + "value": "Shared immutable data among threads" } ] } \ No newline at end of file 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 5973ea126f6a098beee50917de41911730a8aff7..03f32d9b3c7e24927fb7fb730c549bc06e7f9055 100644 --- a/UseSendable/entry/src/main/resources/en_US/element/string.json +++ b/UseSendable/entry/src/main/resources/en_US/element/string.json @@ -15,6 +15,50 @@ { "name": "home_title", "value": "UseSendable Homologous Engineering" + }, + { + "name": "time_consuming_task_title", + "value": "Scenarios for concurrent execution of time-consuming tasks" + }, + { + "name": "long_term_task_title", + "value": "Scenario of concurrent execution of long-duration tasks" + }, + { + "name": "multitasking_title", + "value": "Multi-tasking concurrent execution (serial sequential dependency)" + }, + { + "name": "tree_like_dependency_title", + "value": "Multi-task concurrent execution (tree-like dependency)" + }, + { + "name": "task_set_title", + "value": "Multi-task synchronous waiting for results (task group)" + }, + { + "name": "priority_scheduling_title", + "value": "Multi-task priority scheduling" + }, + { + "name": "task_delay_scheduling_title", + "value": "Task Delay Scheduling" + }, + { + "name": "multithreaded_communication_title", + "value": "Cross-language multithreaded communication (C++ and ArkTS)" + }, + { + "name": "singleton_pattern_title", + "value": "Inter-thread module sharing (Singleton pattern) (Solution 1)" + }, + { + "name": "singleton_solution_two_title", + "value": "Module sharing among threads (Singleton pattern)" + }, + { + "name": "immutable_data_sharing_title", + "value": "Shared immutable data among threads" } ] } \ 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 aa260ead9fe1539b350f8a5af5cca960bd882dfb..5de6fc19df37ca3f5b6fdda41b6fa39b0927def6 100644 --- a/UseSendable/entry/src/main/resources/zh_CN/element/string.json +++ b/UseSendable/entry/src/main/resources/zh_CN/element/string.json @@ -15,6 +15,50 @@ { "name": "home_title", "value": "应用并发设计同源代码工程" + }, + { + "name": "time_consuming_task_title", + "value": "耗时任务并发执行场景" + }, + { + "name": "long_term_task_title", + "value": "长时任务并发执行场景" + }, + { + "name": "multitasking_title", + "value": "多任务关联执行(串行顺序依赖)" + }, + { + "name": "tree_like_dependency_title", + "value": "多任务关联执行(树状依赖)" + }, + { + "name": "task_set_title", + "value": "多任务同步等待结果(任务组)" + }, + { + "name": "priority_scheduling_title", + "value": "多任务优先级调度" + }, + { + "name": "task_delay_scheduling_title", + "value": "任务延时调度" + }, + { + "name": "multithreaded_communication_title", + "value": "跨语言多线程通信(C++与ArkTS)" + }, + { + "name": "singleton_pattern_title", + "value": "线程间模块共享(单例模式) (方案一)" + }, + { + "name": "singleton_solution_two_title", + "value": "线程间模块共享(单例模式)" + }, + { + "name": "immutable_data_sharing_title", + "value": "线程间不可变数据共享" } ] } \ No newline at end of file