diff --git a/OAT.xml b/OAT.xml index 1a67ebc2da5f0708956df8d1dd45227f848fd947..412b52053da8b977b0fe8e37ad4b053211de7c29 100644 --- a/OAT.xml +++ b/OAT.xml @@ -103,6 +103,10 @@ Note:If the text contains special characters, please escape them according to th + + + + @@ -2083,6 +2087,11 @@ Note:If the text contains special characters, please escape them according to th + + + + + @@ -2150,6 +2159,11 @@ Note:If the text contains special characters, please escape them according to th + + + + + diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/.gitignore b/code/ArkTS1.2/ThreadSafeContainersSample/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/app.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8b63a1f32a1fac14c4bafea21e3625acc92d49b4 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/app.json5 @@ -0,0 +1,10 @@ +{ + "app": { + "bundleName": "com.samples.threadsafecontainerssample", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/resources/base/element/string.json b/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..dabdd969b5f7715611cef8e2e3f14d07522b2deb --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "ThreadSafeContainersSample" + } + ] +} diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/resources/base/media/app_icon.png b/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/resources/base/media/app_icon.png differ diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/build-profile.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d5b13fa941f6f3912e974615e2e9a02f1a819a02 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/build-profile.json5 @@ -0,0 +1,42 @@ +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "arkTSVersion": "1.2", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/code-linter.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..073990fa45394e1f8e85d85418ee60a8953f9b99 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/code-linter.json5 @@ -0,0 +1,32 @@ +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + "@security/no-unsafe-aes": "error", + "@security/no-unsafe-hash": "error", + "@security/no-unsafe-mac": "warn", + "@security/no-unsafe-dh": "error", + "@security/no-unsafe-dsa": "error", + "@security/no-unsafe-ecdsa": "error", + "@security/no-unsafe-rsa-encrypt": "error", + "@security/no-unsafe-rsa-sign": "error", + "@security/no-unsafe-rsa-key": "error", + "@security/no-unsafe-dsa-key": "error", + "@security/no-unsafe-dh-key": "error", + "@security/no-unsafe-3des": "error" + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/.gitignore b/code/ArkTS1.2/ThreadSafeContainersSample/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/build-profile.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8ac81840b69fd7c78c2024c6570ed01e4292cf6d --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/build-profile.json5 @@ -0,0 +1,29 @@ +{ + "apiType": "stageMode", + "arkTSVersion": "1.2", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/hvigorfile.ts b/code/ArkTS1.2/ThreadSafeContainersSample/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/obfuscation-rules.txt b/code/ArkTS1.2/ThreadSafeContainersSample/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/oh-package.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..248c3b7541a589682a250f86a6d3ecf7414d2d6a --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/oh-package.json5 @@ -0,0 +1,10 @@ +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/entryability/EntryAbility.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b892d18a00d879e20ee20840d94a2e06f10ae1c5 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,31 @@ +import UIAbility from '@ohos.app.ability.UIAbility'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; +import Want from '@ohos.app.ability.Want'; +import window from '@ohos.window'; +import { BusinessError } from '@ohos.base'; +import hilog from '@ohos.hilog'; +import { MyCallback } from '../models/MyCallback'; + + +class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', 'EntryAbility onCreate'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + hilog.info(0x0000, 'testTag', 'EntryAbility onWindowStageCreate'); + try { + windowStage.loadContent('pages/Index', (err: BusinessError): void => { + hilog.info(0x0000, 'testTag', 'loadContent entering'); + MyCallback.runCasesOfCallback(); + if (err.code) { + hilog.info(0x0000, 'testTag', 'loadContent error'); + return; + } + hilog.info(0x0000, 'testTag', 'loadContent ok'); + }); + } catch (e: Error) { + hilog.info(0x0000, 'testTag', 'loadContent catch error:-----------' + e.message); + } + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/models/MyCallback.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/models/MyCallback.ets new file mode 100644 index 0000000000000000000000000000000000000000..5827b846dc824ef26c25e0a8612717cb7b448a3a --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/models/MyCallback.ets @@ -0,0 +1,38 @@ +import { AsyncCallback, BusinessError, Callback, ErrorCallback } from '@ohos.base'; +import hilog from '@ohos.hilog'; + +const SYNTAX_ERROR_CODE: double = 1002; + +export class MyCallback { + static myCallback(callback: Callback): void { + hilog.info(0x0000, 'testTag', 'myCallback'); + callback('myCallback'); + } + + static myAsyncCallback(callback: AsyncCallback): void { + hilog.info(0x0000, 'testTag', 'myAsyncCallback'); + let error = new Error('Business Error', 'basicError2 message', undefined); + let be2: BusinessError = new BusinessError(SYNTAX_ERROR_CODE, error); + callback(be2, 'yAsyncCallback'); + } + + static myErrorCallback(callback: ErrorCallback>): void { + hilog.info(0x0000, 'testTag', 'myErrorCallback'); + let error = new Error('Business Error', 'myErrorCallback message', undefined); + let result: string = 'test'; + let be: BusinessError = new BusinessError(SYNTAX_ERROR_CODE, result, error); + callback(be); + } + + static runCasesOfCallback() { + MyCallback.myCallback((data: string) => { + hilog.info(0x0000, 'testTag', 'MyCallback' + data); + }) + MyCallback.myAsyncCallback((error: BusinessError, result: string) => { + hilog.info(0x0000, 'testTag', `myAsyncCallback, error: ${error.message}, result:${result}`); + }) + MyCallback.myErrorCallback((error: BusinessError) => { + hilog.info(0x0000, 'testTag', `myAsyncCallback, error: ${error.message}`); + }) + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/Index.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..98546ebff6b8c8c2ae054256ce39fff76667f922 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,80 @@ +import { + memo, + __memo_context_type, + __memo_id_type +} from '@ohos.arkui.stateManagement' // should be insert by ui-plugins +import { + Text, + TextAttribute, + Column, + Component, + Button, + ButtonAttribute, + ClickEvent, + UserView +} from '@ohos.arkui.component' // TextAttribute should be insert by ui-plugins +import { + State, + StateDecoratedVariable, + MutableState, + stateOf, + observableProxy +} from '@ohos.arkui.stateManagement' // should be insert by ui-plugins +import hilog from '@ohos.hilog' +import { ArrayBlockingQueueTest } from './containers/ArrayBlockingQueueTest'; +import { ConcurrentHashMapTest } from './containers/ConcurrentHashMapTest'; +import { ConcurrentSetTest } from './containers/ConcurrentSetTest'; +import { LinkedBlockingQueueTest } from './containers/LinkedBlockingQueueTest'; + + +@Component +struct MyStateSample { + aboutToAppear() { + let arrayQueue = new ArrayBlockingQueueTest(); + arrayQueue.basicScene(); + arrayQueue.concurrentInQueue(); + arrayQueue.concurrentOutQueue(); + arrayQueue.nullBlocking(); + let map = new ConcurrentHashMapTest(); + map.basicScene(); + map.concurrentInQueue(); + map.concurrentOutQueue(); + map.hashMapBenchmark(); + let set = new ConcurrentSetTest(); + set.basicScene(); + set.concurrentInSet(); + set.concurrentOutSet(); + let linkedQueue = new LinkedBlockingQueueTest(); + linkedQueue.basicScene(); + linkedQueue.concurrentInQueue(); + linkedQueue.concurrentOutQueue(); + linkedQueue.fullBlocking(); + linkedQueue.nullBlocking(); + } + + @State stateVar: string = 'state var'; + message: string = 'var'; + + build() { + Column(undefined) { + Button(this.message).backgroundColor('#FFFF00FF') + .onClick((e: ClickEvent) => { + hilog.info(0x0000, 'testTag', 'On Click'); + }) + Text(this.stateVar).fontSize(20) + } + } +} + +export class ComExampleTrivialApplication extends UserView { + getBuilder() { + hilog.info(0x0000, 'testTag', 'getBuilder'); + let wrapper = @ + memo() => + { + hilog.info(0x0000, 'testTag', 'MyStateSample'); + MyStateSample(undefined) + } + return wrapper + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ArrayBlockingQueueTest.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ArrayBlockingQueueTest.ets new file mode 100644 index 0000000000000000000000000000000000000000..83802c8a2e54b3357090d5e571c5504c9537a617 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ArrayBlockingQueueTest.ets @@ -0,0 +1,118 @@ +import hilog from '@ohos.hilog'; + +function pushData1(queue: ArrayBlockingQueue, logs: Array) { + for (let i: int = 0; i < 100; i++) { + queue.push(i); + logs.push(`push: ${i}`); + } +} + +function pushData2(queue: ArrayBlockingQueue, logs: Array) { + for (let i: int = 100; i < 200; i++) { + queue.push(i); + logs.push(`push: ${i}`); + } +} + +function popData1(queue: ArrayBlockingQueue, logs: Array) { + for (let i = 0; i < 100; i++) { + queue.pop(); + logs.push(`pop: ${i}`); + } +} + +function popData2(queue: ArrayBlockingQueue, logs: Array) { + for (let i = 100; i < 200; i++) { + queue.pop(); + logs.push(`pop: ${i}`); + } +} + +function nullBlockingPop(queue: ArrayBlockingQueue, logs: Array) { + for (let i = 0; i < 3; i++) { + logs.push(`pop: ${i}`); + queue.pop(); + logs.push(`删除${i}成功 `); + } +} + +function nullBlockingPush(queue: ArrayBlockingQueue, logs: Array) { + for (let i = 0; i < 3; i++) { + logs.push(`push: ${i}`); + queue.push(i); + logs.push(`添加${i}成功 `); + } +} + +export class ArrayBlockingQueueTest { + basicScene() { + const queueCapacity: int = 10; + let queueInt: ArrayBlockingQueue = new ArrayBlockingQueue(queueCapacity); + let logs: Array = new Array(); + for (let i: int = 0; i < queueCapacity; i++) { + queueInt.push(i); + logs.push(`push: ${i}`); + } + const firstData = queueInt.getFirst(); + logs.push(`getFirst: ${firstData}`); + const endData = queueInt.getEnd(); + logs.push(`getEnd: ${endData}`); + const popData = queueInt.pop(); + logs.push(`pop: ${popData}`); + const queueSize = queueInt.size; + logs.push(`size: ${queueSize}`); + const isEmpty = queueInt.isEmpty(); + logs.push(`isEmpty: ${isEmpty}`); + hilog.info(0x0000, 'testTag', 'ArrayBlockingQueue basicScene ' + logs); + } + + concurrentInQueue() { + const queueCapacity: int = 200; + let queueInt: ArrayBlockingQueue = new ArrayBlockingQueue(queueCapacity); + let logs: Array = new Array(); + let p1 = launch + pushData1(queueInt, logs); + let p2 = launch + pushData2(queueInt, logs); + await p1; + await p2; + const queueSize = queueInt.size; + if (queueCapacity === queueSize) { + logs.push('测试成功'); + } + hilog.info(0x0000, 'testTag', 'ArrayBlockingQueue concurrentInQueue ' + logs); + } + + concurrentOutQueue() { + const queueCapacity = 200; + let queueInt: ArrayBlockingQueue = new ArrayBlockingQueue(queueCapacity); + let logs: Array = new Array(); + for (let i: int = 0; i < queueCapacity; i++) { + queueInt.push(i); + } + let p1 = launch + popData1(queueInt, logs); + let p2 = launch + popData2(queueInt, logs); + await p1; + await p2; + const isEmpty = queueInt.isEmpty(); + if (isEmpty) { + logs.push('测试成功'); + } + hilog.info(0x0000, 'testTag', 'ArrayBlockingQueue concurrentOutQueue ' + logs); + } + + nullBlocking() { + const queueCapacity = 3; + let queueInt: ArrayBlockingQueue = new ArrayBlockingQueue(queueCapacity); + let logs: Array = new Array(); + let p1 = launch + nullBlockingPop(queueInt, logs); + let p2 = launch + nullBlockingPush(queueInt, logs); + await p1; + await p2; + hilog.info(0x0000, 'testTag', 'ArrayBlockingQueue nullBlocking ' + logs); + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentHashMapTest.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentHashMapTest.ets new file mode 100644 index 0000000000000000000000000000000000000000..e0c8c69ae6109c61187b27fd15ce4e5906d42f2c --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentHashMapTest.ets @@ -0,0 +1,196 @@ +import hilog from '@ohos.hilog'; + +const MAP_CAPACITY: number = 100; + +function setData1(map: ConcurrentHashMap, logs: Array) { + for (let i = 0; i < 50; i++) { + map.set(i, `v${i}`); + logs.push(`set key: ${i} value: v${i}`); + } +} + +function setData2(map: ConcurrentHashMap, logs: Array) { + for (let i = 50; i < 100; i++) { + map.set(i, `v${i}`); + logs.push(`set key: ${i} value: v${i}`); + } +} + +function deleteData1(map: ConcurrentHashMap, logs: Array) { + for (let i = 0; i < 50; i++) { + let isDelete = map.delete(i); + if (isDelete) { + logs.push(`delete key: ${i} 成功`); + } + } +} + +function deleteData2(map: ConcurrentHashMap, logs: Array) { + for (let i = 50; i < 100; i++) { + let isDelete = map.delete(i); + if (isDelete) { + logs.push(`delete key: ${i} 成功`); + } + } +} + +function setLargeData(map: ConcurrentHashMap, logs: ArrayBlockingQueue, quantity: number) { + for (let i = quantity; i < quantity * 2; i++) { + map.set(i, `v${i}`); + logs.push(`set key: ${i} value: v${i}`); + } +} + +function getLargeData(map: ConcurrentHashMap, logs: ArrayBlockingQueue, quantity: number) { + for (let i = quantity; i < quantity * 2; i++) { + let getData = map.get(i); + logs.push(`get key: ${i} value: ${getData}`); + } +} + +export class ConcurrentHashMapTest { + basicScene() { + let hashMapInt: ConcurrentHashMap = new ConcurrentHashMap(); + let logs: Array = new Array(); + for (let i = 0; i < 10; i++) { + hashMapInt.set(i, `v${i}`); + logs.push(`set: k${i}, v${i}`); + } + logs.push(`当前map: ${hashMapInt}`); + const getKey = 1; + const getData = hashMapInt.get(1); + logs.push(`get key=: ${getKey} value=: ${getData}`); + const hasKey = 2; + let isHas = hashMapInt.has(2); + logs.push(`key: ${hasKey} 是否存在: ${isHas}`); + const keys = hashMapInt.keys(); + let keysStr = ''; + while (true) { + const v = keys.next() + if (v.done) { + break; + } + let value = v.value; + keysStr += value + ' '; + } + ; + logs.push(`keys: ${keysStr}`); + const values = hashMapInt.values(); + let valuesStr = ''; + while (true) { + const v = values.next() + if (v.done) { + break; + } + let value = v.value; + valuesStr += value + ' '; + } + ; + logs.push(`values: ${valuesStr}`); + const entries = hashMapInt.entries(); + let entriesStr = ''; + while (true) { + const v = entries.next() + if (v.done) { + break; + } + let value = v.value; + entriesStr += value + ' '; + } + ; + logs.push(`entries: ${entriesStr}`); + const deleteKey = 3; + const isDelete = hashMapInt.delete(3); + if (isDelete) { + logs.push(`delete key= ${deleteKey},删除成功`); + } else { + logs.push(`delete key= ${deleteKey},删除失败`); + } + const removeKey = 4; + let isRemove = hashMapInt.remove(4); + if (isRemove) { + logs.push(`remove key= ${removeKey},移除成功`); + } else { + logs.push(`remove key= ${removeKey},移除失败`); + } + const replaceKey = 5; + const isReplace = hashMapInt.replace(5, 'five'); + if (isReplace) { + logs.push(`replace key= ${replaceKey},替换成功`); + } else { + logs.push(`replace key= ${replaceKey},替换失败`); + } + hashMapInt.clear(); + const isEmpty = hashMapInt.isEmpty(); + const mapSize = hashMapInt.size; + if (isEmpty) { + logs.push(`hashMapInt通过clear方法清空,hashMapInt.size = ${mapSize} `); + } + hilog.info(0x0000, 'testTag', 'ConcurrentHashMap basicScene ' + logs); + } + + concurrentInQueue() { + let hashMapInt: ConcurrentHashMap = new ConcurrentHashMap(); + let logs: Array = new Array(); + let p1 = launch + setData1(hashMapInt, logs); + let p2 = launch + setData2(hashMapInt, logs); + await p1; + await p2; + const mapSize = hashMapInt.size; + if (MAP_CAPACITY === mapSize) { + logs.push('测试成功'); + } + hilog.info(0x0000, 'testTag', 'ConcurrentHashMap concurrentInQueue ' + logs); + } + + concurrentOutQueue() { + let hashMapInt: ConcurrentHashMap = new ConcurrentHashMap(); + let logs: Array = new Array(); + for (let i = 0; i < MAP_CAPACITY; i++) { + hashMapInt.set(i, `v${i}`); + } + let p1 = launch + deleteData1(hashMapInt, logs); + let p2 = launch + deleteData2(hashMapInt, logs); + await p1; + await p2; + const isEmpty = hashMapInt.isEmpty(); + if (isEmpty) { + logs.push('测试成功'); + } + hilog.info(0x0000, 'testTag', 'ConcurrentHashMap concurrentOutQueue ' + logs); + } + + hashMapBenchmark() { + const startTime = new Date().getTime(); + let hashMapInt: ConcurrentHashMap = new ConcurrentHashMap(); + let logs: ArrayBlockingQueue = new ArrayBlockingQueue(2801); + let p1 = launch + setLargeData(hashMapInt, logs, 200); + let p2 = launch + setLargeData(hashMapInt, logs, 400); + let p3 = launch + setLargeData(hashMapInt, logs, 800); + let p4 = launch + getLargeData(hashMapInt, logs, 200); + let p5 = launch + getLargeData(hashMapInt, logs, 400); + let p6 = launch + getLargeData(hashMapInt, logs, 800); + await p1; + await p2; + await p3; + await p4; + await p5; + await p6; + + const duration = new Date().getTime() - startTime; + if (hashMapInt.size === 1400) { + logs.push(`测试成功 吞吐量: 6 tasks in ${duration}ms`); + } + hilog.info(0x0000, 'testTag', 'ConcurrentHashMap hashMapBenchmark ' + logs.getEnd()); + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentSetTest.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentSetTest.ets new file mode 100644 index 0000000000000000000000000000000000000000..4f067084d2ef3191fafa8ac691423bf84189a488 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentSetTest.ets @@ -0,0 +1,126 @@ +import hilog from '@ohos.hilog'; + +function addData1(setInt: ConcurrentSet, logs: Array) { + for (let i = 0; i < 100; i++) { + setInt.add(i); + logs.push(`add: ${i}`); + } +} + +function addData2(setInt: ConcurrentSet, logs: Array) { + for (let i = 100; i < 200; i++) { + setInt.add(i); + logs.push(`add: ${i}`); + } +} + +function deleteData1(setInt: ConcurrentSet, logs: Array) { + for (let i = 0; i < 100; i++) { + let isDelete = setInt.delete(i); + if (isDelete) { + logs.push(`delete: ${i}`); + } else { + logs.push(`删除失败`); + } + } +} + +function deleteData2(setInt: ConcurrentSet, logs: Array) { + for (let i = 100; i < 200; i++) { + let isDelete = setInt.delete(i); + if (isDelete) { + logs.push(`delete: ${i}`); + } else { + logs.push(`删除失败`); + } + } +} + +export class ConcurrentSetTest { + basicScene() { + let setInt: ConcurrentSet = new ConcurrentSet(); + let logs: Array = new Array(); + for (let i = 0; i < 10; i++) { + setInt.add(i); + logs.push(`add: ${i}`); + } + logs.push(`当前集合: ${setInt}`); + let zero = 1; + let isHas = setInt.has(zero); + if (isHas) { + logs.push(`集合存在数据${zero}`); + } + const size = setInt.size; + logs.push(`size: ${size}`); + let isDelete = setInt.delete(2); + logs.push(`delete: 2 删除是否成功: ${isDelete}`); + logs.push(`当前集合: ${setInt}`); + let keys = setInt.keys(); + let keyStr = ''; + while (true) { + const v = keys.next() + if (v.done) { + break; + } + let value = v.value; + keyStr += value + ' '; + } + ; + logs.push(`keys: ${keyStr}`); + let values = setInt.values(); + let valuesStr = ''; + while (true) { + const v = values.next() + if (v.done) { + break; + } + let value = v.value; + valuesStr += value + ' '; + } + ; + logs.push(`values: ${valuesStr}`); + logs.push(`当前集合: ${setInt}`); + setInt.clear(); + const setSize = setInt.size; + logs.push(`清空集合后size: ${setSize}`); + hilog.info(0x0000, 'testTag', 'ConcurrentSet basicScene ' + logs); + } + + concurrentInSet() { + const setCapacity = 200; + let set: Set = new Set(); + let setInt: ConcurrentSet = new ConcurrentSet(set); + let logs: Array = new Array(); + let p1 = launch + addData1(setInt, logs); + let p2 = launch + addData2(setInt, logs); + await p1; + await p2; + const setSize = setInt.size; + if (setCapacity === setSize) { + logs.push('测试成功'); + } + hilog.info(0x0000, 'testTag', 'ConcurrentSet concurrentInSet ' + logs); + } + + concurrentOutSet() { + const setCapacity = 200; + let set: Set = new Set(); + for (let i: number = 0; i < setCapacity; i++) { + set.add(i); + } + let setInt: ConcurrentSet = new ConcurrentSet(set); + let logs: Array = new Array(); + let p1 = launch + deleteData1(setInt, logs); + let p2 = launch + deleteData2(setInt, logs); + await p1; + await p2; + if (setInt.size === 0) { + logs.push('测试成功'); + } + hilog.info(0x0000, 'testTag', 'ConcurrentSet concurrentOutSet ' + logs); + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/LinkedBlockingQueueTest.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/LinkedBlockingQueueTest.ets new file mode 100644 index 0000000000000000000000000000000000000000..67588c246b3dd6d4afd598bd68dc8ec08891c17a --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/LinkedBlockingQueueTest.ets @@ -0,0 +1,154 @@ +import hilog from '@ohos.hilog'; + +function pushData1(queue: LinkedBlockingQueue, logs: Array) { + for (let i = 0; i < 100; i++) { + queue.push(i); + logs.push(`push: ${i}`); + } +} + +function pushData2(queue: LinkedBlockingQueue, logs: Array) { + for (let i = 100; i < 200; i++) { + queue.push(i); + logs.push(`push: ${i}`); + } +} + +function popData1(queue: LinkedBlockingQueue, logs: Array) { + for (let i = 0; i < 100; i++) { + queue.pop(); + logs.push(`pop: ${i}`); + } +} + +function popData2(queue: LinkedBlockingQueue, logs: Array) { + for (let i = 100; i < 200; i++) { + queue.pop(); + logs.push(`pop: ${i}`); + } +} + +function fullBlockingPush(queue: LinkedBlockingQueue, logs: Array) { + for (let i = 0; i < 3; i++) { + logs.push(`push: ${i}`); + queue.push(i); + logs.push(`添加${i}成功 `); + } +} + +function fullBlockingPop(queue: LinkedBlockingQueue, logs: Array) { + for (let i = 0; i < 6; i++) { + logs.push(`pop: ${i}`); + queue.pop(); + logs.push(`删除${i}成功 `); + } +} + +function nullBlockingPop(queue: LinkedBlockingQueue, logs: Array) { + for (let i = 0; i < 3; i++) { + logs.push(`pop: ${i}`); + queue.pop(); + logs.push(`删除${i}成功 `); + } +} + +function nullBlockingPush(queue: LinkedBlockingQueue, logs: Array) { + for (let i = 0; i < 3; i++) { + logs.push(`push: ${i}`); + queue.push(i); + logs.push(`添加${i}成功 `); + } +} + +export class LinkedBlockingQueueTest { + basicScene() { + const queue = 10; + let queueInt: LinkedBlockingQueue = new LinkedBlockingQueue(queue); + let logs: Array = new Array(); + const capacity = queueInt.capacity; + logs.push(`队列capacity: ${capacity}`); + for (let i = 0; i < queue; i++) { + queueInt.push(i); + logs.push(`push: ${i}`); + } + const popData = queueInt.pop(); + logs.push(`pop: ${popData}`); + const addData = 10; + queueInt.add(addData); + logs.push(`add: ${addData}`); + const pollData = queueInt.poll(); + logs.push(`poll: ${pollData}`); + const size = queueInt.size; + logs.push(`size: ${size}`); + const remainingCapacity = queueInt.remainingCapacity(); + logs.push(`remainingCapacity: ${remainingCapacity}`); + + hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest basicScene ' + logs); + } + + concurrentInQueue() { + const queueCapacity = 200; + let queueInt: LinkedBlockingQueue = new LinkedBlockingQueue(queueCapacity); + let logs: Array = new Array(); + let p1 = launch + pushData1(queueInt, logs); + let p2 = launch + pushData2(queueInt, logs); + await p1; + await p2; + const queueSize = queueInt.size; + if (queueCapacity === queueSize) { + logs.push('测试成功'); + } + hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest concurrentInQueue ' + logs); + } + + concurrentOutQueue() { + const queueCapacity = 200; + let queueInt: LinkedBlockingQueue = new LinkedBlockingQueue(queueCapacity); + let logs: Array = new Array(); + for (let i = 0; i < queueCapacity; i++) { + queueInt.push(i); + } + let p1 = launch + popData1(queueInt, logs); + let p2 = launch + popData2(queueInt, logs); + await p1; + await p2; + const isEmpty = queueInt.isEmpty(); + if (isEmpty) { + logs.push('测试成功'); + } + hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest concurrentOutQueue ' + logs); + } + + fullBlocking() { + const queueCapacity = 3; + let queueInt: LinkedBlockingQueue = new LinkedBlockingQueue(queueCapacity); + let logs: Array = new Array(); + for (let i = 0; i < queueCapacity; i++) { + queueInt.push(i); + } + let p1 = launch + fullBlockingPush(queueInt, logs); + let p2 = launch + fullBlockingPop(queueInt, logs); + await p1; + await p2; + hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest fullBlocking ' + logs); + } + + nullBlocking() { + const queueCapacity = 3; + let queueInt: LinkedBlockingQueue = new LinkedBlockingQueue(queueCapacity); + let logs: Array = new Array(); + let p1 = launch + nullBlockingPop(queueInt, logs); + let p2 = launch + nullBlockingPush(queueInt, logs); + await p1; + await p2; + hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest nullBlocking ' + logs); + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/module.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..de78b5cae3b2ba99788048311e5d0cd34d075039 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/module.json5 @@ -0,0 +1,38 @@ +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ] + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/color.json b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/float.json b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/string.json b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..7f6fcd10cfc441a3a6922a518a957647fd6c89dd --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "线程安全容器" + } + ] +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/background.png b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/background.png differ diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/foreground.png b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/foreground.png differ diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/layered_image.json b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/startIcon.png b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/startIcon.png differ diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/profile/backup_config.json b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/profile/main_pages.json b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/dark/element/color.json b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/hvigor/hvigor-config.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..85e8d4b2175fc4747650344f025e7d145bc3d361 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/hvigor/hvigor-config.json5 @@ -0,0 +1,22 @@ +{ + "modelVersion": "5.1.0", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/hvigorfile.ts b/code/ArkTS1.2/ThreadSafeContainersSample/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..f3cb9f1a87a81687554a76283af8df27d8bda775 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/hvigorfile.ts @@ -0,0 +1,6 @@ +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/oh-package.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7f512c9632708e85a7ea34e739c01b0b8f1ad92e --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/oh-package.json5 @@ -0,0 +1,8 @@ +{ + "modelVersion": "5.1.0", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + } +}