Precedence of Java concurrent threads

Source: Internet
Author: User

Precedence of Java concurrent threads

@author Ixenos

Execution time for low-priority threads

1. At any one time, when multiple threads are in a running state, the running system always picks a thread with the highest priority to execute, and a low-priority thread may be executed only if the thread stops, exits, or is not executed for some reason.

2. When two threads with the same priority are waiting to execute at the same time, the running system chooses a thread execution in Round-robin (that is, the polling schedule, as determined by the algorithm) (the priority policy of Java is preemptive scheduling!). )

3. The selected thread can exit for a reason and give other threads the opportunity to execute:

1) A higher-priority thread is in a running state (Runnable)

2) thread active exit (yield), or the end of its Run method

3) on a system that supports time- sharing , the end of the slice allocated to the thread

The thread scheduling algorithm for the 4.Java operating system is preemptive (preemptive), and when a higher priority thread appears and is in the runnable state, the running system chooses a high-priority thread to execute

5. Exceptionally, when a high-priority thread is in a blocking state and the CPU is idle, a low-priority thread is scheduled to execute

1  Public classpriorityexample{2      Public Static voidMain (strinig[] args) {3Thread A =NewPThread ("A");4Thread B =NewPThread ("B");5A.setpriority (7); Setting the priority level6A.setpriority (1);7     }8 }9 Ten classPThreadextendsthread{ One      PublicPThread (String N) { A         Super(n); -     } -  the      Public voidrun () { -          for(inti=0; i<5000000; i++){ -             if(i%5000000 = = 0){ - System.out.print (GetName ()); +             } -         } +     } A}

Output AAAAAAAAABBBBBBBBB

Selfish threads

1. In general, the sleep method can be called in the thread, discarding the current threads ' use of the processor, so that each thread has an opportunity to be executed, but sometimes the thread may not follow this rule!

1  Public void run () {2      for (int i=0; i<5000000; i++) {3         if (i%5000000 = = 0) {4            System.out.print (GetName ()); 5         }6    }7}

The 2.for loop is a tight loop, and once the running system chooses a thread that has a for loop body , the thread does not abandon the use of the processor, unless the For loop is terminated naturally or the thread is preempted by a thread with a higher priority, which is called a self-interested thread

3. Self-interested threads generally do not cause problems, but sometimes allow other threads to wait a long time before they get the right to use the processor

Time Sharing method

1. In order to solve the problem that self-interested threads may occupy CPU for a long time, some systems restrict the execution of self-interested threads by means of ticks , such as Windows2000 or windowsnt systems

2. In the time-sharing mode, the processor is allocated according to the temporal slice, for those with the same highest priority of multiple threads, the time-sharing technology will alternately allocate the CPU slices to execute, when the time slice ends, even if the thread does not run the end, will also give up the CPU use

3. After commenting out the priority setting, the output becomes aaaaabbbbbaaabbb or Aabbaaaabbbbaabba

1 Public classpriorityexample{2 Public Static voidMain (strinig[] args) {3 Thread A =NewPThread ("A"); 4 Thread B =NewPThread ("B"); 5//a.setpriority (7);//setting the priority level6//a.setpriority (1);7     } 8 } 9 10classPThreadextendsthread{11 PublicPThread (String N) {12Super(n);13     }14 15 Public voidrun () {16 for(inti=0; i<5000000; i++){17if(i%5000000 = = 0){18System.out.print (GetName ());19             }20         }21st     }22}

If you run the program on another platform that does not support time-sharing technology, the resulting output may be deterministic! aaaaaaaaabbbbbbbbb

4.Java running System does not realize the time-sharing, the timeshare is related to the peace platform, and some platforms do not support ticks, in the writing of Java multi-thread program, can not rely too much on timeshare technology to ensure that each thread has a fair execution opportunity! You should usually write a program that can voluntarily abandon the use of the processor, while a thread can invoke the yield method to voluntarily discard the processor's right to use

Note: Using yield only provides execution opportunities to the same-priority thread, and if no thread with the same priority is in a running state, the yield method is ignored!

Precedence of Java concurrent threads

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.