CN116700817B - Application program running method and electronic equipment - Google Patents
Application program running method and electronic equipment Download PDFInfo
- Publication number
- CN116700817B CN116700817B CN202211403614.5A CN202211403614A CN116700817B CN 116700817 B CN116700817 B CN 116700817B CN 202211403614 A CN202211403614 A CN 202211403614A CN 116700817 B CN116700817 B CN 116700817B
- Authority
- CN
- China
- Prior art keywords
- thread
- priority
- lock
- target
- identification information
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
- 238000000034 method Methods 0.000 title claims abstract description 105
- 230000008569 process Effects 0.000 claims description 52
- 238000004590 computer program Methods 0.000 claims description 7
- 230000000903 blocking effect Effects 0.000 abstract description 18
- 238000010586 diagram Methods 0.000 description 18
- 230000006870 function Effects 0.000 description 9
- 230000003993 interaction Effects 0.000 description 6
- 238000012545 processing Methods 0.000 description 6
- 238000004904 shortening Methods 0.000 description 6
- 230000000694 effects Effects 0.000 description 4
- 230000007246 mechanism Effects 0.000 description 4
- 230000004044 response Effects 0.000 description 4
- 101001121408 Homo sapiens L-amino-acid oxidase Proteins 0.000 description 3
- 102100026388 L-amino-acid oxidase Human genes 0.000 description 3
- 101100233916 Saccharomyces cerevisiae (strain ATCC 204508 / S288c) KAR5 gene Proteins 0.000 description 3
- 230000008859 change Effects 0.000 description 3
- 238000013461 design Methods 0.000 description 3
- 230000004888 barrier function Effects 0.000 description 2
- 230000009286 beneficial effect Effects 0.000 description 2
- 238000004891 communication Methods 0.000 description 2
- 230000000295 complement effect Effects 0.000 description 2
- 238000009987 spinning Methods 0.000 description 2
- 101100012902 Saccharomyces cerevisiae (strain ATCC 204508 / S288c) FIG2 gene Proteins 0.000 description 1
- 230000005856 abnormality Effects 0.000 description 1
- 230000001133 acceleration Effects 0.000 description 1
- 230000002411 adverse Effects 0.000 description 1
- 210000000988 bone and bone Anatomy 0.000 description 1
- 238000011161 development Methods 0.000 description 1
- 230000004927 fusion Effects 0.000 description 1
- 238000010295 mobile communication Methods 0.000 description 1
- 230000003287 optical effect Effects 0.000 description 1
- 238000009877 rendering Methods 0.000 description 1
- 230000003068 static effect Effects 0.000 description 1
- 230000001360 synchronised effect Effects 0.000 description 1
- 230000000007 visual effect Effects 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/445—Program loading or initiating
- G06F9/44505—Configuring for program initiating, e.g. using registry, configuration files
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/48—Program initiating; Program switching, e.g. by interrupt
- G06F9/4806—Task transfer initiation or dispatching
- G06F9/4843—Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system
- G06F9/4881—Scheduling strategies for dispatcher, e.g. round robin, multi-level priority queues
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Debugging And Monitoring (AREA)
- Stored Programmes (AREA)
Abstract
Description
技术领域Technical Field
本申请涉及终端领域,尤其涉及一种应用程序运行的方法及电子设备。The present application relates to the field of terminals, and in particular to a method for running an application and an electronic device.
背景技术Background technique
随着智能手机的不断发展,智能手机上运行的应用程序也越来越多,例如,视频应用程序、社交应用程序等。为了减小用户使用应用程序的成本,某些应用程序还允许其他小程序在该应用中运行。With the continuous development of smart phones, more and more applications are running on smart phones, such as video applications, social applications, etc. In order to reduce the cost of using applications for users, some applications also allow other small programs to run in the application.
然而,在应用程序启动时,会出现启动慢、启动页面卡顿的问题;应用中的小程序在启动或加载扫码结果(如扫描二维码的结果)时也会出现启动慢或加载慢的问题。However, when the application is started, there will be problems with slow startup and startup page jamming; the mini-programs in the application will also have problems with slow startup or slow loading when starting or loading scan results (such as the results of scanning a QR code).
发明内容Summary of the invention
为了解决上述技术问题,本申请提供一种应用程序运行的方法及电子设备,可以提高电子设备启动的速度或加载数据的速度,避免应用程序的显示页面出现卡顿、丢帧问题。In order to solve the above technical problems, the present application provides a method for running an application and an electronic device, which can improve the speed of starting up the electronic device or loading data, and avoid freezes and frame drops in the display page of the application.
第一方面,本申请提供一种应用程序运行的方法,包括:响应于用户对应用程序的操作,应用程序中的第一线程运行;若第一线程对目标资源加锁失败且检测到第一线程属于目标线程,则目标线程将第二线程的第一优先级更新为第二优先级,第二优先级高于第一优先级,第二线程为持有目标资源的锁的线程;目标线程的优先级高于第一优先级,将第二线程的第一标识信息传输至内核;内核根据第一标识信息,在预设时长内优先为第二线程调度资源;若第二线程在预设时长内未释放锁,则按照第二优先级运行。In a first aspect, the present application provides a method for running an application, comprising: in response to a user's operation on the application, a first thread in the application runs; if the first thread fails to lock a target resource and it is detected that the first thread belongs to the target thread, the target thread updates the first priority of the second thread to a second priority, the second priority is higher than the first priority, and the second thread is the thread holding the lock of the target resource; the priority of the target thread is higher than the first priority, and the first identification information of the second thread is transmitted to the kernel; the kernel schedules resources for the second thread preferentially within a preset time length based on the first identification information; if the second thread does not release the lock within the preset time length, it runs according to the second priority.
这样,该电子设备可以是手机、手环、平板电脑、电脑等设备。当用户对应用程序进行操作时,该应用程序中的第一线程运行。该用户对应用程序的操作可以是启动应用程序操作、启动该应用程序中的小程序的操作、扫描二维码的操作等。当第一线程启动后,在第一线程对目标资源加锁失败且检测到第一线程为目标线程时,提高持锁线程(即第二线程)的优先级;并将持锁线程的第一标识信息传输至内核,由内核在预设时长内优先为该持锁线程调度资源。由于内核优先为第二线程调度资源,使得第二线程可以优先获取到CPU的时间片,也即第二线程可以在预设时长内频繁获得CPU时间片,从而提升了第二线程运行速度,缩短了第二线程运行的时长。同时,内核仅在预设时长内为第二线程优先分配资源,当第二线程在预设时长内未释放锁时,第二线程按照第二优先级运行,由于第二线程的第二优先级高于第一优先级,使得处理器可以按照第二优先级为第二线程分配资源,避免第二线程在超出预设时长后被其他高于第一优先级的线程抢占CPU,导致第二线程处于就绪状态,不能及时释放锁的问题。本示例中,结合修改第二线程的优先级的方式和由内核在预设时长内为第二线程分配资源的方式,两种方式相互互补,可以进一步加快第二线程运行的速度,减小第一线程被阻塞的时长,从而解决因第一线程被阻塞导致的加载数据慢、启动页面出现丢帧、卡顿等问题。In this way, the electronic device can be a mobile phone, a bracelet, a tablet computer, a computer and other devices. When the user operates the application, the first thread in the application runs. The user's operation on the application can be an operation to start the application, an operation to start the applet in the application, an operation to scan a QR code, etc. After the first thread is started, when the first thread fails to lock the target resource and detects that the first thread is the target thread, the priority of the locking thread (i.e., the second thread) is increased; and the first identification information of the locking thread is transmitted to the kernel, and the kernel prioritizes the scheduling of resources for the locking thread within a preset duration. Since the kernel prioritizes scheduling resources for the second thread, the second thread can preferentially obtain the CPU time slice, that is, the second thread can frequently obtain the CPU time slice within the preset duration, thereby improving the running speed of the second thread and shortening the running time of the second thread. At the same time, the kernel only allocates resources to the second thread preferentially within the preset time. When the second thread does not release the lock within the preset time, the second thread runs according to the second priority. Since the second priority of the second thread is higher than the first priority, the processor can allocate resources to the second thread according to the second priority, avoiding the problem that the second thread is preempted by other threads with higher priority than the first priority after exceeding the preset time, causing the second thread to be in a ready state and unable to release the lock in time. In this example, the method of modifying the priority of the second thread and the method of allocating resources to the second thread by the kernel within the preset time are combined. The two methods complement each other, which can further speed up the running speed of the second thread and reduce the length of time the first thread is blocked, thereby solving the problems of slow data loading, frame loss and freeze on the startup page caused by the blocking of the first thread.
根据第一方面,该方法还包括:第一线程对目标资源进行加锁操作,若检测到目标资源的锁为重锁且锁未被第二线程释放,则第一线程确定对目标资源加锁失败。这样,若目标资源的锁处于轻锁状态时,第一线程可以多次尝试上锁,第一线程不会进入阻塞状态;而在锁处于重锁且未被第二线程释放的情况下,第一线程加锁失败后将进入阻塞状态,在该情况下检测第一线程是否属于目标线程,可以避免错误更改第二线程的优先级的情况。According to the first aspect, the method further includes: the first thread performs a locking operation on the target resource, and if it is detected that the lock of the target resource is heavily locked and the lock has not been released by the second thread, the first thread determines that the locking of the target resource has failed. In this way, if the lock of the target resource is in a lightly locked state, the first thread can try to lock it multiple times, and the first thread will not enter a blocked state; and if the lock is heavily locked and has not been released by the second thread, the first thread will enter a blocked state after the locking fails. In this case, detecting whether the first thread belongs to the target thread can avoid the situation where the priority of the second thread is changed incorrectly.
根据第一方面,检测第一线程是否属于所述目标线程,包括:第一线程检测第一线程所属进程是否处于前台运行状态;若第一线程确定第一线程所属进程处于前台运行状态,则检测第一线程是否为关键线程;若第一线程检测到第一线程为关键线程,则确定第一线程属于目标线程。这样,通常应用程序中进程的关键线程被阻塞会导致页面卡顿、丢帧的问题,对于后台运行的应用程序中的关键线程发生阻塞,也不易导致应用程序的显示画面出现异常,因而仅针对处于前台运行状态的进程进行检测,可以快速定位到需要提升第二线程优先级以及由内核优先为第二线程调度资源的场景。According to the first aspect, detecting whether the first thread belongs to the target thread includes: the first thread detecting whether the process to which the first thread belongs is in the foreground running state; if the first thread determines that the process to which the first thread belongs is in the foreground running state, then detecting whether the first thread is a key thread; if the first thread detects that the first thread is a key thread, then determining that the first thread belongs to the target thread. In this way, the blocking of the key thread of the process in the application usually causes page freezes and frame drops. For the blocking of the key thread in the application running in the background, it is not easy to cause abnormalities in the display screen of the application. Therefore, only the process in the foreground running state is detected, and the scenario where the priority of the second thread needs to be increased and the kernel prioritizes the scheduling of resources for the second thread can be quickly located.
根据第一方面,检测第一线程是否为关键线程,包括:第一线程获取第一线程所属进程的第二标识信息;判断第二标识信息是否与第一线程的第三标识信息相同;若检测到第二标识信息与第三标识信息相同,则确定第一线程为关键线程;若检测到第二标识信息与第三标识信息不相同,则确定第一线程不是关键线程。这样,通过第一线程的标识信息以及第一线程所属进程的标识信息,可以快速判断出该第一线程是否为所属进程中的关键线程。According to the first aspect, detecting whether the first thread is a key thread includes: the first thread obtains the second identification information of the process to which the first thread belongs; judging whether the second identification information is the same as the third identification information of the first thread; if the second identification information is detected to be the same as the third identification information, then determining that the first thread is a key thread; if the second identification information is detected to be different from the third identification information, then determining that the first thread is not a key thread. In this way, through the identification information of the first thread and the identification information of the process to which the first thread belongs, it is possible to quickly judge whether the first thread is a key thread in the process to which it belongs.
根据第一方面,内核根据第一标识信息,在预设时长内优先为第二线程调度资源,包括:内核若获取到第一标识信息,则启动计时器;内核检测到计时器记录的时长达到预设时长,则结束对第二线程优先调度资源的操作。这样,通过计时器可以及时检测到内核优先为第二线程调度资源的时长,在达到预设时长后,结束对第二线程优先调度资源的操作,可以避免长时间为第二线程优先调度资源的操作对其他线程造成影响。According to the first aspect, the kernel preferentially schedules resources for the second thread within a preset duration according to the first identification information, including: if the kernel obtains the first identification information, the timer is started; when the kernel detects that the duration recorded by the timer reaches the preset duration, the operation of preferentially scheduling resources for the second thread is terminated. In this way, the duration for which the kernel preferentially schedules resources for the second thread can be detected in time through the timer, and after reaching the preset duration, the operation of preferentially scheduling resources for the second thread is terminated, which can avoid the impact of the operation of preferentially scheduling resources for the second thread for a long time on other threads.
根据第一方面,第一标识信息包括:第二线程的身份标识号和第一属性信息,第一属性信息用于指示内核是否为第二线程优先调度资源。这样,内核可以根据第一属性信息快速确定是否为第二线程优先调度资源。According to the first aspect, the first identification information includes: the identity identification number of the second thread and the first attribute information, and the first attribute information is used to indicate whether the kernel prioritizes the scheduling of resources for the second thread. In this way, the kernel can quickly determine whether to prioritize the scheduling of resources for the second thread according to the first attribute information.
根据第一方面,该方法还包括:当目标线程对目标资源加锁成功时,则将第二线程的第二优先级更新为第一优先级。这样,当第二线程已释放锁后,目标线程尝试对目标资源加锁,且加锁成功后,该目标线程将结束阻塞状态,恢复运行,此时目标线程将第二线程的第二优先级更改为第一优先级,使得第二线程的优先级可以及时恢复原始的第一优先级,避免对其他线程的运行造成影响。According to the first aspect, the method further includes: when the target thread successfully locks the target resource, the second priority of the second thread is updated to the first priority. In this way, when the second thread has released the lock, the target thread attempts to lock the target resource, and after the lock is successful, the target thread will end the blocking state and resume operation. At this time, the target thread changes the second priority of the second thread to the first priority, so that the priority of the second thread can be restored to the original first priority in time to avoid affecting the operation of other threads.
根据第一方面,目标线程将第二线程的第一优先级更新为第二优先级,包括:目标线程检测到锁为重锁,则获取第二线程的第一标识信息;目标线程根据第一标识信息,通过接口将第一优先级更新为第二优先级。这样,第一线程通过调用接口可以将第二线程的第一标识信传递至内核。According to the first aspect, the target thread updates the first priority of the second thread to the second priority, including: the target thread detects that the lock is re-locked, then obtains the first identification information of the second thread; the target thread updates the first priority to the second priority through the interface according to the first identification information. In this way, the first thread can pass the first identification information of the second thread to the kernel by calling the interface.
根据第一方面,预设时长小于或等于20毫秒。这样,内核可以避免长时间为第二线程优先调度资源而影响其他线程的运行的问题。According to the first aspect, the preset duration is less than or equal to 20 milliseconds. In this way, the kernel can avoid the problem of preferentially scheduling resources for the second thread for a long time and affecting the operation of other threads.
根据第一方面,第一线程属于应用程序或属于应用程序中的小程序。这样,第一线程可以属于应用程序,或者属于应用程序中的小程序,增加了该应用程序运行的方法的适用场景。According to the first aspect, the first thread belongs to the application or to a small program in the application. In this way, the first thread can belong to the application or to a small program in the application, which increases the applicable scenarios of the method for running the application.
第二方面,本申请提供一种应用程序运行的方法,包括:响应于用户对应用程序的操作,应用程序中的第一线程运行;若应用程序中第一线程对目标资源加锁失败且检测到第一线程属于目标线程,则目标线程将第二线程的第一优先级更新为第二优先级,第二优先级高于第一优先级,第二线程为持有目标资源的锁的线程;目标线程的优先级高于第一优先级,第二线程按照第二优先级运行。In a second aspect, the present application provides a method for running an application, comprising: in response to a user operation on the application, a first thread in the application runs; if the first thread in the application fails to lock a target resource and it is detected that the first thread belongs to the target thread, the target thread updates the first priority of the second thread to a second priority, the second priority is higher than the first priority, and the second thread is the thread holding the lock of the target resource; the priority of the target thread is higher than the first priority, and the second thread runs according to the second priority.
这样,该电子设备可以是手机、手环、平板电脑、电脑等设备。在应用程序的第一线程对目标资源加锁失败且检测到第一线程为目标线程时,提高持锁线程(即第二线程)的优先级;第二线程按照第二优先级运行,由于第二线程的第二优先级高于第一优先级,使得处理器可以按照第二优先级为第二线程分配资源,可以减小高于第一优先级的线程抢占CPU的概率,从而可以减小第一线程长时间处于阻塞的概率,从而减小因第一线程被长时间阻塞导致的加载数据慢、启动页面出现丢帧、卡顿等问题。In this way, the electronic device can be a mobile phone, a bracelet, a tablet computer, a computer and other devices. When the first thread of the application fails to lock the target resource and detects that the first thread is the target thread, the priority of the lock-holding thread (i.e., the second thread) is increased; the second thread runs at the second priority, and since the second priority of the second thread is higher than the first priority, the processor can allocate resources to the second thread according to the second priority, which can reduce the probability of threads higher than the first priority seizing the CPU, thereby reducing the probability of the first thread being blocked for a long time, thereby reducing the problems of slow data loading, frame loss, and freezes on the startup page caused by the first thread being blocked for a long time.
根据第二方面,检测第一线程是否属于目标线程,包括:第一线程检测第一线程所属进程是否处于前台运行状态;若第一线程确定第一线程所属进程处于前台运行状态,则检测第一线程是否为关键线程;若第一线程检测到第一线程为关键线程,则确定第一线程属于目标线程。According to the second aspect, detecting whether the first thread belongs to the target thread includes: the first thread detecting whether the process to which the first thread belongs is in the foreground running state; if the first thread determines that the process to which the first thread belongs is in the foreground running state, detecting whether the first thread is a key thread; if the first thread detects that the first thread is a key thread, determining that the first thread belongs to the target thread.
根据第二方面,检测第一线程是否为关键线程,包括:第一线程获取第一线程所属进程的第二标识信息;判断第二标识信息是否与第一线程的第三标识信息相同;若检测到第二标识信息与第三标识信息相同,则确定第一线程为关键线程;若检测到第二标识信息与第三标识信息不相同,则确定第一线程不是关键线程。According to the second aspect, detecting whether the first thread is a critical thread includes: the first thread obtains second identification information of the process to which the first thread belongs; determining whether the second identification information is the same as the third identification information of the first thread; if it is detected that the second identification information is the same as the third identification information, determining that the first thread is the critical thread; if it is detected that the second identification information is not the same as the third identification information, determining that the first thread is not the critical thread.
根据第二方面,该方法还包括:当目标线程对目标资源加锁成功时,则将第二线程的第二优先级更新为第一优先级。According to the second aspect, the method further includes: when the target thread successfully locks the target resource, updating the second priority of the second thread to the first priority.
第二方面的任意一种实现方式分别与第一方面的任意一种实现方式相对应。第二方面的任意一种实现方式所对应的技术效果可参见上述第一方面的任意一种实现方式所对应的技术效果,此处不再赘述。Any implementation of the second aspect corresponds to any implementation of the first aspect. The technical effect corresponding to any implementation of the second aspect can refer to the technical effect corresponding to any implementation of the first aspect, which will not be repeated here.
第三方面,本申请提供了一种电子设备,包括:存储器和处理器,存储器与处理器耦合;存储器存储有程序指令,当程序指令由处理器执行时,使得电子设备执行第一方面的任意一种实现方式对应的应用程序运行的方法,或者,执行第二方面的任意一种实现方式对应的应用程序运行的方法。In a third aspect, the present application provides an electronic device comprising: a memory and a processor, the memory being coupled to the processor; the memory storing program instructions, which, when executed by the processor, enables the electronic device to execute a method for running an application corresponding to any one of the implementations of the first aspect, or to execute a method for running an application corresponding to any one of the implementations of the second aspect.
第三方面以及第三方面的任意一种实现方式分别与第一方面以及第一方面的任意一种实现方式相对应,或者,与第二方面以及第二方面的任意一种实现方式相对应,此处不再赘述。The third aspect and any implementation method of the third aspect respectively correspond to the first aspect and any implementation method of the first aspect, or correspond to the second aspect and any implementation method of the second aspect, and will not be repeated here.
第四方面,本申请提供了一种计算机可读介质,用于存储计算机程序,当计算机程序在电子设备上运行时,使得电子设备执行第一方面的任意一种实现方式对应的应用程序运行的方法,或者,执行第二方面的任意一种实现方式对应的应用程序运行的方法。In a fourth aspect, the present application provides a computer-readable medium for storing a computer program. When the computer program is run on an electronic device, the electronic device executes a method for running an application corresponding to any one of the implementations of the first aspect, or executes a method for running an application corresponding to any one of the implementations of the second aspect.
附图说明BRIEF DESCRIPTION OF THE DRAWINGS
为了更清楚地说明本申请实施例的技术方案,下面将对本申请实施例的描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本申请的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动性的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the description of the embodiments of the present application will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
图1是示例性示出的一种电子设备启动视频应用程序的示意图;FIG1 is a schematic diagram showing an exemplary electronic device starting a video application;
图2是示例性示出的一个关键线程各运行状态所对应的时长;FIG2 is an exemplary diagram showing the duration corresponding to each running state of a key thread;
图3是示例性示出的java对象头结构;FIG3 is an exemplary diagram of a Java object header structure;
图4a是示例性示出的线程A竞争共享资源的锁的示意图;FIG4a is a schematic diagram showing an exemplary embodiment of thread A competing for a lock of a shared resource;
图4b是示例性示出的一种应用程序的启动页面出现卡顿时线程调用CPU的示意图;FIG4b is a schematic diagram showing an example of a thread calling a CPU when a startup page of an application program freezes;
图5是示例性示出的一种电子设备的结构示意图;FIG5 is a schematic diagram showing the structure of an electronic device;
图6是示例性示出的一种电子设备的软件结构框图;FIG6 is a block diagram showing an exemplary software structure of an electronic device;
图7是示例性示出的一种应用程序运行的流程图;FIG7 is a flowchart showing an exemplary operation of an application program;
图8是示例性示出的第一线程、第二线程以及内核之间的交互的示意图;FIG8 is a schematic diagram exemplarily showing the interaction between the first thread, the second thread and the kernel;
图9是示例性示出的采用本申请中应用程序运行的方法的线程调用CPU的示意图;FIG9 is a schematic diagram exemplarily showing a thread calling a CPU using the method for running an application program in the present application;
图10是示例性示出的一种应用程序运行的流程图;FIG10 is a flowchart showing an exemplary operation of an application program;
图11是示例性示出的第一线程、第二线程之间的交互的示意图。FIG. 11 is a schematic diagram exemplarily showing the interaction between a first thread and a second thread.
具体实施方式Detailed ways
下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本申请一部分实施例,而不是全部的实施例。基于本申请中的实施例,本领域普通技术人员在没有作出创造性劳动前提下所获得的所有其他实施例,都属于本申请保护的范围。The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
在对本申请实施例的技术方案说明之前,首先结合附图对本申请实施例的应用场景进行说明。图1为示例性示出的一种电子设备启动某视频应用程序(如应用程序A)的示意图。如图1所示,手机响应于用户的启动操作,手机的显示界面101显示该应用程序A的启动动画。启动动画正常的画面如图1的1a所示,当启动动画出现卡顿或丢帧的情况时,在显示界面102中的显示效果如图1的1b所示。当用户发现手机出现如图1的1b所示的卡顿问题时,导致用户误以为电子设备出现运行卡顿的问题,降低了用户使用电子设备的使用体验。Before describing the technical solution of the embodiment of the present application, the application scenario of the embodiment of the present application is first described in conjunction with the accompanying drawings. Figure 1 is a schematic diagram of an electronic device starting a video application (such as application A) as shown in an exemplary manner. As shown in Figure 1, the mobile phone responds to the user's startup operation, and the display interface 101 of the mobile phone displays the startup animation of application A. The normal screen of the startup animation is shown in 1a of Figure 1. When the startup animation is stuck or frame-dropped, the display effect in the display interface 102 is shown in 1b of Figure 1. When the user finds that the mobile phone has a stuck problem as shown in 1b of Figure 1, the user mistakenly believes that the electronic device has a stuck problem, which reduces the user experience of using the electronic device.
在另一种示例中,如某聊天应用中的小程序在启动时,也存在启动时间长的问题。具有扫描功能的应用程序或小程序,从结束扫描二维码至显示扫描结果之间的时长长,用户容易误以为应用程序出现卡死的问题。通常用户在遇到应用程序卡死时,会手动杀掉应用程序(即结束整个应用程序),但是,即使应用程序或小程序被重启,也可能还存在上述的问题(如显示二维码的结果的时长长),降低了用户对电子设备的使用体验。In another example, when a small program in a chat application is started, there is also a problem of long startup time. For applications or small programs with scanning functions, the time from the end of scanning the QR code to the display of the scan results is long, and users are easily mistaken for the problem that the application is stuck. Usually, when the user encounters an application stuck, they will manually kill the application (that is, end the entire application), but even if the application or small program is restarted, the above problems may still exist (such as the long time to display the results of the QR code), which reduces the user's experience of using the electronic device.
应用程序运行过程中,会产生多个进程,每个进程包括多个(如2个及以上)的线程。多个线程可以同步运行。本申请中对图1中的应用程序A进行性能分析,例如可以通过性能分析工具分析卡顿页面中线程的运行状态。图2为示例性示出的某一关键线程各运行状态所对应的时长。该关键线程可以是该应用程序A的启动页面的UI(User Interface,人机交互界面)线程。如图2所示,该线程在启动过程中耗时的总时长(wall Duration)为234.58ms。该线程处于就绪状态的时长为195.764151ms,该线程处于运行状态的时长为22.721464ms,该线程处于睡眠状态的时长为8.95877ms,该线程处于不可中断睡眠状态的时长为7.135615ms。该线程处于就绪状态的时长占总时长的83%,而运行时长占总时长的9.6%,可见,该UI线程在就绪状态中消耗的时间多,导致了该UI线程总运行时长长的问题,使得显示页面的时间变长,或者出现卡顿、丢帧的问题。During the running of the application, multiple processes will be generated, and each process includes multiple (such as 2 or more) threads. Multiple threads can run synchronously. In this application, the performance analysis of the application A in Figure 1 is performed. For example, the running state of the thread in the stuck page can be analyzed by a performance analysis tool. Figure 2 is an exemplary illustration of the duration corresponding to each running state of a certain key thread. The key thread can be the UI (User Interface, human-computer interaction interface) thread of the startup page of the application A. As shown in Figure 2, the total time (wall Duration) consumed by the thread during the startup process is 234.58ms. The duration of the thread in the ready state is 195.764151ms, the duration of the thread in the running state is 22.721464ms, the duration of the thread in the sleep state is 8.95877ms, and the duration of the thread in the uninterruptible sleep state is 7.135615ms. The time that the thread is in the ready state accounts for 83% of the total time, while the running time accounts for 9.6% of the total time. It can be seen that the UI thread spends a lot of time in the ready state, resulting in the problem of the total running time of the UI thread being long, which makes the page display time longer, or causes freezes and frame drops.
具体地,应用程序在运行时,手机的操作系统为该应用程序创建一个进程,该进程中可以包括一个或多个线程。当手机中有多个应用程序同时运行时,CPU允许每个程序的指令和数据在CPU上运行一段很短的时间(时间片),这个程序时间片运行结束,则切换到下一个程序。由于该CPU的分时机制,当多线程并发访问共享数据时,为了保证共享数据在同一时刻只被一个或一些线程使用,通常采用互斥同步的方式实现,例如,悲观锁(Synchronized),该Synchronized通过Monitor(管程)同步机制实现。Specifically, when an application is running, the operating system of the mobile phone creates a process for the application, which may include one or more threads. When multiple applications are running simultaneously in a mobile phone, the CPU allows the instructions and data of each program to run on the CPU for a very short period of time (time slice). When the program time slice ends, it switches to the next program. Due to the time-sharing mechanism of the CPU, when multiple threads concurrently access shared data, in order to ensure that the shared data is only used by one or some threads at the same time, it is usually implemented in a mutually exclusive synchronization manner, such as pessimistic locking (Synchronized), which is implemented through the Monitor synchronization mechanism.
图3为示例性示出该java对象头的结构。从对象头中可以识别目标资源的锁状态的相关信息。如图3所示,锁的状态包括未上锁状态(Unlock State)、轻锁(Thin State,也称为“瘦锁”)状态和重锁(fat State也称为“胖锁”),在实际应用中锁还包括其他的状态,本示例中未一一列举出。FIG3 is an exemplary diagram showing the structure of the Java object header. The relevant information of the lock state of the target resource can be identified from the object header. As shown in FIG3, the lock state includes an unlocked state (Unlock State), a light lock (Thin State, also known as "thin lock") state and a heavy lock (Fat State, also known as "fat lock"). In actual applications, the lock also includes other states, which are not listed one by one in this example.
当共享资源中的锁处于未上锁状态时,该共享资源可以被任意一个线程加锁并获取该共享资源的锁。该对象头结构如图3所示,该对象头包括32位,第30~31位用于指示锁类型,其中,“00”用于指示该锁的状态为未上锁状态或轻锁状态。第29位和第28位分别为标志位(mark bit)和读屏障位(Read Barrier bit),该两位用于配合GC(GarbageCollection,“垃圾回收”)机制,此处不再进行详述。当锁处于未上锁状态时,0~27位为全0。When the lock in a shared resource is in an unlocked state, the shared resource can be locked by any thread and the lock of the shared resource can be acquired. The structure of the object header is shown in Figure 3. The object header includes 32 bits, and bits 30 to 31 are used to indicate the lock type, where "00" is used to indicate that the lock is in an unlocked state or a lightly locked state. Bits 29 and 28 are the mark bit and the read barrier bit, respectively. These two bits are used to cooperate with the GC (Garbage Collection) mechanism and will not be described in detail here. When the lock is in an unlocked state, bits 0 to 27 are all 0.
轻锁状态:是指在一段时间内请求的该锁的线程较少,且持有锁的线程执行时间短,其他等待线程在有限的自旋等待期可以得到该锁。该对象头结构的第30~31位同样用于指示锁类型,第29位和第28位分别为mark bit和Read Barrier bit。0~27位包括加锁次数(lock count)和持锁线程的身份标识号(ower thread id)。Light lock state: refers to the situation that fewer threads request the lock within a period of time, and the execution time of the thread holding the lock is short, and other waiting threads can obtain the lock in a limited spin waiting period. Bits 30 to 31 of the object header structure are also used to indicate the lock type, and bits 29 and 28 are mark bit and Read Barrier bit respectively. Bits 0 to 27 include the lock count and the identity number of the thread holding the lock.
重锁状态:当共享资源中的锁处于重锁状态时,仅有一个线程可以持有该锁,当多个线程同时访问该共享资源时,非持锁者线程会先尝试获取该共享资源的锁对象中monitor的所有权;若非持锁者线程获取monitor的所有权失败,当前非持锁者线程会被阻塞,等待持锁者线程解锁后被唤醒,并再次竞争该共享资源的锁。该对象头结构的第30~31位同样用于指示锁类型,其中,“01”用于指示当前为重锁状态。第29位和第28位分别为标志位(Mark bit)和读屏障位(Read Barrier bit),该两位用于配合GC(Garbage Collection,“垃圾回收”)机制,此处不再进行详述。0~27位包括管程对象的身份标识号(Monitor Id)。Re-locked state: When the lock in a shared resource is in a re-locked state, only one thread can hold the lock. When multiple threads access the shared resource at the same time, the non-lock holder thread will first try to obtain the ownership of the monitor in the lock object of the shared resource; if the non-lock holder thread fails to obtain the ownership of the monitor, the current non-lock holder thread will be blocked, waiting for the lock holder thread to unlock and then wake up, and compete for the lock of the shared resource again. Bits 30 to 31 of the object header structure are also used to indicate the lock type, where "01" is used to indicate that the current state is re-locked. Bits 29 and 28 are the mark bit and the read barrier bit, respectively. These two bits are used to cooperate with the GC (Garbage Collection) mechanism and will not be described in detail here. Bits 0 to 27 include the monitor object's identity number (Monitor Id).
图4a为示例性示出的线程A竞争共享资源的锁时,CPU被占用的示意图。如图4a所示,线程C在T0时刻访问共享资源,并对共享资源加锁成功,即该线程C在T0时刻获取到该共享资源的锁。若该锁的状态为重锁状态,当线程A在T1时刻对该共享资源竞争加锁,由于该共享资源的锁已被线程C持有,且该锁的状态为重锁,该线程A将被阻塞,等待线程C释放该共享资源的锁。当线程C在T2时刻释放该共享资源的锁,线程A被线程C唤醒,并再次竞争该共享资源的锁,此时由于线程C已释放该共享资源的锁,该线程A持锁成功。如图4a所示,T1~T3时刻为该线程A处于阻塞状态的时长。FIG4a is a schematic diagram showing an example of CPU being occupied when thread A competes for the lock of a shared resource. As shown in FIG4a , thread C accesses the shared resource at time T0 and successfully locks the shared resource, that is, thread C obtains the lock of the shared resource at time T0. If the state of the lock is a relocked state, when thread A competes to lock the shared resource at time T1, since the lock of the shared resource is already held by thread C and the state of the lock is a relocked state, thread A will be blocked and wait for thread C to release the lock of the shared resource. When thread C releases the lock of the shared resource at time T2, thread A is awakened by thread C and competes for the lock of the shared resource again. At this time, since thread C has released the lock of the shared resource, thread A successfully holds the lock. As shown in FIG4a , time T1 to T3 is the duration of the time when thread A is in a blocked state.
在一些场景中,如图1的1b所示,应用程序的启动页面出现卡顿的场景中,假设UI线程为线程A。线程A的优先级高于线程B的优先级,线程B的优先级高于线程C的优先级。该优先级越高的线程越先调用运算资源。In some scenarios, as shown in 1b of FIG1 , in a scenario where the startup page of an application is stuck, it is assumed that the UI thread is thread A. The priority of thread A is higher than the priority of thread B, and the priority of thread B is higher than the priority of thread C. The thread with a higher priority calls computing resources first.
如图4b所示,线程C在T0时刻对共享资源加了锁,且该锁的状态为重锁状态。线程A在T1时刻对该共享资源加锁,由于目标资源的锁已经被线程C持有,该线程A竞争锁失败,导致该线程A进入阻塞状态。由于线程B的优先级高于线程C的优先级,CPU将优先为线程B调度资源,也就是说,线程B将先于线程C占用CPU。如图4b所示,线程B在T2时刻抢占CPU,线程C在T2时刻变为就绪状态,等待线程B运行。线程B在T3时刻运行完毕,释放占用的CPU,此时线程C获得CPU,继续运行,直至在T4时刻释放锁。当线程C释放锁之后,该线程A竞争该锁成功。由于线程B的优先级高于线程C,线程B可以优先线程C抢占CPU,导致线程C的运行时长增加(即增加的时长为T4-T3),导致线程A的阻塞时长增加。也就是说,如图4b中所示的场景中,线程B的优先级高于持锁线程的优先级,优先持锁线程调度资源,持锁线程的总运行时长(包括就绪时长和运行时长)增加,UI线程的阻塞时长增加,导致该应用程序的启动页面出现卡顿、丢帧等现象。As shown in Figure 4b, thread C locks the shared resource at time T0, and the lock is in the relocked state. Thread A locks the shared resource at time T1. Since the lock of the target resource is already held by thread C, thread A fails to compete for the lock, causing thread A to enter a blocked state. Since thread B has a higher priority than thread C, the CPU will prioritize thread B in scheduling resources, that is, thread B will occupy the CPU before thread C. As shown in Figure 4b, thread B seizes the CPU at time T2, and thread C becomes ready at time T2, waiting for thread B to run. Thread B finishes running at time T3 and releases the occupied CPU. At this time, thread C obtains the CPU and continues to run until the lock is released at time T4. After thread C releases the lock, thread A successfully competes for the lock. Since thread B has a higher priority than thread C, thread B can seize the CPU before thread C, resulting in an increase in the running time of thread C (that is, the increase is T4-T3), which increases the blocking time of thread A. That is to say, in the scenario shown in Figure 4b, the priority of thread B is higher than the priority of the lock holding thread, and the lock holding thread is prioritized in scheduling resources. The total running time (including ready time and running time) of the lock holding thread increases, and the blocking time of the UI thread increases, causing the startup page of the application to become stuck, drop frames, and other phenomena.
本申请实施例中提供了一种应用程序运行的方法,该方法由电子设备执行,该电子设备可以是手机、手环、平板电脑、电脑等设备。在应用程序的第一线程对目标资源加锁失败且检测到第一线程为目标线程时,提高持锁线程的优先级;并将持锁线程的标识信息传输至内核,由内核在预设时长内优先为该持锁线程调度资源。通过内核为持锁线程优先调度资源,若预设时长内持锁线程未释放锁,则该持锁线程可以按照高优先级运行,使得处理器可以优先为该持锁线程分配资源,进一步缩短持锁线程的运行时长,缩短第一线程的阻塞时长,以解决应用程序出现丢帧、卡顿的问题、应用程序显示扫码结果时间过长的问题。A method for running an application is provided in an embodiment of the present application, and the method is executed by an electronic device, and the electronic device can be a mobile phone, a bracelet, a tablet computer, a computer and other devices. When the first thread of the application fails to lock the target resource and the first thread is detected as the target thread, the priority of the lock-holding thread is increased; and the identification information of the lock-holding thread is transmitted to the kernel, and the kernel prioritizes scheduling resources for the lock-holding thread within a preset time. By giving priority to scheduling resources for the lock-holding thread by the kernel, if the lock-holding thread does not release the lock within the preset time, the lock-holding thread can run at a high priority, so that the processor can prioritize allocating resources to the lock-holding thread, further shortening the running time of the lock-holding thread, and shortening the blocking time of the first thread, so as to solve the problems of frame loss and freeze in the application, and the problem that the application takes too long to display the scan result.
图5为本申请实施例示出的一种电子设备100的结构示意图。应该理解的是,图5示电子设备100仅是电子设备的一个范例,并且电子设备100可以具有比图中所示的更多的或者更少的部件,可以组合两个或多个的部件,或者可以具有不同的部件配置。图1中所示出的各种部件可以在包括一个或多个信号处理和/或专用集成电路在内的硬件、软件、或硬件和软件的组合中实现。FIG5 is a schematic diagram of the structure of an electronic device 100 shown in an embodiment of the present application. It should be understood that the electronic device 100 shown in FIG5 is only an example of an electronic device, and the electronic device 100 may have more or fewer components than those shown in the figure, may combine two or more components, or may have different component configurations. The various components shown in FIG1 may be implemented in hardware, software, or a combination of hardware and software including one or more signal processing and/or application specific integrated circuits.
电子设备100可以包括:处理器110,外部存储器接口120,内部存储器121,通用串行总线(universal serial bus,USB)接口130,充电管理模块140,电源管理模块141,电池142,天线1,天线2,移动通信模块150,无线通信模块160,音频模块170,扬声器170A,受话器170B,麦克风170C,耳机接口170D,传感器模块180,按键190,马达191,指示器192,摄像头193,显示屏194,以及用户标识模块(subscriber identification module,SIM)卡接口195等。其中传感器模块180可以包括压力传感器,陀螺仪传感器,气压传感器,磁传感器,加速度传感器,距离传感器,接近光传感器,指纹传感器,温度传感器,触摸传感器,环境光传感器,骨传导传感器等。The electronic device 100 may include: a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor, a gyroscope sensor, an air pressure sensor, a magnetic sensor, an acceleration sensor, a distance sensor, a proximity light sensor, a fingerprint sensor, a temperature sensor, a touch sensor, an ambient light sensor, a bone conduction sensor, etc.
图6是本申请实施例的电子设备100的软件结构框图。FIG. 6 is a software structure block diagram of the electronic device 100 according to an embodiment of the present application.
电子设备100的分层架构将软件分成若干个层,每一层都有清晰的角色和分工。层与层之间通过软件接口通信。在一些实施例中,将Android系统分为四层,从上至下分别为应用程序层,应用程序框架层,安卓运行时(Android runtime)和系统库,以及内核层。The layered architecture of the electronic device 100 divides the software into several layers, each with clear roles and division of labor. The layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers, from top to bottom, namely, the application layer, the application framework layer, the Android runtime (Android runtime) and the system library, and the kernel layer.
应用程序层可以包括一系列应用程序包。The application layer can include a series of application packages.
如图6所示,应用程序包可以包括视频,音乐,地图,WLAN,日历,短信息,图库,通话,导航,蓝牙,相机等应用程序。As shown in FIG6 , the application package may include applications such as video, music, map, WLAN, calendar, short message, gallery, call, navigation, Bluetooth, camera, etc.
应用程序框架层为应用程序层的应用程序提供应用编程接口(applicationprogramming interface,API)和编程框架。应用程序框架层包括一些预先定义的函数。The application framework layer provides an application programming interface (API) and a programming framework for the applications in the application layer. The application framework layer includes some predefined functions.
如图6所示,应用程序框架层可以包括窗口管理器,内容提供器,视图系统,电话管理器,资源管理器,通知管理器等。As shown in FIG. 6 , the application framework layer may include a window manager, a content provider, a view system, a telephony manager, a resource manager, a notification manager, and the like.
窗口管理器用于管理窗口程序。窗口管理器可以获取显示屏大小,判断是否有状态栏,锁定屏幕,截取屏幕等。The window manager is used to manage window programs. The window manager can obtain the display screen size, determine whether there is a status bar, lock the screen, capture the screen, etc.
内容提供器用来存放和获取数据,并使这些数据可以被应用程序访问。所述数据可以包括视频,图像,音频,拨打和接听的电话,浏览历史和书签,电话簿等。Content providers are used to store and retrieve data and make it accessible to applications. The data may include videos, images, audio, calls made and received, browsing history and bookmarks, phone books, etc.
视图系统包括可视控件,例如显示文字的控件,显示图片的控件等。视图系统可用于构建应用程序。显示界面可以由一个或多个视图组成的。例如,包括短信通知图标的显示界面,可以包括显示文字的视图以及显示图片的视图。The view system includes visual controls, such as controls for displaying text, controls for displaying images, etc. The view system can be used to build applications. A display interface can be composed of one or more views. For example, a display interface including a text notification icon can include a view for displaying text and a view for displaying images.
电话管理器用于提供电子设备100的通信功能。例如通话状态的管理(包括接通,挂断等)。The phone manager is used to provide communication functions of the electronic device 100, such as management of call status (including connecting, hanging up, etc.).
资源管理器为应用程序提供各种资源,比如本地化字符串,图标,图片,布局文件,视频文件等等。The resource manager provides various resources for applications, such as localized strings, icons, images, layout files, video files, and so on.
通知管理器使应用程序可以在状态栏中显示通知信息,可以用于传达告知类型的消息,可以短暂停留后自动消失,无需用户交互。比如通知管理器被用于告知下载完成,消息提醒等。通知管理器还可以是以图表或者滚动条文本形式出现在系统顶部状态栏的通知,例如后台运行的应用程序的通知,还可以是以对话窗口形式出现在屏幕上的通知。例如在状态栏提示文本信息,发出提示音,电子设备振动,指示灯闪烁等。The notification manager enables applications to display notification information in the status bar. It can be used to convey notification-type messages and can disappear automatically after a short stay without user interaction. For example, the notification manager is used to notify download completion, message reminders, etc. The notification manager can also be a notification that appears in the system top status bar in the form of a chart or scroll bar text, such as notifications of applications running in the background, or a notification that appears on the screen in the form of a dialog window. For example, a text message is displayed in the status bar, a prompt sound is emitted, an electronic device vibrates, an indicator light flashes, etc.
Android Runtime包括核心库和虚拟机。Android runtime负责安卓系统的调度和管理。Android Runtime includes core libraries and virtual machines. Android runtime is responsible for scheduling and management of the Android system.
核心库包含两部分:一部分是java语言需要调用的功能函数,另一部分是安卓的核心库。The core library consists of two parts: one part is the function that needs to be called by the Java language, and the other part is the Android core library.
应用程序层和应用程序框架层运行在虚拟机中。虚拟机将应用程序层和应用程序框架层的java文件执行为二进制文件。虚拟机用于执行对象生命周期的管理,堆栈管理,线程管理,安全和异常的管理,以及垃圾回收等功能。The application layer and the application framework layer run in a virtual machine. The virtual machine executes the Java files of the application layer and the application framework layer as binary files. The virtual machine is used to perform functions such as object life cycle management, stack management, thread management, security and exception management, and garbage collection.
系统库可以包括多个功能模块。例如:表面管理器(surface manager),媒体库(Media Libraries),三维图形处理库(例如:OpenGL ES),2D图形引擎(例如:SGL)等。The system library may include multiple functional modules, such as surface manager, media library, 3D graphics processing library (such as OpenGL ES), 2D graphics engine (such as SGL), etc.
表面管理器用于对显示子系统进行管理,并且为多个应用程序提供了2D和3D图层的融合。The surface manager is used to manage the display subsystem and provide the fusion of 2D and 3D layers for multiple applications.
媒体库支持多种常用的音频,视频格式回放和录制,以及静态图像文件等。媒体库可以支持多种音视频编码格式,例如:MPEG4,H.264,MP3,AAC,AMR,JPG,PNG等。The media library supports playback and recording of a variety of commonly used audio and video formats, as well as static image files, etc. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.
三维图形处理库用于实现三维图形绘图,图像渲染,合成,和图层处理等。The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, compositing, and layer processing.
2D图形引擎是2D绘图的绘图引擎。A 2D graphics engine is a drawing engine for 2D drawings.
内核层是硬件和软件之间的层。内核层包括内核调度模块,该内核调度模块可以修改线程的优先等级;也可以根据第一线程的指示,在预设时长内为第二线程优先调度资源。该内核层还可以包块各驱动,例如,内核层包含显示驱动,摄像头驱动,音频驱动,传感器驱动。The kernel layer is the layer between hardware and software. The kernel layer includes a kernel scheduling module, which can modify the priority of threads; it can also prioritize resources for the second thread within a preset time according to the instructions of the first thread. The kernel layer can also package various drivers. For example, the kernel layer includes display drivers, camera drivers, audio drivers, and sensor drivers.
可以理解的是,图6示出的软件结构中的层以及各层中包含的部件,并不构成对电子设备100的具体限定。在本申请另一些实施例中,电子设备100可以包括比图示更多或更少的层,以及每个层中可以包括更多或更少的部件,本申请不做限定。It is understandable that the layers in the software structure shown in FIG6 and the components contained in each layer do not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer layers than shown in the figure, and each layer may include more or fewer components, which is not limited in the present application.
图7为示例性示出的一种应用程序运行的流程图。该应用程序运行的方法由电子设备执行,本示例中,电子设备以手机为例进行说明。该应用程序运行的过程包括:FIG7 is a flowchart showing an example of an application program running. The method for running the application program is executed by an electronic device. In this example, the electronic device is described by taking a mobile phone as an example. The process of running the application program includes:
步骤701:响应于用户对应用程序的操作,应用程序中的第一线程运行。Step 701: In response to a user operation on an application, a first thread in the application runs.
具体地,手机中安装有多个应用程序,手机响应于用户启动应用程序的操作,启动应用程序。当应用程序启动(或者小程序启动、显示扫描结果)过程中,手机的操作系统为该应用程序创建一个进程,该进程包括一个或多个线程,手机允许多个线程同时运行。本示例中,第一线程可以是应用程序运行中的任意一个线程。用户对应用程序的操作可以包括:启动应用程序,启动小程序、扫描二维码等操作。Specifically, a mobile phone has multiple applications installed, and the mobile phone starts the application in response to the user's operation of starting the application. When the application is started (or the mini program is started, the scan result is displayed), the operating system of the mobile phone creates a process for the application, and the process includes one or more threads. The mobile phone allows multiple threads to run simultaneously. In this example, the first thread can be any thread in the running application. The user's operation on the application may include: starting the application, starting the mini program, scanning the QR code, and other operations.
步骤702:若应用程序中第一线程对目标资源加锁失败且检测到第一线程属于目标线程,则目标线程将第二线程的第一优先级更新为第二优先级。Step 702: If the first thread in the application fails to lock the target resource and it is detected that the first thread belongs to the target thread, the target thread updates the first priority of the second thread to the second priority.
具体地,当第一线程对目标资源加锁失败时,该第一线程可以检测第一线程是否属于目标线程。该目标资源可以是数据资源、代码资源、输入/输出资源等。Specifically, when the first thread fails to lock the target resource, the first thread may detect whether the first thread belongs to the target thread. The target resource may be a data resource, a code resource, an input/output resource, etc.
可选地,第一线程可以检测第一线程所属进程是否处于前台运行状态,若检测到第一线程所属进程处于前台运行状态,则检测第一线程是否为关键线程。关键线程可以是该进程中的主线程。Optionally, the first thread may detect whether the process to which the first thread belongs is in the foreground running state, and if it is detected that the process to which the first thread belongs is in the foreground running state, then detect whether the first thread is a key thread. The key thread may be the main thread in the process.
可选地,第一线程检测所属进程是否处于前台运行状态的方式有多种,例如,第一线程可以检测所属进程中是否有画布被显示,若是,则确定该第一线程所属进程处于前台运行状态。可选地,进程中可以设置运行状态的运行属性信息,若该运行属性信息为真,则指示该进程处于前台运行状态。该第一线程可以获取该第一线程所属进程中的运行属性信息,即可确定该第一线程所属进程是否处于前台运行状态。Optionally, there are multiple ways for the first thread to detect whether the process to which it belongs is in the foreground running state. For example, the first thread can detect whether a canvas is displayed in the process to which it belongs. If so, it is determined that the process to which the first thread belongs is in the foreground running state. Optionally, running attribute information of the running state can be set in the process. If the running attribute information is true, it indicates that the process is in the foreground running state. The first thread can obtain the running attribute information in the process to which the first thread belongs, and can determine whether the process to which the first thread belongs is in the foreground running state.
可选地,当第一线程确定所属进程处于前台运行状态,该第一线程可以检测该第一线程所属进程的第二标识信息是否与第一线程的第三标识信息相同;若第一线程确定该第二标识信息与第三标识信息相同,则确定第一线程属于关键线程。Optionally, when the first thread determines that the process to which it belongs is in the foreground running state, the first thread can detect whether the second identification information of the process to which the first thread belongs is the same as the third identification information of the first thread; if the first thread determines that the second identification information is the same as the third identification information, it is determined that the first thread belongs to a critical thread.
若第一线程检测到第一线程所属进程未处于前台运行状态,则结束流程。If the first thread detects that the process to which the first thread belongs is not in the foreground running state, the process ends.
当第一线程对目标资源加载失败且检测到第一线程属于目标线程,则该第一线程将第二线程的第一优先级更新为第二优先级,第二优先级高于第一优先级,第二线程为持有目标资源的锁的线程;目标线程的优先级高于第一优先级。When the first thread fails to load the target resource and it is detected that the first thread belongs to the target thread, the first thread updates the first priority of the second thread to the second priority, the second priority is higher than the first priority, and the second thread is the thread holding the lock of the target resource; the priority of the target thread is higher than the first priority.
可选地,第二优先级可以是第二线程所属优先级范围内的最高优先级,例如,第二线程所属优先级的范围为139~100,其中,数值为139表示的优先级最小,数值为100表示的优先级最高;若第一优先级为139,该第一线程可以将第二线程的优先级从139更新为100。Optionally, the second priority can be the highest priority within the priority range of the second thread. For example, the priority range of the second thread is 139 to 100, where the value 139 represents the lowest priority and the value 100 represents the highest priority. If the first priority is 139, the first thread can update the priority of the second thread from 139 to 100.
可选地,该第一线程还可以将第二线程的优先级在预设的优先级范围内提升预设级数,例如,优先级的范围为139~100,若第二线程的第一优先级为139,预设级数为20,则该第一线程可以将第二线程的优先级从139更新为119。Optionally, the first thread can also increase the priority of the second thread by a preset level within a preset priority range. For example, the priority range is 139 to 100. If the first priority of the second thread is 139 and the preset level is 20, the first thread can update the priority of the second thread from 139 to 119.
步骤703:目标线程在进入阻塞状态之前,将第二线程的第一标识信息传输至内核。Step 703: Before entering the blocking state, the target thread transmits the first identification information of the second thread to the kernel.
具体地,由于第一线程加锁失败且在进入阻塞状态之前,该第一线程还可以将第二线程的第一标识信息传输至内核。该第一标识信息可以包括第二线程的线程的ID。可选地,第一标识信息中还可以包括第一属性信息,该第一属性信息用于指示内核是否优先为第一标识信息对应的线程调用资源。例如,第一属性信息以“is_vip”表示,若第一属性信息的值为true时,指示内核确定在预设时长内优先为该该第一属性所属线程调用资源。Specifically, since the first thread fails to lock and before entering the blocking state, the first thread may also transmit the first identification information of the second thread to the kernel. The first identification information may include the thread ID of the second thread. Optionally, the first identification information may also include first attribute information, which is used to indicate whether the kernel gives priority to calling resources for the thread corresponding to the first identification information. For example, the first attribute information is represented by "is_vip". If the value of the first attribute information is true, it indicates that the kernel determines to give priority to calling resources for the thread to which the first attribute belongs within a preset time length.
步骤704:内核根据第一标识信息,在预设时长内优先为第二线程调度资源。Step 704: The kernel preferentially schedules resources for the second thread within a preset time period according to the first identification information.
具体地,内核获取到第一标识信息,根据第一标识信息中的指示,在预设时长内优先为第二线程调度资源。可选地,内核见到到第一标识信息中的第一属性信息为true,则可以在预设时长内优先为第二线程调度资源。Specifically, the kernel obtains the first identification information and, according to the instruction in the first identification information, preferentially schedules resources for the second thread within a preset duration. Optionally, if the kernel sees that the first attribute information in the first identification information is true, it may preferentially schedule resources for the second thread within a preset duration.
由于内核优先为第二线程调度资源,使得第二线程可以频繁获得CPU的时间片,加快了第二线程运行的速度。CPU频繁为第二线程分配CPU时间片,会降低其他线程获取CPU时间片的概率,为了避免对其他线程的运行造成不良影响,内核优先为第二线程调度资源的时间不宜过长,本示例中,预设时长可以小于或等于20毫秒(如预设时长为5、毫秒、10毫秒、15毫秒、20毫秒等)。当内核检测到为第二线程优先调度资源的时长达到预设时长,则结束为第二线程优先调度资源的操作。Since the kernel prioritizes scheduling resources for the second thread, the second thread can frequently obtain CPU time slices, which speeds up the operation of the second thread. The CPU frequently allocates CPU time slices to the second thread, which reduces the probability of other threads obtaining CPU time slices. In order to avoid adverse effects on the operation of other threads, the time for the kernel to prioritize scheduling resources for the second thread should not be too long. In this example, the preset duration can be less than or equal to 20 milliseconds (such as the preset duration is 5 milliseconds, 10 milliseconds, 15 milliseconds, 20 milliseconds, etc.). When the kernel detects that the duration of the priority scheduling of resources for the second thread reaches the preset duration, the operation of prioritizing scheduling resources for the second thread ends.
步骤705:若第二线程在预设时长内未释放锁,则按照第二优先级运行。Step 705: If the second thread does not release the lock within the preset time, it runs according to the second priority.
具体地,第二线程若在预设时长内还未运行完,未释放锁,该内核结束对第二线程优先调度资源的操作。该第二线程由于优先级被更新为第二优先级,该第二线程按照第二优先级运行。该第二优先级高于第一优先级,第二优先级的第二线程将比第一优先级的第二线程优先获得CPU时间片,优先调度资源,从而缩短第二线程持锁的时长,缩短第一线程被阻塞的时长。Specifically, if the second thread has not finished running within the preset time and has not released the lock, the kernel ends the operation of preferentially scheduling resources for the second thread. Since the priority of the second thread is updated to the second priority, the second thread runs according to the second priority. The second priority is higher than the first priority, and the second thread of the second priority will obtain the CPU time slice and schedule resources preferentially before the second thread of the first priority, thereby shortening the time the second thread holds the lock and shortening the time the first thread is blocked.
可选地,在第二线程运行结束后,该第二线程释放锁。第一线程被第二线程唤醒,该第一线程再次竞争该锁,并加锁成功。该第一线程在持锁成功后,将第二线程的第二优先级更改为第一优先级。Optionally, after the second thread finishes running, the second thread releases the lock. The first thread is awakened by the second thread, and the first thread competes for the lock again and successfully locks. After successfully holding the lock, the first thread changes the second priority of the second thread to the first priority.
本示例中,该电子设备可以是手机、手环、平板电脑、电脑等设备。内核仅在预设时长内为第二线程优先分配资源,当第二线程在预设时长内未释放锁时,第二线程按照第二优先级运行,由于第二线程的第二优先级高于第一优先级,使得处理器可以按照第二优先级为第二线程分配资源,避免第二线程在超出预设时长后被其他高于第一优先级的线程抢占CPU,导致第二线程处于就绪状态,不能及时释放锁的问题。本示例中,结合修改第二线程的优先级的方式和由内核在预设时长内为第二线程分配资源的方式,两种方式相互互补,可以进一步缩短第二线程持锁的时间,减小第一线程被阻塞的时长,从而解决因第一线程被阻塞导致的加载数据慢、启动页面出现丢帧、卡顿等问题。In this example, the electronic device can be a mobile phone, a bracelet, a tablet computer, a computer and other devices. The kernel only preferentially allocates resources to the second thread within a preset time length. When the second thread does not release the lock within the preset time length, the second thread runs according to the second priority. Since the second priority of the second thread is higher than the first priority, the processor can allocate resources to the second thread according to the second priority, thereby avoiding the problem that the second thread is preempted by other threads with higher priority than the first priority after exceeding the preset time length, resulting in the second thread being in a ready state and unable to release the lock in time. In this example, the method of modifying the priority of the second thread and the method of allocating resources to the second thread by the kernel within a preset time length are combined. The two methods complement each other, which can further shorten the time the second thread holds the lock and reduce the time the first thread is blocked, thereby solving the problems of slow data loading, frame loss and freeze on the startup page caused by the blocking of the first thread.
图8为示例性示出的第一线程、第二线程以及内核之间的交互的示意图。下面结合图8具体说明本申请中应用程序运行的过程。Fig. 8 is a schematic diagram showing the interaction between the first thread, the second thread and the kernel. The process of running the application in the present application will be described in detail below with reference to Fig. 8 .
本示例中,第一线程以线程A为例,第二线程以线程C为例。In this example, thread A is taken as an example of the first thread, and thread C is taken as an example of the second thread.
当目标资源的锁处于未上锁状态时,若线程C对目标资源进行加锁操作,该目标资源的锁处于轻锁状态,当有其他线程(如线程D)访问该目标资源,线程D通过自旋进行等待。当线程D自旋的次数超过次数阈值,该目标资源的锁将从轻锁状态膨胀升级为重锁状态。或者,目标资源的锁将处于轻锁状态的情况下,当线程D在自旋且线程E访问该目标资源时,该目标资源的锁将从轻量状态升级为重锁状态。When the lock of the target resource is in an unlocked state, if thread C performs a locking operation on the target resource, the lock of the target resource is in a light lock state. When other threads (such as thread D) access the target resource, thread D waits by spinning. When the number of times thread D spins exceeds the number threshold, the lock of the target resource will expand and upgrade from a light lock state to a heavy lock state. Alternatively, when the lock of the target resource is in a light lock state, when thread D is spinning and thread E accesses the target resource, the lock of the target resource will be upgraded from a light lock state to a heavy lock state.
该目标资源的锁在T1时刻处于重锁状态,此时该线程C持有该Monitor对象,其中,Monitor对象包含有锁实例(即mutex实例,后文中也称为mutex)。The lock of the target resource is in a relocked state at time T1, at which time the thread C holds the Monitor object, wherein the Monitor object contains a lock instance (ie, a mutex instance, also referred to as mutex hereinafter).
步骤801:线程A对目标资源进行上锁操作。Step 801: Thread A performs a lock operation on the target resource.
具体地,在T1时刻线程C持有目标资源的锁,且该目标资源的锁处于重锁状态,该目标资源的Monitor对象被线程C持有。该线程A在T2时刻,对目标资源加锁,即执行MonitorEnter()方法。其中,T2时刻晚于T1时刻。线程A执行MonitorEnter()方法时,该线程A将尝试获取该Monitor对象。Specifically, at time T1, thread C holds the lock of the target resource, and the lock of the target resource is in a relocked state, and the Monitor object of the target resource is held by thread C. At time T2, thread A locks the target resource, that is, executes the MonitorEnter() method. Among them, time T2 is later than time T1. When thread A executes the MonitorEnter() method, thread A will try to obtain the Monitor object.
步骤802:线程A尝试上锁,且上锁失败。Step 802: Thread A attempts to lock, and fails to lock.
具体地,线程A可以通过CAS(Compare And Swap,比较与交换)算法尝试持有Monitor对象,若尝试失败,即确定线程A上锁失败。Specifically, thread A may attempt to hold the Monitor object through a CAS (Compare And Swap) algorithm. If the attempt fails, it is determined that thread A has failed to lock.
步骤803:线程A检测是否属于目标线程。Step 803: Thread A detects whether it belongs to the target thread.
具体地,线程A检测到该锁的状态处于重锁状态,可以检测该线程A是否属于目标线程。线程A首先检测该线程A所属进程是否处于前台运行状态。若线程A所属进程处于前台运行状态,该线程A可以获取该线程A所属进程的标识信息(即为PID),将该线程A所属进程的标识信息与该线程A的标识信息进行比较,若线程A检测到该线程A所属进程的标识信息(即PID)与线程A的标识信息(即TID)一致,则确定该线程A为该线程A所属进程的关键线程,即该线程A为目标线程。Specifically, thread A detects that the state of the lock is in a relocked state, and can detect whether thread A belongs to the target thread. Thread A first detects whether the process to which thread A belongs is in the foreground running state. If the process to which thread A belongs is in the foreground running state, thread A can obtain the identification information (i.e., PID) of the process to which thread A belongs, and compare the identification information of the process to which thread A belongs with the identification information of thread A. If thread A detects that the identification information (i.e., PID) of the process to which thread A belongs is consistent with the identification information of thread A (i.e., TID), it is determined that thread A is the key thread of the process to which thread A belongs, that is, thread A is the target thread.
在一个示例中,由于在应用程序中还存在有小程序,该线程A所属进程可以为小程序的进程。In one example, since there is also a small program in the application program, the process to which thread A belongs may be the process of the small program.
需要说明的是,在线程A检测到锁处于重锁状态,线程A可以通过该Monitor对像检测线程A是否处于目标线程。It should be noted that, when thread A detects that the lock is in a relocked state, thread A can detect whether thread A is in the target thread through the Monitor object.
线程A确定线程A属于目标线程,则执行步骤804。Thread A determines that thread A belongs to the target thread, and then executes step 804.
步骤804:若线程A检测到属于目标线程,则线程A将线程C的第一优先级更改为第二优先级。Step 804: If thread A detects that it belongs to the target thread, thread A changes the first priority of thread C to the second priority.
具体地,若线程A检测到该线程A属于目标线程,则线程A可以将线程C的第一优先级更改为第二优先级,该第二优先级高于第一优先级。可选地,该第二优先级可以为线程C所属优先等级范围内的最高优先级,例如,普通优先等级的范围为139~100,其中,139为最低优先级,100为最高优先级,线程C的优先等级属于普通优先等级,故线程A可以将线程C的优先等级更改为100;即最高的优先级。Specifically, if thread A detects that thread A belongs to the target thread, thread A can change the first priority of thread C to a second priority, which is higher than the first priority. Optionally, the second priority can be the highest priority within the priority range of thread C. For example, the range of ordinary priority is 139 to 100, where 139 is the lowest priority and 100 is the highest priority. The priority of thread C belongs to the ordinary priority, so thread A can change the priority of thread C to 100, which is the highest priority.
可选地,线程A还可以获取线程C的第一优先级,并根据线程C所属优先等级范围,确定第二优先级。例如,若线程A检测到第一优先级增加预设等级后处于所属优先级等级范围内,则线程A确定第二优先级为当前第一优先与预设等级之和(如预设等级为-20、-15、-30等),即第二优先级=第一优先级(139-20)。Optionally, thread A may also obtain the first priority of thread C, and determine the second priority according to the priority range of thread C. For example, if thread A detects that the first priority is within the priority range after increasing the preset level, thread A determines the second priority as the sum of the current first priority and the preset level (such as the preset level is -20, -15, -30, etc.), that is, the second priority = the first priority (139-20).
步骤805:线程A在进入阻塞状态之前,从锁中获取线程C的标识信息。Step 805: Before entering the blocking state, thread A obtains the identification information of thread C from the lock.
具体地,线程A在进入阻塞状态之前,可以从Monitor对象或mutex中获取到持锁到线程C的标识信息。该Monitor对象或mutex存储有指向持锁线程的指针,通过该指针可以获取到持锁线程(即线程C)的标识信息,以便于后续线程A更改持锁线程(即线程C)的优先级。Specifically, before entering the blocking state, thread A can obtain the identification information of thread C from the Monitor object or mutex. The Monitor object or mutex stores a pointer to the lock-holding thread, through which the identification information of the lock-holding thread (i.e., thread C) can be obtained, so that thread A can subsequently change the priority of the lock-holding thread (i.e., thread C).
本示例中以线程A从mutex中获取线程C的标识信息为例。In this example, thread A obtains the identification information of thread C from mutex.
步骤806:线程A将线程C的标识信息传递至内核。Step 806: Thread A passes the identification information of thread C to the kernel.
具体地,线程A将线程C的第一标识信息传递至内核。该第一标识信息可以包括线程的ID号,还可以包括用于指示内核在预设时长内优先为线程C调度资源的第一属性信息,如第一属性信息可以以“is_Vip”表示,当第一属性信息为真(true)时,表明该线程需要在预设时长内调度资源。Specifically, thread A transmits the first identification information of thread C to the kernel. The first identification information may include the ID number of the thread, and may also include first attribute information for indicating that the kernel should give priority to scheduling resources for thread C within a preset duration. For example, the first attribute information may be represented by "is_Vip". When the first attribute information is true, it indicates that the thread needs to schedule resources within the preset duration.
步骤807:内核为线程C优先调度资源。Step 807: The kernel schedules resources for thread C preferentially.
具体地,当内核获取到该第一标识信息中的线程ID号以及第一属性信息,可以启动计时器,在预设时长内优先为线程C调度资源。例如,内核接收到第一标识信息,识别到线程C的ID号以及第一属性信息为真,则启动计时器开始计时,其中,预设时长小于等于20毫秒如,预设时长为20毫秒、15毫秒、10毫秒等,本示例中预设时长以20毫秒为例进行说明。在20毫秒内,该内核可以为该线程C频繁分配资源,即线程C在20毫秒内可以频繁占用CPU。当内核检测到计时器达到20毫秒,则立即结束为线程C分配资源的操作。可选地,预设时长也可以通过定时器进行检测。Specifically, when the kernel obtains the thread ID number and the first attribute information in the first identification information, the timer can be started to prioritize scheduling resources for thread C within a preset duration. For example, the kernel receives the first identification information, recognizes that the ID number and the first attribute information of thread C are true, and then starts the timer to start timing, wherein the preset duration is less than or equal to 20 milliseconds, such as the preset duration is 20 milliseconds, 15 milliseconds, 10 milliseconds, etc. In this example, the preset duration is illustrated by taking 20 milliseconds as an example. Within 20 milliseconds, the kernel can frequently allocate resources to thread C, that is, thread C can frequently occupy the CPU within 20 milliseconds. When the kernel detects that the timer reaches 20 milliseconds, the operation of allocating resources to thread C is immediately terminated. Optionally, the preset duration can also be detected by a timer.
步骤808:在达到预设时长后,内核结束为线程C优先调度资源的操作。Step 808: After the preset duration is reached, the kernel ends the operation of prioritizing resource scheduling for thread C.
具体地,该内核可以在结束为线程C优先调度资源的操作后,删除第一标识信息。Specifically, the kernel may delete the first identification information after completing the operation of preferentially scheduling resources for thread C.
可选地,若线程C在预设时长内运行完毕并释放锁,该内核也可以结束为线程C优先调度资源的操作。Optionally, if thread C finishes running within a preset time and releases the lock, the kernel may also end the operation of prioritizing resource scheduling for thread C.
步骤809:线程C按照第二优先级运行。Step 809: Thread C runs at the second priority.
具体地,若线程C在预设时长内未释放锁,此时由于内核已结束为线程C优先调度资源的操作,线程C将按照修改后的第二优先级运行。由于线程C的第二优先级高于第一优先级,减小了线程C运行过程中被其他线程抢占CPU的概率,从而减小了线程A被阻塞时间长的问题。Specifically, if thread C does not release the lock within the preset time, thread C will run at the modified second priority because the kernel has finished scheduling resources for thread C. Since the second priority of thread C is higher than the first priority, the probability of thread C being preempted by other threads during its execution is reduced, thereby reducing the problem of thread A being blocked for a long time.
若第二优先级为最高优先级,则可以避免被其他线程抢占CPU的问题,缩短了线程C处于就绪状态的时长,进一步减小了线程C的持锁时长,减小了线程A被阻塞的时长。If the second priority is the highest priority, the problem of CPU being preempted by other threads can be avoided, the time that thread C is in the ready state is shortened, the lock holding time of thread C is further reduced, and the time that thread A is blocked is reduced.
步骤810:线程C释放锁。Step 810: Thread C releases the lock.
具体地,当线程C运行完毕,则可以释放锁,即该线程C释放管程对象。Specifically, when thread C finishes running, the lock can be released, that is, thread C releases the monitor object.
步骤811:线程A被唤醒。Step 811: Thread A is awakened.
具体地,由于线程C释放了Monitor对象,线程A被唤醒,执行步骤811。Specifically, because thread C releases the Monitor object, thread A is awakened and step 811 is executed.
步骤812:线程A持锁成功。Step 812: Thread A holds the lock successfully.
具体地,当线程C释放锁后,线程A再次尝试对目标资源上锁,由于此时目标资源的锁处于重锁状态且未被线程持有,线程A持锁成功。Specifically, after thread C releases the lock, thread A attempts to lock the target resource again. Since the lock of the target resource is in a relocked state and is not held by the thread at this time, thread A successfully holds the lock.
步骤813:线程A将线程C的第二优先级更新为第一优先级。Step 813: Thread A updates the second priority of thread C to the first priority.
具体地,当线程C释放锁之后,线程A持锁成功,结束阻塞。该线程A可以将线程C的第二优先级更新为第一优先级。将线程C的优先级更改为第一优先级,避免线程C在高优先级状态下对其他线程造成不必要的影响。Specifically, after thread C releases the lock, thread A successfully holds the lock and ends the blocking. Thread A can update the second priority of thread C to the first priority. Changing the priority of thread C to the first priority avoids unnecessary impact of thread C on other threads in a high priority state.
图9为示例性示出的采用本申请中应用程序运行的方法的线程调用CPU的示意图。FIG. 9 is a schematic diagram exemplarily showing a thread calling a CPU using the method for running an application program in the present application.
如图9所示,线程A为应用程序A的启动进程中主线程。线程A的优先级高与线程C的优先级,线程A的优先级高于线程B的优先级,线程B的优先级高于线程C的优先级。线程C在T0时刻对共享资源加了锁,且该锁的状态为重锁状态。线程A在T1时刻对该共享资源加锁,由于目标资源的锁已经被线程C持有,该线程A竞争锁失败。线程A如图9中虚线箭头所示,将线程C的第一优先级(如为139)修改为第二优先级,第二优先级为线程C所属优先级范围内的最高优先级(如100)。该线程A获取线程C的第一标识信息,该第一标识信息包括线程C的身份ID以及第一属性信息,该第一属性信息指示为真。线程A将线程C的第一标识信息传输至内核,内核获取第一标识信息,启动计时器,内核为线程C优先调度资源。内核检测到及时器达到预设时长(如T1~T2),则结束为线程C优先调度资源的操作。线程C在预设时长内未释放锁,线程C将按照优先级100运行。由于线程C的优先级为100,线程B的优先级为120,线程C的优先级高于线程B的优先级,线程C将优先调度资源,即线程C优先分配CPU时间片,线程B处于就绪状态。线程C在T3时刻释放锁,线程A对目标资源加锁,线程A对目标资源加锁成功。线程A可以将线程C的优先级修改为第一优先级(即139)。此时,线程B的优先级为120,可以获得CPU时间片。由于线程C的运行时间缩短,使得线程A阻塞的时间变短,从而解决了线程A因阻塞时间过长导致启动页面出现丢帧、卡顿的问题,也可以解决因线程A阻塞时间长导致显示扫码结果的时间长的问题。As shown in FIG9 , thread A is the main thread in the startup process of application A. The priority of thread A is higher than that of thread C. The priority of thread A is higher than that of thread B, and the priority of thread B is higher than that of thread C. Thread C locks the shared resource at time T0, and the lock is in a relocked state. Thread A locks the shared resource at time T1. Since the lock of the target resource is already held by thread C, thread A fails to compete for the lock. As shown by the dotted arrow in FIG9 , thread A modifies the first priority of thread C (such as 139) to the second priority, and the second priority is the highest priority (such as 100) within the priority range to which thread C belongs. Thread A obtains the first identification information of thread C, which includes the identity ID of thread C and the first attribute information, and the first attribute information indicates true. Thread A transmits the first identification information of thread C to the kernel, and the kernel obtains the first identification information, starts the timer, and the kernel preferentially schedules resources for thread C. When the kernel detects that the timer reaches the preset duration (such as T1 to T2), the operation of preferentially scheduling resources for thread C ends. Thread C does not release the lock within the preset time, and thread C will run at priority 100. Since the priority of thread C is 100 and the priority of thread B is 120, the priority of thread C is higher than that of thread B. Thread C will prioritize resource scheduling, that is, thread C will prioritize CPU time slices, and thread B is in the ready state. Thread C releases the lock at time T3, and thread A locks the target resource, and thread A successfully locks the target resource. Thread A can modify the priority of thread C to the first priority (that is, 139). At this time, the priority of thread B is 120, and it can obtain the CPU time slice. Since the running time of thread C is shortened, the blocking time of thread A is shortened, thereby solving the problem of frame loss and jamming on the startup page due to long blocking time of thread A, and also solving the problem of long time to display the scan result due to long blocking time of thread A.
在一些实施例中,当应用程序中的第一线程对目标资源加锁失败且检测到第一线程为目标线程时,可以通过增加当前持锁线程的优先级,使得持锁线程可以优先其他线程抢占CPU,减少持锁线程的运行时长,进而缩短第一线程被阻塞的时长,解决应用程序出现丢帧、卡顿的问题。In some embodiments, when the first thread in the application fails to lock the target resource and it is detected that the first thread is the target thread, the priority of the current lock-holding thread can be increased so that the lock-holding thread can preempt other threads from seizing the CPU, thereby reducing the running time of the lock-holding thread and thereby shortening the time the first thread is blocked, thereby solving the problem of frame loss and freeze in the application.
图10为示例性示出的一种应用程序运行的流程图,其过程包括:FIG10 is a flowchart showing an exemplary operation of an application program, wherein the process includes:
步骤1001:第二线程对目标资源加锁成功。Step 1001: The second thread successfully locks the target resource.
本示例中,该第二线程对目标资源加锁操作成功,当该目标资源的锁的状态为重锁状态时,该第二线程持有该目标资源的锁。In this example, the second thread successfully locks the target resource. When the lock state of the target resource is in a relocked state, the second thread holds the lock of the target resource.
步骤1002:第一线程尝试对目标资源加锁。Step 1002: The first thread attempts to lock the target resource.
具体地,第一线程的优先级高于第二线程的优先级。该第一线程可以对目标资源尝试加锁。若目标资源的锁状态处于重锁状态,则可以检测该重锁是否被持有线程释放,即执行步骤1003。Specifically, the priority of the first thread is higher than the priority of the second thread. The first thread can attempt to lock the target resource. If the lock state of the target resource is in a re-locked state, it can be detected whether the re-lock is released by the holding thread, that is, step 1003 is executed.
步骤1003:第一线程检测该目标资源的锁是否被其他线程持有。Step 1003: The first thread detects whether the lock of the target resource is held by other threads.
具体地,第一线程可以通过尝试上锁的方式检测该目标资源的锁是否被释放,若尝试上锁失败,则可以确定该目标资源的锁被其他线程持有。Specifically, the first thread may detect whether the lock of the target resource is released by attempting to lock. If the locking attempt fails, it may be determined that the lock of the target resource is held by other threads.
步骤1004:若第一线程检测到目标资源的锁被其他线程持有,则第一线程检测是否属于目标线程。Step 1004: If the first thread detects that the lock of the target resource is held by other threads, the first thread detects whether it belongs to the target thread.
该过程与步骤803类似,可以参照步骤803中的描述,此处不再进行赘述。This process is similar to step 803, and reference may be made to the description in step 803, which will not be repeated here.
步骤1005:若第一线程检测到第一线程属于目标线程,则第一线程将第二线程的第一优先级更新为第二优先级。Step 1005: If the first thread detects that the first thread belongs to the target thread, the first thread updates the first priority of the second thread to the second priority.
该过程与步骤804类似,可以参照步骤804中的描述,此处不再进行赘述。第二线程的优先级为第二优先级后,该第二线程按照第二优先级运行,即执行步骤1007。This process is similar to step 804 , and the description in step 804 may be referred to, and will not be repeated here. After the priority of the second thread is the second priority, the second thread runs according to the second priority, that is, step 1007 is executed.
步骤1006:第一线程处于阻塞状态,等待第二线程释放目标资源的锁。Step 1006: The first thread is in a blocked state, waiting for the second thread to release the lock of the target resource.
具体地,第一线程将第二线程的优先级更改为第二优先级后,该第一线程进入阻塞状态。该第一线程等待第二线程释放目标资源的锁。Specifically, after the first thread changes the priority of the second thread to the second priority, the first thread enters a blocked state and waits for the second thread to release the lock of the target resource.
步骤1007:第二线程按照第二优先级运行,由处理器按照第二优先级为第二线程调度资源。Step 1007: The second thread runs according to the second priority, and the processor schedules resources for the second thread according to the second priority.
该过程与步骤809类似,可以参照步骤809中的描述,此处不再进行赘述。This process is similar to step 809, and you can refer to the description in step 809, which will not be repeated here.
步骤1008:第二线程释放锁。Step 1008: The second thread releases the lock.
具体地,第二线程按照第二优先级运行完毕,即可释放目标资源的锁。Specifically, after the second thread finishes running according to the second priority, the lock of the target resource can be released.
步骤1009:第一线程对目标资源加锁成功,将第二线程的第二优先级更新为第一优先级。Step 1009: The first thread successfully locks the target resource, and the second priority of the second thread is updated to the first priority.
当第二线程释放锁之后,该第一线程加锁成功,该第一线程恢复运行。该第一线程可以将第二线程的第二优先级更新为第一优先级。After the second thread releases the lock, the first thread successfully locks and the first thread resumes running. The first thread can update the second priority of the second thread to the first priority.
图11为示例性示出的第一线程、第二线程之间的交互的示意图。下面结合图11具体说明本申请中应用程序运行的过程。Fig. 11 is a schematic diagram showing the interaction between the first thread and the second thread. The following specifically describes the process of running the application in the present application in conjunction with Fig. 11.
步骤1101:线程A对目标资源进行上锁操作。Step 1101: Thread A performs a lock operation on the target resource.
步骤1102:线程A尝试上锁,且上锁失败。Step 1102: Thread A attempts to lock, and fails to lock.
步骤1103:线程A检测是否属于目标线程。Step 1103: Thread A detects whether it belongs to the target thread.
步骤1104:若线程A检测到属于目标线程,则线程A将线程C的第一优先级更改为第二优先级。Step 1104: If thread A detects that it belongs to the target thread, thread A changes the first priority of thread C to the second priority.
步骤1101~步骤1104与步骤801~步骤804类似,可以参照步骤801~步骤804中的相关描述,此处不再进行赘述。Steps 1101 to 1104 are similar to steps 801 to 804 , and reference may be made to the relevant descriptions in steps 801 to 804 , which will not be repeated here.
步骤1105:线程C按照第二优先级运行。Step 1105: Thread C runs at the second priority.
具体地,由于线程C的第二优先级高于第一优先级,减小了线程C运行过程中被其他线程抢占CPU的概率,从而减小了线程A被阻塞时间长的问题。Specifically, since the second priority of thread C is higher than the first priority, the probability of CPU being preempted by other threads during the running of thread C is reduced, thereby reducing the problem of thread A being blocked for a long time.
若第二优先级为最高优先级,则可以避免被其他线程抢占CPU的问题,缩短了线程C处于就绪状态的时长,进一步减小了线程C运行时长,减小了线程A被阻塞的时长。If the second priority is the highest priority, the problem of CPU being preempted by other threads can be avoided, the time that thread C is in the ready state is shortened, the running time of thread C is further reduced, and the time that thread A is blocked is reduced.
步骤1106:线程C释放锁。Step 1106: Thread C releases the lock.
步骤1107:线程A被唤醒。Step 1107: Thread A is awakened.
步骤1108:线程A持锁成功。Step 1108: Thread A holds the lock successfully.
步骤1109:线程A将线程C的第二优先级更新为第一优先级。Step 1109: Thread A updates the second priority of thread C to the first priority.
步骤1106~步骤1109与步骤810~步骤813类似,可以参照步骤810~步骤813中的相关描述,此处不再进行赘述。Steps 1106 to 1109 are similar to steps 810 to 813 , and reference may be made to the relevant descriptions in steps 810 to 813 , which will not be repeated here.
可以理解的是,电子设备为了实现上述功能,其包含了执行各个功能相应的硬件和/或软件模块。结合本文中所公开的实施例描述的各示例的算法步骤,本申请能够以硬件或硬件和计算机软件的结合形式来实现。某个功能究竟以硬件还是计算机软件驱动硬件的方式来执行,取决于技术方案的特定应用和设计约束条件。本领域技术人员可以结合实施例对每个特定的应用来使用不同方法来实现所描述的功能,但是这种实现不应认为超出本申请的范围。It is understandable that, in order to realize the above functions, the electronic device includes hardware and/or software modules corresponding to the execution of each function. In combination with the algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application in combination with the embodiments, but such implementation should not be considered to be beyond the scope of the present application.
本实施例还提供一种计算机存储介质,该计算机存储介质中存储有计算机指令,当该计算机指令在电子设备上运行时,使得电子设备执行上述相关方法步骤实现上述实施例中的应用程序运行的方法。存储介质包括:U盘、移动硬盘、只读存储器(read onlymemory,ROM)、随机存取存储器(random access memory,RAM)、磁碟或者光盘等各种可以存储程序代码的介质。This embodiment also provides a computer storage medium, in which computer instructions are stored. When the computer instructions are executed on an electronic device, the electronic device executes the above-mentioned related method steps to implement the method for running the application program in the above-mentioned embodiment. The storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and other media that can store program codes.
本实施例还提供了一种计算机程序产品,当该计算机程序产品在计算机上运行时,使得计算机执行上述相关步骤,以实现上述实施例中的应用程序运行的方法。This embodiment also provides a computer program product. When the computer program product is run on a computer, the computer is caused to execute the above-mentioned related steps to implement the method for running the application program in the above-mentioned embodiment.
其中,本实施例提供的电子设备、计算机存储介质、计算机程序产品或芯片均用于执行上文所提供的对应的方法,因此,其所能达到的有益效果可参考上文所提供的对应的方法中的有益效果,此处不再赘述。Among them, the electronic device, computer storage medium, computer program product or chip provided in this embodiment is used to execute the corresponding method provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding method provided above and will not be repeated here.
本文中术语“和/或”,仅仅是一种描述关联对象的关联关系,表示可以存在三种关系,例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B这三种情况。The term "and/or" in this article is merely a description of the association relationship of associated objects, indicating that three relationships may exist. For example, A and/or B can mean: A exists alone, A and B exist at the same time, and B exists alone.
本申请实施例的说明书和权利要求书中的术语“第一”和“第二”等是用于区别不同的对象,而不是用于描述对象的特定顺序。例如,第一目标对象和第二目标对象等是用于区别不同的目标对象,而不是用于描述目标对象的特定顺序。The terms "first" and "second" in the description and claims of the embodiments of the present application are used to distinguish different objects rather than to describe a specific order of objects. For example, a first target object and a second target object are used to distinguish different target objects rather than to describe a specific order of target objects.
在本申请实施例中,“示例性的”或者“例如”等词用于表示作例子、例证或说明。本申请实施例中被描述为“示例性的”或者“例如”的任何实施例或设计方案不应被解释为比其它实施例或设计方案更优选或更具优势。确切而言,使用“示例性的”或者“例如”等词旨在以具体方式呈现相关概念。In the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.
在本申请实施例的描述中,除非另有说明,“多个”的含义是指两个或两个以上。例如,多个处理单元是指两个或两个以上的处理单元;多个系统是指两个或两个以上的系统。In the description of the embodiments of the present application, unless otherwise specified, the meaning of "multiple" refers to two or more than two. For example, multiple processing units refer to two or more processing units; multiple systems refer to two or more systems.
本申请各个实施例的任意内容,以及同一实施例的任意内容,均可以自由组合。对上述内容的任意组合均在本申请的范围之内。Any content of each embodiment of the present application, as well as any content of the same embodiment, can be freely combined. Any combination of the above content is within the scope of the present application.
上面结合附图对本申请的实施例进行了描述,但是本申请并不局限于上述的具体实施方式,上述的具体实施方式仅仅是示意性的,而不是限制性的,本领域的普通技术人员在本申请的启示下,在不脱离本申请宗旨和权利要求所保护的范围情况下,还可做出很多形式,均属于本申请的保护之内。The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.
Claims (11)
Priority Applications (2)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202211403614.5A CN116700817B (en) | 2022-11-10 | 2022-11-10 | Application program running method and electronic equipment |
CN202410613001.7A CN118567744A (en) | 2022-11-10 | 2022-11-10 | Application program running method and electronic equipment |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202211403614.5A CN116700817B (en) | 2022-11-10 | 2022-11-10 | Application program running method and electronic equipment |
Related Child Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202410613001.7A Division CN118567744A (en) | 2022-11-10 | 2022-11-10 | Application program running method and electronic equipment |
Publications (2)
Publication Number | Publication Date |
---|---|
CN116700817A CN116700817A (en) | 2023-09-05 |
CN116700817B true CN116700817B (en) | 2024-05-31 |
Family
ID=87834484
Family Applications (2)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202211403614.5A Active CN116700817B (en) | 2022-11-10 | 2022-11-10 | Application program running method and electronic equipment |
CN202410613001.7A Pending CN118567744A (en) | 2022-11-10 | 2022-11-10 | Application program running method and electronic equipment |
Family Applications After (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202410613001.7A Pending CN118567744A (en) | 2022-11-10 | 2022-11-10 | Application program running method and electronic equipment |
Country Status (1)
Country | Link |
---|---|
CN (2) | CN116700817B (en) |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN107133092A (en) * | 2017-05-24 | 2017-09-05 | 努比亚技术有限公司 | Multi-thread synchronization processing method, terminal and computer-readable recording medium |
CN111552574A (en) * | 2019-09-25 | 2020-08-18 | 华为技术有限公司 | Multithreading synchronization method and electronic equipment |
CN114461353A (en) * | 2020-11-09 | 2022-05-10 | 中兴通讯股份有限公司 | Method, terminal and computer readable storage medium for adjusting thread priority |
Family Cites Families (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US7975271B2 (en) * | 2005-03-30 | 2011-07-05 | Hewlett-Packard Development Company, L.P. | System and method for dynamically determining a portion of a resource for which a thread is to obtain a lock |
-
2022
- 2022-11-10 CN CN202211403614.5A patent/CN116700817B/en active Active
- 2022-11-10 CN CN202410613001.7A patent/CN118567744A/en active Pending
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN107133092A (en) * | 2017-05-24 | 2017-09-05 | 努比亚技术有限公司 | Multi-thread synchronization processing method, terminal and computer-readable recording medium |
CN111552574A (en) * | 2019-09-25 | 2020-08-18 | 华为技术有限公司 | Multithreading synchronization method and electronic equipment |
CN114461353A (en) * | 2020-11-09 | 2022-05-10 | 中兴通讯股份有限公司 | Method, terminal and computer readable storage medium for adjusting thread priority |
Also Published As
Publication number | Publication date |
---|---|
CN116700817A (en) | 2023-09-05 |
CN118567744A (en) | 2024-08-30 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US9424085B2 (en) | Recognizing a process group that is related to an application | |
CN111831441A (en) | Memory recovery method, device, storage medium and electronic device | |
CN112860145B (en) | Application control method and electronic equipment | |
CN114020652B (en) | Application program management method and electronic equipment | |
US20130298143A1 (en) | Wait on address synchronization interface | |
CN111831440A (en) | Memory recovery method, device, storage medium and electronic device | |
CN111831436B (en) | IO request scheduling method and device, storage medium and electronic equipment | |
CN111813520A (en) | Thread scheduling method, device, storage medium and electronic device | |
WO2018129269A1 (en) | Execution of multiple applications on a device | |
CN113709026B (en) | Method, device, storage medium and program product for processing instant communication message | |
CN116700818B (en) | Application program running method and electronic equipment | |
CN116700817B (en) | Application program running method and electronic equipment | |
CN118444995B (en) | Application startup method and electronic device | |
CN111831432B (en) | IO request scheduling method, device, storage medium and electronic equipment | |
CN111459462B (en) | Decentralized relock downgrade | |
CN116400938B (en) | Operating system upgrading method, device and storage medium | |
CN115017487B (en) | Switching method for login account of electronic equipment and electronic equipment | |
CN115421662A (en) | Memory data write-back method, device and equipment | |
CN117724634B (en) | Process management method and device for application program | |
CN117094876B (en) | Data processing method, electronic device and readable storage medium | |
CN111831444B (en) | Memory recycling method and device, storage medium and electronic equipment | |
CN115543612A (en) | Thread control method and device, electronic equipment and storage medium | |
WO2024193358A1 (en) | Memory reclaim method and electronic device | |
CN120255985A (en) | Unlocking dynamic effect display method and electronic equipment | |
WO2025007569A1 (en) | System resource scheduling method, electronic device, and storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant | ||
CP03 | Change of name, title or address | ||
CP03 | Change of name, title or address |
Address after: Unit 3401, unit a, building 6, Shenye Zhongcheng, No. 8089, Hongli West Road, Donghai community, Xiangmihu street, Futian District, Shenzhen, Guangdong 518040 Patentee after: Honor Terminal Co.,Ltd. Country or region after: China Address before: 3401, unit a, building 6, Shenye Zhongcheng, No. 8089, Hongli West Road, Donghai community, Xiangmihu street, Futian District, Shenzhen, Guangdong Patentee before: Honor Device Co.,Ltd. Country or region before: China |