• Home
Name Date Size #Lines LOC

..--

AppScope/22-Oct-2025-3532

entry/22-Oct-2025-3,0522,797

hvigor/22-Oct-2025-2221

screenshots/device/22-Oct-2025-

.gitignoreD22-Oct-2025119 1111

README.mdD22-Oct-20252.4 KiB5125

README_zh.mdD22-Oct-20256 KiB12280

build-profile.json5D22-Oct-20251.1 KiB4342

hvigorfile.tsD22-Oct-2025159 21

hvigorwD22-Oct-20252.1 KiB6228

hvigorw.batD22-Oct-20252 KiB7356

oh-package.json5D22-Oct-2025848 2826

ohosTest.mdD22-Oct-20251.3 KiB1010

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| ![](screenshots/device/index.jpg) | ![](screenshots/device/copyFile.jpg) | ![](screenshots/device/workerTab.jpg) | ![](screenshots/device/taskpoolTab.jpg) |
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.ets                       // 公用方法,如bufferToString
56|   |---Logger.ts                       // 日志
57|---component
58|   |---SendableTest.ets                 // 传递SendableClass页签
59|   |---TaskPoolTab.ets                 // taskpool页签
60|   |---test.ets                        // Sendable Class
61|   |---WorkerTab.ets                   // worker页签
62|---entryability
63|---fileFs
64|   |---MyWorker.ets                    // 批量拷贝文件
65|---pages
66|   |---CopyFile.ets                    // 拷贝文件界面,可选择把文件进行拷贝并显示触发事件后日志
67|   |---Index.ets                       // 首页
68|   |---StrSort.ets                     // worker和taskpool页签都在这里调用
69|---workers
70|   |---Worker.ts                       // worker线程
71|   |---WorkerCopy.ts                   // 拷贝文件的worker线程
72```
73
74### 具体实现
75
76* worker页签的实现在字符串排序页面调用,源码参考[StrSort.ets](entry/src/main/ets/pages/StrSort.ets)
77  * 字符串排序:通过调用executeWorkerFunc()创建一个worker线程,把待排序字符串发送给worker线程,等worker线程排序完成后再把结果返回。
78  * 清除:把字符串输入框和结果都清除。
79
80* taskpool页签的实现在字符串排序页面调用,源码参考[StrSort.ets](entry/src/main/ets/pages/StrSort.ets)
81  * 立即执行:如没有添加至任务组操作,通过调用executeImmediately()创建一个task任务,这个任务是立即执行字符串排序;如先进行了添加至任务组操作,则通过调用executeImmediately()将任务组执行完毕。
82  * 超时3s执行:通过调用executeDelay()创建一个task任务,这个任务是延迟3s后执行字符串排序。
83  * 函数任务:调用executeFunc()接口,不创建task任务,直接调用taskpool.execute()执行字符串排序。
84  * 取消任务:调用cancelTask()接口,取消最后一个未执行的task任务。
85  * 清除:把字符串输入框和结果都清除。
86  * 添加至任务组:调用addTask()接口,将当前任务添加至任务组。
87
88* 内存共享的实现在taskpool页面调用,源码参考[TaskPoolTab.ets](entry/src/main/ets/component/TaskPoolTab.ets)
89
90* 批量拷贝文件的功能封装在MyWorker,源码参考:[MyWorker.ets](entry/src/main/ets/fileFs/MyWorker.ets)
91
92  * 拷贝文件:在[CopyFile.ets](entry/src/main/ets/pages/CopyFile.ets)
93
94    中调用MyWorker.WorkToCopyFiles(),在WorkToCopyFiles方法中向WorkerCopy线程发消息,并在WorkerCopy线程中批量拷贝,拷贝完成后将结果返回。
95
96### 相关权限
97
98不涉及。
99
100### 依赖
101
102不涉及。
103
104### 约束与限制
105
1061.本示例仅支持标准系统上运行, 支持设备:RK3568;。
107
1082.本示例为Stage模型,支持API12版本SDK,版本号:5.0.0.21,镜像版本号:OpenHarmony 5.0.0.21。
109
1103.本示例需要使用DevEco Studio NEXT Developer Preview1 (Build Version: 4.1.3.501, built on February 5, 2024)及以上版本才可编译运行。
111
112### 下载
113
114如需单独下载本工程,执行如下命令:
115
116````
117git init
118git config core.sparsecheckout true
119echo code/LanguageBaseClassLibrary/ConcurrentModule/ > .git/info/sparse-checkout
120git remote add origin https://gitee.com/openharmony/applications_app_samples.git
121git pull origin master
122````