diff --git a/thread/JsWorker/README_zh.md b/thread/JsWorker/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..0de2cfc76b0f0c236ca1aae8ba330133aa5de687 --- /dev/null +++ b/thread/JsWorker/README_zh.md @@ -0,0 +1,19 @@ +# JsWorker + + + +### 简介 + +本示例展示了在Js中如何启动一个worker线程,并实现worker线程和宿主线程的通信。 + +### 使用说明 + +1、在首页中显示了排序前的四个字符串,点击**字符串排序**按钮,会将排序前的字符串发送给worker线程,在worker线程实现字符串排序,然后将排序后的字符串发送给主线程,主线程中显示排序后的字符串。 + +2、点击**清除**按钮,清除排序后的字符串。 + + + +### 约束与限制 + +本示例仅支持标准系统上运行。 diff --git a/thread/JsWorker/build.gradle b/thread/JsWorker/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..8469dbed391d35ea3560bcb82b1258e27bdeb09a --- /dev/null +++ b/thread/JsWorker/build.gradle @@ -0,0 +1,34 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +// For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 7 + supportSystem "standard" +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } + dependencies { + classpath 'com.huawei.ohos:hap:3.0.3.4' + classpath 'com.huawei.ohos:decctest:1.2.6.0' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } +} diff --git a/thread/JsWorker/entry/build.gradle b/thread/JsWorker/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..895f5ab55babc59d9158eda9724930a93d1c9c72 --- /dev/null +++ b/thread/JsWorker/entry/build.gradle @@ -0,0 +1,21 @@ +apply plugin: 'com.huawei.ohos.hap' +// For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 7 + defaultConfig { + compatibleSdkVersion 7 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) + testImplementation 'junit:junit:4.13.1' +} diff --git a/thread/JsWorker/entry/src/main/config.json b/thread/JsWorker/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..d4c96c5d84e3f037c94ecaea025a92a51ae75b51 --- /dev/null +++ b/thread/JsWorker/entry/src/main/config.json @@ -0,0 +1,60 @@ +{ + "app": { + "bundleName": "ohos.samples.jsworker", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.jsworker", + "name": ".MyApplication", + "mainAbility": ".MainAbility", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "visible": true, + "srcPath": "MainAbility", + "name": ".MainAbility", + "srcLanguage": "js", + "icon": "$media:icon", + "description": "$string:description_mainability", + "label": "$string:entry_MainAbility", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "pages": [ + "pages/index/index" + ], + "name": ".MainAbility", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } +} \ No newline at end of file diff --git a/thread/JsWorker/entry/src/main/js/MainAbility/app.js b/thread/JsWorker/entry/src/main/js/MainAbility/app.js new file mode 100644 index 0000000000000000000000000000000000000000..58d12b7b501fbf46db906a86187aaf18dfb1f677 --- /dev/null +++ b/thread/JsWorker/entry/src/main/js/MainAbility/app.js @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 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. + */ + +export default { + onCreate() { + console.info("Application onCreate"); + }, + onDestroy() { + console.info("Application onDestroy"); + } +}; diff --git a/thread/JsWorker/entry/src/main/js/MainAbility/i18n/en-US.json b/thread/JsWorker/entry/src/main/js/MainAbility/i18n/en-US.json new file mode 100644 index 0000000000000000000000000000000000000000..2bc254fa3d24cd2526a5e8552a091ab1270f506c --- /dev/null +++ b/thread/JsWorker/entry/src/main/js/MainAbility/i18n/en-US.json @@ -0,0 +1,12 @@ +{ + "strings": { + "title": "JsWorker", + "tip_before": "Before sort:", + "tip_after": "After sort:", + "send_string": "Sort string array", + "clear": "Clear", + "menus": ["string adc","string adcf","string afc","string aaccdd"] + }, + "Files": { + } +} \ No newline at end of file diff --git a/thread/JsWorker/entry/src/main/js/MainAbility/i18n/zh-CN.json b/thread/JsWorker/entry/src/main/js/MainAbility/i18n/zh-CN.json new file mode 100644 index 0000000000000000000000000000000000000000..7353845156c22057f94b0de34d937528a7a4773b --- /dev/null +++ b/thread/JsWorker/entry/src/main/js/MainAbility/i18n/zh-CN.json @@ -0,0 +1,11 @@ +{ + "strings": { + "title": "JsWorker", + "tip_before": "排序前:", + "tip_after": "排序后:", + "send_string": "字符串排序", + "clear": "清除" + }, + "Files": { + } +} \ No newline at end of file diff --git a/thread/JsWorker/entry/src/main/js/MainAbility/pages/index/index.css b/thread/JsWorker/entry/src/main/js/MainAbility/pages/index/index.css new file mode 100644 index 0000000000000000000000000000000000000000..50d91acf9a9c06a7420e390d9385ab2d75afd847 --- /dev/null +++ b/thread/JsWorker/entry/src/main/js/MainAbility/pages/index/index.css @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 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. + */ + +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 42px; + color: #FFFFFF; + opacity: 0.9; + width: 100%; + padding: 10px; + background-color: dodgerblue; + height: 90px; +} + +.content { + width: 100%; + padding: 15px; + align-content: flex-start; + flex-direction: column; +} + +.btn { + width: 90%; + height: 90px; + font-size: 40px; + margin-top: 30px; + align-self: center; +} + +.tip { + font-size: 40px; + color: black; + margin-top: 20px; +} + +.text { + font-size: 40px; + color: black; + opacity: 0.9; + width: 100%; + height: 250px; + padding: 10px; + background-color: #F0F0F0; + margin-top: 10px; +} diff --git a/thread/JsWorker/entry/src/main/js/MainAbility/pages/index/index.hml b/thread/JsWorker/entry/src/main/js/MainAbility/pages/index/index.hml new file mode 100644 index 0000000000000000000000000000000000000000..9cf0c7dd52286d4a67f9eba8d440308e17310726 --- /dev/null +++ b/thread/JsWorker/entry/src/main/js/MainAbility/pages/index/index.hml @@ -0,0 +1,28 @@ + + +
+ {{ $t('strings.title') }} + + + {{ $t('strings.tip_before') }} + {{ before }} + {{ $t('strings.tip_after') }} + {{ after }} + + + + +
diff --git a/thread/JsWorker/entry/src/main/js/MainAbility/pages/index/index.js b/thread/JsWorker/entry/src/main/js/MainAbility/pages/index/index.js new file mode 100644 index 0000000000000000000000000000000000000000..59bd4deb58312322102a37625b40e1a543cddfe4 --- /dev/null +++ b/thread/JsWorker/entry/src/main/js/MainAbility/pages/index/index.js @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2021 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 worker from '@ohos.worker'; + +let afterStr = null + +export default { + data: { + before: "", + after: "", + worker: new worker.Worker("workers/worker.js", { + name: "worker_test" + }) + }, + onInit() { + this.before = this.$t('strings.menus') + this.worker.onerror = function (data) { + console.info('[worker.index] on error:' + data) + } + this.worker.onmessageerror = function (data) { + console.info('[worker.index] on messageerror:' + data) + } + this.worker.onexit = function (data) { + console.info('[worker.index] on exit:' + data) + } + this.worker.onmessage = function (e) { + var data = e.data + if (data.type == 'normal') { + console.info('[worker.index] normal:' + data.data) + this.after = data.data + } + } + }, + sendString: function () { + var obj = { + type: "normal", data: this.before.toString() + } + console.info('[worker.index] sendString:' + obj.data) + this.worker.postMessage(obj) + }, + clear: function () { + this.after = "" + }, + terminate() { + this.worker.terminate() + } +} + + + diff --git a/thread/JsWorker/entry/src/main/js/MainAbility/workers/worker.js b/thread/JsWorker/entry/src/main/js/MainAbility/workers/worker.js new file mode 100644 index 0000000000000000000000000000000000000000..ea67e4dec48169d8ef86fc20ff2f1a9982020fee --- /dev/null +++ b/thread/JsWorker/entry/src/main/js/MainAbility/workers/worker.js @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 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 workers from '@ohos.worker'; + +const parentPort = workers.parentPort + +parentPort.onmessageerror = function (e) { + var data = e.data + console.info('[worker.worker] onmessageerror:' + JSON.stringify(data)) +} + +parentPort.onmessage = function (e) { + var data = e.data + console.info('[worker.worker] onmessage:' + JSON.stringify(data)) + if (data.type == 'normal') { + var obj = data.data + console.info('[worker.worker] postMessage obj:' + obj) + var after = obj.split(",").sort() + console.info('[worker.worker] postMessage after:' + after) + data.data = after + parentPort.postMessage(data) + } +} \ No newline at end of file diff --git a/thread/JsWorker/entry/src/main/resources/base/element/string.json b/thread/JsWorker/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..caccf1984ed4ce3cff11c103873deac5841b0475 --- /dev/null +++ b/thread/JsWorker/entry/src/main/resources/base/element/string.json @@ -0,0 +1,12 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "JsWorker" + }, + { + "name": "description_mainability", + "value": "JsWorker Ability" + } + ] +} \ No newline at end of file diff --git a/thread/JsWorker/entry/src/main/resources/base/media/icon.png b/thread/JsWorker/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/thread/JsWorker/entry/src/main/resources/base/media/icon.png differ diff --git a/thread/JsWorker/screenshots/devices/main.png b/thread/JsWorker/screenshots/devices/main.png new file mode 100644 index 0000000000000000000000000000000000000000..6bdfe7ec2e0c06ba68f5b75b2479ecf20286c14b Binary files /dev/null and b/thread/JsWorker/screenshots/devices/main.png differ diff --git a/thread/JsWorker/settings.gradle b/thread/JsWorker/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..4773db73233a570c2d0c01a22e75321acfbf7a07 --- /dev/null +++ b/thread/JsWorker/settings.gradle @@ -0,0 +1 @@ +include ':entry'