16e7c69efSopenharmony_ci# Starting a Multi-Threaded Task
26e7c69efSopenharmony_ci
36e7c69efSopenharmony_ci
46e7c69efSopenharmony_ci### Introduction
56e7c69efSopenharmony_ci
66e7c69efSopenharmony_ciThis sample show how to start worker threads and taskpool threads.
76e7c69efSopenharmony_ci
86e7c69efSopenharmony_ci### Related Concepts
96e7c69efSopenharmony_ci
106e7c69efSopenharmony_ciworker: 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.
116e7c69efSopenharmony_ci
126e7c69efSopenharmony_citaskpool: A taskpool can be used to create background tasks and manage their execution, cancellation, etc.
136e7c69efSopenharmony_ci
146e7c69efSopenharmony_ci### Required Permissions
156e7c69efSopenharmony_ci
166e7c69efSopenharmony_ciNo permissions are required.
176e7c69efSopenharmony_ci
186e7c69efSopenharmony_ci### Usage
196e7c69efSopenharmony_ci
206e7c69efSopenharmony_ciworker:
216e7c69efSopenharmony_ci
226e7c69efSopenharmony_ci1. Select the Worker tab and enter the string to be sorted, separated by commas.
236e7c69efSopenharmony_ci
246e7c69efSopenharmony_ci2. 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.
256e7c69efSopenharmony_ci
266e7c69efSopenharmony_ci3. Click the **Clear** button to clear the string.
276e7c69efSopenharmony_ci
286e7c69efSopenharmony_citaskpool:
296e7c69efSopenharmony_ci
306e7c69efSopenharmony_ci1. Select the Taskpool tab and enter the string to be sorted, separated by commas.
316e7c69efSopenharmony_ci
326e7c69efSopenharmony_ci2.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.
336e7c69efSopenharmony_ci
346e7c69efSopenharmony_ci3. Click the **Execute Immediately** button to execute the task immediately, and the sorted string will be displayed after the task completes.
356e7c69efSopenharmony_ci
366e7c69efSopenharmony_ci4. 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.
376e7c69efSopenharmony_ci
386e7c69efSopenharmony_ci5. 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.
396e7c69efSopenharmony_ci
406e7c69efSopenharmony_ci6. 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.
416e7c69efSopenharmony_ci
426e7c69efSopenharmony_ci7. Click the **Clear** button to clear the string.
436e7c69efSopenharmony_ci
446e7c69efSopenharmony_ci8. Click the **Pass SendableClass** button to transfer SendableClass through memory sharing.
456e7c69efSopenharmony_ci
466e7c69efSopenharmony_ci### Constraints and Limitations
476e7c69efSopenharmony_ci
486e7c69efSopenharmony_ci1. This example only supports running on a standard system.
496e7c69efSopenharmony_ci
506e7c69efSopenharmony_ci2. This example requires DevEco Studio 4.0 Release (Build Version: 4.0.0.600, built on October 17, 2023) to compile and run.
51