During modern software development, multi-thread or multi-process concurrent handling has become a norm. In a multithreaded environment, tasks executed by different threads may be of different importance and urgency. In high-load scenarios, system resources (such as CPU time) become precious. If key threads (such as UI rendering threads) are frequently preempted by non-key threads and cannot obtain sufficient time for continuous execution and resources, problems such as frame freezing and delay may occur. As a result, user experience is severely affected.
When the load is heavy, to ensure that key tasks can obtain sufficient resources, the entire system allocates corresponding time slices to tasks based on task importance. A task of higher importance can be allocated with more time slices. Therefore, you can identify a key thread in an app and provide a relatively high QoS level for the key thread in terms of the task urgency to prevent the key thread from being interrupted by another thread, thereby ensuring smooth running of the app and better user experience.
Quality of Service (QoS) describes the basic concepts and principles of QoS, use scenarios of each QoS level, load characteristics, and usage of related APIs.
For OS, the QoS level is a technology used to distinguish different thread priorities and quality of service. Generally, the system automatically identifies the main thread and configures a QoS level higher than that open to app developers when the foreground is focused to ensure that the main thread is executed first.
Similar to the thread priority of taskpool.Priority on the ArkTS side, the priority level provided by QoS is also mapped to the kernel priority. However, QoS provides more levels and stronger adaptive scheduling policies, which belong to two different sets of logic.
The QoS of function flow runtime (FFRT) provides five priorities ranging from ffrt_qos_inherit (-1) to ffrt_qos_user_initiated (3), which has the same underlying logic as the current QoS API. The difference is that the QoS API used in current development is directly open to the app thread, while the FFRT QoS is task-oriented priority configuration. For details about the comparison between the thread programming model and task programming model, see Introduction to Function Flow Runtime Kit.
The following figure shows the time taken by two key threads configured with different QoS levels to complete the same computing task in the case of heavy load. According to the running result, the thread configured with a higher priority takes less time to complete the computing task.

The procedure is as follows:
1. Implement the task to be completed by the load thread.
// Set the load task.
void AddLoads(int n) {
if (!n) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "QoS", "invalid input.");
return;
}
// Set QoS level.
int ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_BACKGROUND);
if (ret) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "QoS", "set load thread QoS level failed.");
return;
}
// Bind CPU.
cpu_set_t mask;
CPU_SET(*g_affinity, &mask);
if (sched_setaffinity(0, sizeof(mask), &mask) != 0) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "QoS", "bind load thread failed");
return;
}
// Perform load calculation.
for (int i = 0; i < BOUND; i++) {
for (int j = 0; j < BOUND; j++) {
int x = (i + j) - n;
printf("%d", x);
}
}
// Reset load flag.
g_addLoad = false;
} 2. Implement the Fibonacci sequence calculation tasks completed by the calculation threads (key threads) of high and low QoS levels. You can call OH_QoS_SetThreadQoS to set the QoS level of the calling thread, and call DoFib().
// Perform Fibonacci sequence calculations.
long long DoFib(double n) {
if (n == ONE) {
return ONE;
}
if (n == TWO) {
return TWO;
}
return DoFib(n - ONE) + DoFib(n - TWO);
}
void SetQoS(QoS_Level level) {
// Set QoS level.
int ret = OH_QoS_SetThreadQoS(level);
if (!ret) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "QoS", "set qos level success.");
// Query QoS level.
QoS_Level queryLevel = QOS_DEFAULT;
ret = OH_QoS_GetThreadQoS(&queryLevel);
if (!ret) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "QoS", "the qos level of current thread : %{public}d",
queryLevel);
} else {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "QoS", "get qos level failed.");
return;
}
} else {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "QoS", "get level qos failed!");
return;
}
// Bind CPU.
cpu_set_t mask;
CPU_SET(*g_affinity, &mask);
if (sched_setaffinity(0, sizeof(mask), &mask) != 0) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "QoS", "bind qos thread failed");
return;
}
auto startTime = std::chrono::system_clock::now();
// Execute computational tasks.
long long res = DoFib(DEPTH);
auto endTime = std::chrono::system_clock::now();
g_durationTime = std::chrono::duration<double, std::milli>(endTime - startTime).count();
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "QoS", "calculate res is: %{public}llu", res);
// Reset QoS level.
ret = OH_QoS_ResetThreadQoS();
if (!ret) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "QoS", "reset qos level success.");
} else {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "QoS", "reset qos level failed!");
return;
}
// After resetting QoS, querying QoS again will fail.
QoS_Level queryLevelTwo;
ret = OH_QoS_GetThreadQoS(&queryLevelTwo);
if (!ret) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "QoS", "the qos level after: %{public}d", queryLevelTwo);
return;
} else {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "QoS", "query qos level failed after reset.");
return;
}
} 3. Set the calculation thread (key thread) to low and high QoS levels respectively to compare the time taken by threads in different QoS levels when the Fibonacci sequence calculation is completed at the same level in the heavy load scenario.
static napi_value lowQoSCalculate(napi_env env, napi_callback_info info) {
g_durationTime = 0;
// Simulate system load.
if (!g_addLoad) {
std::vector<std::thread> loadThreads;
for (int i = 0; i < TASKS; i++) {
// Activate threads to execute load tasks.
loadThreads.emplace_back(std::thread(AddLoads, TASKS));
loadThreads[i].detach();
}
g_addLoad = true;
}
// Set level to QOS_BACKGROUND.
QoS_Level level = QoS_Level::QOS_BACKGROUND;
std::thread task(SetQoS, level);
task.join();
// Return calculation time.
napi_value res;
napi_create_double(env, g_durationTime, &res);
return res;
} Trace diagram of the calculation thread (thread ID: 39260) with a low QoS level.

As shown in the preceding figure, it takes 726.8 ms for the calculation thread to complete the calculation task.
static napi_value highQoSCalculate(napi_env env, napi_callback_info info) {
g_durationTime = 0;
// Simulate system load.
if (!g_addLoad) {
std::vector<std::thread> loadThreads;
for (int i = 0; i < TASKS; i++) {
// Activate threads to execute load tasks.
loadThreads.emplace_back(std::thread(AddLoads, TASKS));
loadThreads[i].detach();
}
g_addLoad = true;
}
// Set level to QOS_USER_INTERACTIVE.
QoS_Level level = QoS_Level::QOS_USER_INTERACTIVE;
std::thread task(SetQoS, level);
task.join();
// Return calculation time.
napi_value res;
napi_create_double(env, g_durationTime, &res);
return res;
} Trace diagram of the calculation thread (thread ID: 39204) with a high QoS level.

As shown in the preceding figure, it takes 323.9 ms for the calculation thread to complete the calculation task.
This sample is valid only in the heavy-load scenarios.
In low-load scenarios, system resources are abundant, and most threads can obtain sufficient CPU time. Therefore, thread priority settings have little impact on execution efficiency.
Solution | Number of Fibonacci Sequence Items | Calculation Duration |
|---|---|---|
QOS_BACKGROUND (low QoS level) | 34 | 726.8 ms |
QOS_USER_INTERACTIVE (high QoS level) | 34 | 323.9 ms |
The preceding comparison shows that threads with a higher QoS level can execute calculation tasks more quickly in heavy-load scenarios. To ensure stability and high response of an app, you can set a relatively high QoS level for key threads.
Due to limited system resources, setting a high QoS level for all internal app methods may result in resource contention. Moreover, threads with higher QoS levels gain more resources than those with lower QoS levels. Excessively increasing thread QoS levels could lead to resource starvation for other threads, potentially affecting the system stability. Therefore, you should set the QoS levels for threads based on specific application scenarios and needs.