Priority of the thread

Source: Internet
Author: User

Java threads can have priority settings, and high-priority threads have a higher chance of being executed than low-priority threads (not exactly, refer to "thread-priority issues" below).

The value of the priority level

The priority of a Java thread is an integer whose value range is 1 (thread.min_priority)-Ten (thread.max_priority).

The comment for norm_priority (value 5) in the thread source code is "thread default priority"

Java code
    1. /**
    2. * The default priority, which is assigned to a thread.
    3. */
    4. public final static int norm_priority = 5;

actually otherwise The default priority is the priority of the parent thread. In the Init method,

Java code
    1. Thread parent = CurrentThread ();
    2. This.priority = Parent.getpriority ();

Perhaps this is explained because the main thread of the Java program (Main method) defaults to norm_priority, so that the priority of the subsequent created thread is norm_priority.

Java code
    1. Public static void Main (string[] args) {
    2. System.out.println (Thread.CurrentThread (). getpriority ());
    3. }

Its execution result is 5.

Setting the priority level

The priority can be changed by the SetPriority method (final, not quilt class overload). Priority cannot exceed 1-10 of the range of values, or throw illegalargumentexception. In addition, if the thread already belongs to a thread group (threadgroup), the thread's priority cannot exceed the priority of that thread Group:

Java code
    1. if  (newpriority > max_priority | |  newpriority < min_priority)  {  
    2.     throw new illegalargumentexception ();   
    3. }  
    4. if ((G = getthreadgroup ())  != < span class= "keyword" >null)  {  
    5.      if  (newpriority > g.getmaxpriority ())  {  
    6.     newpriority = g.getmaxpriority ();   
    7.     }  
    8.     setpriority0 (priority =  newpriority);   
    9.        }  
    10.     }  

Where SetPriority0 is a local method.

Maximum priority for thread groups

We can set the maximum priority of a thread group, and the thread's priority cannot exceed this number when creating a thread that belongs to that thread group.

Set the maximum priority of the thread group:

    • The maximum priority of the system thread group defaults to Thread.max_priority
    • When you create a thread group, its maximum priority defaults to the parent thread group (if the parent thread group is not specified, its parent line Cheng the thread group that the current thread belongs to).
    • The maximum priority can be changed through setmaxpriority, but cannot exceed the maximum priority of the parent thread Group

Setmaxpriority's question:

    • This method can only change the maximum priority of this thread group and its child thread groups (recursion).
    • However, you cannot affect the priority of a thread that has been created directly or indirectly belonging to that thread group, that is, even if there is currently a child thread that has a higher priority than the newly set thread group, it does not change the priority of that child thread. The maximum priority of a thread group only works if you attempt to change the priority of a child thread or create a new child thread.
Thread priority issues The following excerpt, translated from Javamex, Java threading Introduction, Thread Priorioties for thread precedence, we need to be aware of:

* Thread.setpriority () may not do anything at all, it's about your operating system and the version of the virtual machine
* Thread prioritization may have different meanings for different thread schedulers and may not be your intuitive guess. In particular, the priority does not necessarily refer to CPU sharing. In UNIX systems, the priority can more or less be considered CPU allocated, but Windows is not
* Thread priority is usually a combination of global and local priority settings. The Java SetPriority () method applies only to local precedence. In other words, you cannot prioritize within the entire range of possibilities. (This is usually a form of protection, and you probably don't want the mouse pointer or thread that handles the audio data to be preempted by other random user threads)
* Different systems have different thread-priority values, but Java defines 10 levels (1-10). This makes it possible for several threads to have different priorities in one operating system, with the same priority in another operating system (and therefore potentially unexpected behavior)
* The operating system may (and usually does) add some proprietary behavior to the thread based on the priority of the thread (for example, "only give a quantum boost if the first is below X"). Again, there are some differences in the definition of priorities between different systems.
* Most operating system thread schedulers actually perform temporary operations on the priority of a thread at a strategic level (for example, when a thread receives an event or I/O that it waits for), usually the operating system knows the most, and attempting to manually control the priority may only interfere with the system.
* Your application often does not know which threads are running on other processes, so the impact of changing the priority of a thread is unpredictable for the entire system. For example, you may find that you have a low-priority thread that is expected to run occasionally in the background, because a virus monitor runs on a slightly higher priority (but still less than the normal priority) and cannot predict the performance of your program, It will vary depending on the anti-virus program your customers use.

You can refer to the corresponding relationship between the Java priority and the operating system priority level

Actual coding considerations
      • Do not assume that a high-priority thread must be executed before a low-priority thread, and that there is no logical dependency on the thread priority, which may produce unexpected results

Thread Priority

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.