To set thread priority in Java using setpriority, the source code in the JDK is as follows:
Public Final voidSetPriority (intnewpriority) {Threadgroup g; CheckAccess (); if(Newpriority > Max_priority | | newpriority <min_priority) { Throw Newillegalargumentexception (); } if((g = Getthreadgroup ())! =NULL) { if(Newpriority >g.getmaxpriority ()) {newpriority=g.getmaxpriority (); } setPriority0 ( priority=newpriority); } }
In Java, the priority of a thread is divided into 10 levels of 1~10, less than 1, or greater than 10, which throws a IllegalArgumentException exception
Use 3 constants in the JDK to predefine the priority level:
/*** The minimum priority, a thread can have. */ Public Final Static intmin_priority = 1; /*** The default priority, which is assigned to a thread. */ Public Final Static intNorm_priority = 5; /*** The maximum priority, a thread can have. */ Public Final Static intMax_priority = 10;
The inheritance of thread precedence
In Java, the priority of a thread is inherited, for example a thread initiates a B thread, and the precedence of A and B is the same. As an example:
Public classMyThread1extendsThread {@Override Public voidrun () {System.out.println ("MyThread1 Run priority=" + This. getpriority ()); MyThread2 thread2=NewMyThread2 (); Thread2.start (); }} Public classMyThread2extendsThread {@Override Public voidrun () {System.out.println ("MyThread2 Run priority=" + This. getpriority ()); }} Public classRun { Public Static voidMain (string[] args)throwsinterruptedexception {System.out.println ("Main thread begin priority=" +Thread.CurrentThread (). getpriority ()); System.out.println ("Main thread End priority=" +Thread.CurrentThread (). getpriority ()); MyThread1 Thread1=NewMyThread1 (); Thread1.start (); }}
The results of the operation are as follows:
Main thread begin Priority=5
Main thread End Priority=5
MyThread1 Run priority=5
MyThread2 Run priority=5
Precedence is rule-
Although the SetPriority () method can set the priority of a thread, it does not see the effect of setting the priority level
Examples are as follows:
Public classMyThread1extendsThread {@Override Public voidrun () {LongBeginTime =System.currenttimemillis (); LongAddresult = 0; for(inti = 0; I < 10; i++) { for(intj = 0; J < 50000; J + +) {Random random=NewRandom (); Random.nextint (); Addresult+=J; } } LongEndTime =System.currenttimemillis (); System.out.println ("* * * * * thread 1 Use time=" + (EndTime-beginTime)); }} Public classMyThread2extendsThread {@Override Public voidrun () {LongBeginTime =System.currenttimemillis (); LongAddresult = 0; for(inti = 0; I < 10; i++) { for(intj = 0; J < 50000; J + +) {Random random=NewRandom (); Random.nextint (); Addresult+=J; } } LongEndTime =System.currenttimemillis (); System.out.println ("* * * * * thread 2 Use time=" + (EndTime-beginTime)); }} Public classRun { Public Static voidMain (string[] args)throwsinterruptedexception { for(inti=0;i<5;i++) {MyThread1 thread1=NewMyThread1 (); Thread1.setpriority (10); Thread1.start (); MyThread2 thread2=NewMyThread2 (); Thread2.setpriority (1); Thread2.start (); } }}
The results of the operation are as follows:
* * * * * thread 1 Use time=174
* * * * * thread 1 Use time=221
* * * * * thread 1 Use time=224
* * * * * Thread 2 use time=360
* * * * * thread 1 Use time=202
* * * * * Thread 2 use time=185
* * * * * thread 1 Use time=169
* * * * * Thread 2 use time=466
* * * * * Thread 2 use time=425
* * * * * Thread 2 use time=98
As can be seen from the above results, high priority threads are always executed mostly first, but not all of them are executed first. The first execution is not the first call, if the priority is changed, the first execution is not related to the order of the Code.
Priority has a certain degree of discipline, the CPU always as far as possible to the execution of resources to high-priority threads
Precedence has randomness
A higher priority thread does not have to be executed every time, for example:
Public classMyThread1extendsThread {@Override Public voidrun () {LongBeginTime =System.currenttimemillis (); for(inti = 0; i < 1000; i++) {Random random=NewRandom (); Random.nextint (); } LongEndTime =System.currenttimemillis (); System.out.println ("* * * * * thread 1 Use time=" + (EndTime-beginTime)); }} Public classMyThread2extendsThread {@Override Public voidrun () {LongBeginTime =System.currenttimemillis (); for(inti = 0; i < 1000; i++) {Random random=NewRandom (); Random.nextint (); } LongEndTime =System.currenttimemillis (); System.out.println ("* * * * * thread 2 Use time=" + (EndTime-beginTime)); }} Public classRun { Public Static voidMain (string[] args)throwsinterruptedexception { for(inti = 0; I < 5; i++) {MyThread1 thread1=NewMyThread1 (); Thread1.setpriority (5); Thread1.start (); MyThread2 thread2=NewMyThread2 (); Thread2.setpriority (6); Thread2.start (); } }}
The results of the operation are as follows:
* * * * * thread 1 Use time=5
* * * * * Thread 2 use time=4
* * * * * thread 1 Use time=5
* * * * * thread 1 Use time=4
* * * * * thread 1 Use time=4
* * * * * Thread 2 use time=6
* * * * * Thread 2 use time=3
* * * * * Thread 2 use time=5
* * * * * thread 1 Use time=2
* * * * * Thread 2 use time=6
The priority of threads is independent of the print order, and their relationships are uncertain and random
Related articles
Java multithreaded Series 8-Thread priority
Java Multithreaded Series 7-stopping threads
Java Multithreaded Series 6-blocking queues
Java Multithreaded Series 5-deadlock and inter-thread communication
Java multithreaded series 4-thread pool
Java multithreaded series 3-Thread synchronization
Java Multi-threaded series 2-wire program control system
Java Multithreading Series-Thread implementation and scheduling
Java multithreaded Series 8-Thread priority