Thread priority-related properties for threads
Each thread has a priority, and in thread, the attributes corresponding to the priority are as follows:
/** * 线程的优先级属性 */private int priority;/** * 线程所能拥有的最大优先级. */public final static int MIN_PRIORITY = 1;/** * 线程默认的优先级. */public final static int NORM_PRIORITY = 5;/** * 线程所能拥有的最大优先级. */public final static int MAX_PRIORITY = 10;
Related functions
Only in the Thread class is discussed here.
Priority class Initialization
private void init(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc) { // .... this.priority = parent.getPriority(); setPriority(priority); // ....}
The init () function initializes the precedence. and call the SetPriority (priority) function to set it. It is learned that the priority of a thread is inherited from the thread that created it.
Setting the priority level
In Init (), in addition to assigning a value to This.priority, the setpriority (priority) function is called because a native method is also called inside the function.
public final void setPriority(int newPriority) { ThreadGroup g; checkAccess(); // 不能大于最大优先级 MAX_PRIORITY if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) { throw new IllegalArgumentException(); } if((g = getThreadGroup()) != null) { // 如比所属线程组的最大优先级还大, 则取线程组的最大优先级 if (newPriority > g.getMaxPriority()) { newPriority = g.getMaxPriority(); } setPriority0(priority = newPriority); }}
The code indicates that the priority of the set cannot be greater than the maximum priority, nor is it greater than the highest priority of the thread group
Get priority level
Getting the current priority, in fact, is the value that returns the Precedence property.
public final int getPriority() { return priority;}
From the above, the thread priority has the following characteristics in thread:
- Java threads are prioritized from 1~10;
- Java thread default priority is 5;
- The Java thread's priority inherits from the thread that created it.
Does it feel like 2 and 3 are contradictory? You can find the answer in the following code and results.
Default priority
First the code to feel the role of thread priority:
public class Threadprioritytest {class Primerun implements Runnable {public void run () { System.out.println (Thread.CurrentThread (). GetName () + "::" + thread.currentthread (). getpriority ()); System.out.println (Thread.CurrentThread (). GetName () + "Run begin"); for (int i = 0; i < i++) {System.out.println (Thread.CurrentThread (). GetName () + "::" +i); } System.out.println (Thread.CurrentThread (). GetName () + "Run End"); }} public void Test () {System.out.println (Thread.CurrentThread (). GetName () + "Begin"); Thread P4 = new Thread (new Primerun ()); Thread P6 = new Thread (new Primerun ()); P4.setname ("P4"); P6.setname ("P6"); P4.start (); P6.start (); System.out.println (Thread.CurrentThread (). GetName () + "End"); public static void Main (string[] args) {new threadprioritytest (). Test (); }}
In the Run () method of the Primerun class, the output is only 0 to 10, preceded by the thread name to identify. Without setting the thread, the init () method, as described above, shows that the priority is inherited from the priority in Threadprioritytest, and no priority is set to default to 5. The output is as follows:
You can see that the thread priority for both P4 and P6 is 5, the output is unordered, the P4 and P6 cross out, so the results are different each time.
Specify the priority level
Change the priority in the thread, and the test () function becomes the following:
public void test(){ System.out.println(Thread.currentThread().getName()+" begin"); Thread p4 = new Thread(new PrimeRun()); Thread p6 = new Thread(new PrimeRun()); p4.setName("P4"); p4.setPriority(4); p6.setName("P6"); p6.setPriority(6); p4.start(); p6.start(); System.out.println(Thread.currentThread().getName()+" end");}
The output after the run is as follows:
As you can see, P6 runs out before P4.
Precautions
But (general "but" the following things should be noted)
The priority is related to the operating system and the virtual machine version.
The + + priority is just a representation of how important the thread scheduler is to the thread dispatcher. If a large number of threads are blocked, they are waiting to be shipped
Line, the debugger first runs the thread that has the highest priority. However, this does not mean that a lower priority thread will not run (in other words, it will not cause a deadlock due to the presence of a priority). If the thread has a lower priority, it simply means that it is allowed to run at a smaller opportunity. ++
Therefore, in the actual coding, it is considered that the high priority must precede the low priority thread execution, and finally the problem.
Precedence inheritance
As for the difference between features 2 and 3, when we first Test (), the priority of P4 and P6 is 5, we change the function:
public void test(){ Thread.currentThread().setPriority(10); System.out.println(Thread.currentThread().getName()+" begin"); Thread p4 = new Thread(new PrimeRun()); Thread p6 = new Thread(new PrimeRun()); p4.setName("P4"); p6.setName("P6"); p4.start(); p6.start(); System.out.println(Thread.currentThread().getName()+" end");}
Set the priority of the current thread to 10 before creating P4 and P6, and remove some extraneous output from run (), and enter it as follows: