Java multi-thread priority instance program

Source: Internet
Author: User

Key point: priority only increases the probability, but not necessarily takes priority.
In multithreading, similar to thread sleep, the thread priority still cannot guarantee the thread execution order. However, a thread with a higher priority has a higher probability of obtaining CPU resources. A thread with a lower priority does not have the chance to execute the task.

The priority of a thread is represented by an integer between 1 and 10. The higher the value, the higher the priority. The default priority is 5.

The code is as follows: Copy code
Package com. javaer. thread;
 
Public class PTread implements Runnable {
 
 
Public static void main (String [] args ){
Thread s1 = new Thread (new TSleep (), "S1 ");
Thread s2 = new Thread (new TSleep (), "S2 ");
S1.setPriority (10 );
S2.setPriority (1 );
S1.start ();
S2.start ();
}
 
@ Override
Public void run (){
// TODO Auto-generated method stub
For (int I = 0; I <10; I ++ ){
System. out. println ("Thread" + Thread. currentThread (). getName () + "" + I + "times! ");
Try {
Thread. sleep (10 );
} Catch (InterruptedException e ){
E. printStackTrace ();
  }
}
}
 
}

Thread S1 0th executions!
Thread S2 0th executions!
Thread S1 1st Executions!
Thread S2 1st Executions!
Thread S2 2nd executions!
Thread S1 2nd executions!
Thread S1 3rd executions!
Thread S2 3rd executions!
Thread S1 4th executions!
Thread S2 4th executions!
Thread S2 5th executions!
Thread S1 5th executions!
Thread S2 6th executions!
Thread S1 6th executions!
Thread S2 7th executions!
Thread S1 7th executions!
Thread S1 8th executions!
Thread S2 8th executions!
Thread S2 9th executions!
Thread S1 9th executions!

Summary

The Java Thread model involves the thread priority that can be dynamically changed. Essentially, the priority of a thread is a number ranging from 1 to 10. The larger the number, the more urgent the task is. The JVM standard calls a thread with a higher priority before calling a thread with a lower priority. However, the processing of threads with the same priority is random. How to handle these threads depends on the operating system policy at the grassroots level. In some cases, threads with the same priority run at a time. In other cases, the threads run until the end. Keep in mind that Java supports 10 priorities, and the base-level operating system may have a much lower priority, which may cause some confusion. Therefore, priority can only be used as a rough tool. The final control can be done by wise using the yield () function. Generally, do not rely on the thread priority to control the thread status.

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.