| Name | Date | Size | #Lines | LOC | ||
|---|---|---|---|---|---|---|
| .. | - | - | ||||
| AppScope/ | 06-May-2025 | - | 35 | 32 | ||
| entry/ | 06-May-2025 | - | 3,052 | 2,797 | ||
| hvigor/ | 06-May-2025 | - | 22 | 21 | ||
| screenshots/device/ | 06-May-2025 | - | ||||
| .gitignore | D | 06-May-2025 | 119 | 11 | 11 | |
| README.md | D | 06-May-2025 | 2.4 KiB | 51 | 25 | |
| README_zh.md | D | 06-May-2025 | 5.8 KiB | 118 | 76 | |
| build-profile.json5 | D | 06-May-2025 | 1.1 KiB | 43 | 42 | |
| hvigorfile.ts | D | 06-May-2025 | 159 | 2 | 1 | |
| hvigorw | D | 06-May-2025 | 2.1 KiB | 62 | 28 | |
| hvigorw.bat | D | 06-May-2025 | 2 KiB | 73 | 56 | |
| oh-package.json5 | D | 06-May-2025 | 875 | 28 | 26 | |
| ohosTest.md | D | 06-May-2025 | 1.3 KiB | 10 | 10 |
README.md
1# Starting a Multi-Threaded Task 2 3 4### Introduction 5 6This sample show how to start worker threads and taskpool threads. 7 8### Related Concepts 9 10worker: A worker is an independent thread that runs in parallel with the main thread. The thread that creates the worker is called the hosting thread, while the worker thread itself is called the worker thread. The URL file passed to the worker when it is created is executed in the worker thread, which can handle time-consuming operations but cannot directly manipulate the UI. 11 12taskpool: A taskpool can be used to create background tasks and manage their execution, cancellation, etc. 13 14### Required Permissions 15 16No permissions are required. 17 18### Usage 19 20worker: 21 221. Select the Worker tab and enter the string to be sorted, separated by commas. 23 242. Click the **Sort String** button, which will send the unsorted string to the worker thread for sorting. The sorted string will then be sent back to the main thread for display. 25 263. Click the **Clear** button to clear the string. 27 28taskpool: 29 301. Select the Taskpool tab and enter the string to be sorted, separated by commas. 31 322.Click the * * String Sort * * button will send the pre sorted string to the worker thread, where string sorting is implemented. Then, the sorted string will be sent to the main thread, which will display the sorted string. 33 343. Click the **Execute Immediately** button to execute the task immediately, and the sorted string will be displayed after the task completes. 35 364. Click the **Execute After 3s** button to delay the task execution by 3 seconds, and the sorted string will be displayed after the task completes. 37 385. Click the **Function Task** button to execute the operation directly, and the sorted string will be displayed after the task completes. Note that the task created using Function Task cannot be cancelled. 39 406. Click the **Cancel Task** button to cancel the last unexecuted task. Note that cancellation can only be successful if the number of tasks is greater than the maximum number of threads and the task has started executing. 41 427. Click the **Clear** button to clear the string. 43 448. Click the **Pass SendableClass** button to transfer SendableClass through memory sharing. 45 46### Constraints and Limitations 47 481. This example only supports running on a standard system. 49 502. This example requires DevEco Studio 4.0 Release (Build Version: 4.0.0.600, built on October 17, 2023) to compile and run. 51
README_zh.md
1# 多线程任务 2 3### 介绍 4 5本示例通过[@ohos.taskpool](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkts/js-apis-taskpool.md) 6和[@ohos.worker](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkts/js-apis-worker.md) 接口,展示了如何启动worker线程和taskpool线程。 7 8### 效果预览 9 10| 首页 | 拷贝文件 | worker页面 | 任务池页面 | 11| --------------------------------- | ------------------------------------ | ------------------------------------- | ------------------------------------ | 12|  |  |  |  | 13 14使用说明 15 161. 在主界面,可以点击**字符串排序**和**拷贝文件**按钮进入对应的界面; 17 182. 点击**字符串排序**按钮进入多线程界面: 19 20 worker: 21 22 1. 选择**Worker**页签,输入待排序的字符串,并以逗号分割。 23 24 2. 点击**字符串排序**按钮,会将排序前的字符串发送给worker线程,在worker线程实现字符串排序,然后将排序后的字符串发送给主线程,主线程中显示排序后的字符串。 25 26 3. 点击**清除**按钮,清除字符串。 27 28 taskpool: 29 30 1. 选择**TaskPool**页签,输入待排序的字符串,并以逗号分割。 31 32 2. 选择**添加至任务组**按钮,将当前字符串的排序任务添加至任务组。 33 34 3. 点击**立即执行**按钮,如之前有添加至任务组操作则任务组执行完成后将排序后的字符串显示出来;如没有上述操作则执行当前任务完成,将排序后的字符串显示出来。 35 36 4. 点击**超时3s执行**按钮,任务延迟3s后执行,执行完成后将排序后的字符串显示出来。 37 38 5. 点击**函数任务**按钮,直接调用执行操作,执行完成后将排序后的字符串显示出来。需要注意的是,通过**函数任务**创建的task任务不支持取消。 39 40 6. 点击**取消任务**按钮,会取消最后一个未执行的task任务。需要注意的是,只有当任务数大于最大线程数且任务未开始执行时才可以取消成功。 41 42 7. 点击**清除**按钮,清除字符串。 43 44 8. 点击**传递SendableClass**按钮,内存共享方式传递SendableClass。 45 463. 点击**拷贝文件**按钮进入文件拷贝界面: 47 48 选择需要拷贝的文件,然后点击**拷贝文件**按钮,文件拷贝成功,触发事件日志显示沙箱下文件个数以及显示部分拷贝成功的文件名。 49 50### 工程目录 51 52``` 53entry/src/main/ets/ 54|---common 55| |---Common.ts // 公用方法,如bufferToString 56| |---Logger.ts // 日志 57|---component 58| |---TaskPoolTab.ets // taskpool页签 59| |---WorkerTab.ets // worker页签 60|---entryability 61|---pages 62| |---CopyFile.ets // 拷贝文件界面,可选择把文件进行拷贝并显示触发事件后日志 63| |---Index.ets // 首页 64| |---StrSort.ets // worker和taskpool页签都在这里调用 65|---workers 66| |---Worker.ts // worker线程 67| |---Worker03.ts // 拷贝文件的worker线程 68``` 69 70### 具体实现 71 72* worker页签的实现在字符串排序页面调用,源码参考[StrSort.ets](entry/src/main/ets/pages/StrSort.ets) 73 * 字符串排序:通过调用executeWorkerFunc()创建一个worker线程,把待排序字符串发送给worker线程,等worker线程排序完成后再把结果返回。 74 * 清除:把字符串输入框和结果都清除。 75 76* taskpool页签的实现在字符串排序页面调用,源码参考[StrSort.ets](entry/src/main/ets/pages/StrSort.ets) 77 * 立即执行:如没有添加至任务组操作,通过调用executeImmediately()创建一个task任务,这个任务是立即执行字符串排序;如先进行了添加至任务组操作,则通过调用executeImmediately()将任务组执行完毕。 78 * 超时3s执行:通过调用executeDelay()创建一个task任务,这个任务是延迟3s后执行字符串排序。 79 * 函数任务:调用executeFunc()接口,不创建task任务,直接调用taskpool.execute()执行字符串排序。 80 * 取消任务:调用cancelTask()接口,取消最后一个未执行的task任务。 81 * 清除:把字符串输入框和结果都清除。 82 * 添加至任务组:调用addTask()接口,将当前任务添加至任务组。 83 84* 内存共享的实现在taskpool页面调用,源码参考[TaskPoolTab.ets](entry/src/main/ets/component/TaskPoolTab.ets) 85 86* 批量拷贝文件的功能封装在MyWorker,源码参考:[MyWorker.ets](entry/src/main/ets/fileFs/MyWorker.ets) 87 88 * 拷贝文件:在[CopyFile.ets](entry/src/main/ets/pages/CopyFile.ets) 89 90 中调用MyWorker.WorkToCopyFiles(),在WorkToCopyFiles方法中向worker03线程发消息,并在worker03线程中批量拷贝,拷贝完成后将结果返回。 91 92### 相关权限 93 94不涉及。 95 96### 依赖 97 98不涉及。 99 100### 约束与限制 101 1021.本示例仅支持标准系统上运行, 支持设备:RK3568;。 103 1042.本示例为Stage模型,支持API12版本SDK,版本号:5.0.0.21,镜像版本号:OpenHarmony 5.0.0.21。 105 1063.本示例需要使用DevEco Studio NEXT Developer Preview1 (Build Version: 4.1.3.501, built on February 5, 2024)及以上版本才可编译运行。 107 108### 下载 109 110如需单独下载本工程,执行如下命令: 111 112```` 113git init 114git config core.sparsecheckout true 115echo code/LaunguageBaseClassLibrary/ConcurrentModule/ > .git/info/sparse-checkout 116git remote add origin https://gitee.com/openharmony/applications_app_samples.git 117git pull origin master 118````