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
- /**
- * The default priority, which is assigned to a thread.
- */
- 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
- Thread parent = CurrentThread ();
- 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
- Public static void Main (string[] args) {
- System.out.println (Thread.CurrentThread (). getpriority ());
- }
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
- if (newpriority > max_priority | | newpriority < min_priority) {
- throw new illegalargumentexception ();
- }
- if ((G = getthreadgroup ()) != < span class= "keyword" >null) {
- if (newpriority > g.getmaxpriority ()) {
- newpriority = g.getmaxpriority ();
- }
- setpriority0 (priority = newpriority);
- }
- }
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