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:
-
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.
-
The current running thread enters a blocking state for some reason, such as blocking on I/O.
-
The current running thread ends with the task running through the run () method.
Java Multi-Threading scheduling strategy