Java Multi-Threading scheduling strategy

Source: Internet
Author: User

Scheduling mode for two threads:

Preemptive scheduling:

Preemptive scheduling refers to the time each thread executes, the switch of the thread is controlled by the system, and the system control means that under some operating mechanism of the system, each thread may be divided into the same execution time slice, or it may be that some threads execute a longer slice of time, even some threads do not get the time slice to execute. In this mechanism, the blockage of one thread does not cause the whole process to clog.

Co-scheduling:

Cooperative scheduling refers to a thread after the execution of the active Notification system switch to another thread execution, this mode is like a relay race, a person ran his way to the next person to the baton, under the individual continue to run down. Thread execution time is controlled by the thread itself, thread switching can be predicted, there is no multithreading synchronization problem, but it has a fatal weakness: If a thread is written with a problem, running to half of the blocked, it may cause the entire system to crash.

Implementation of the JVM:

The JVM specification specifies that each thread has a priority, and the higher priority is performed, but the higher priority does not mean that it can occupy the execution time slice on its own, it may be the more execution time slice with high priority, and conversely, the low-priority sub-execution time is less but the execution time is not allocated.

Thread-Scheduling preemptive scheduling used by Java

Java threads allocate CPU time slices to run on a priority scale

The thread yields the CPU:

    1. The current running thread abandons CPU,JVM temporarily abandons the CPU operation (the JVM operating system based on the time slice rotation schedule will not let the thread Cheng Yongju abandon the CPU, or abandon the execution of this time slice), such as calling the yield () method.

    2. The current running thread enters a blocking state for some reason, such as blocking on I/O.

    3. The current running thread ends with the task running through the run () method.

Java Multi-Threading scheduling strategy

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.