In a Java multithreaded environment. To ensure that all threads run in accordance with certain rules, theJVM implements a thread scheduler, which defines the policy of thread scheduling, provides for the allocation of CPU operations, and assigns CPUs to multiple threads according to these specific mechanisms. The right to use. This section focuses on how threads are scheduled and understands that the Java thread scheduling pattern facilitates further discussion of the concurrency framework behind it.
The general thread scheduling pattern is divided into two kinds-preemptive scheduling and cooperative scheduling.
Preemptive scheduling refers to the time each thread runs, the switch of the thread is controlled by the system, the system control refers to the system under some kind of operating mechanism, each thread may be divided into the same running time slice. It is also possible that some threads are running longer slices of time. Even some threads do not get to run the time slices.
Under such a mechanism, the blockage of one thread does not cause the whole process to jam. Cooperative scheduling refers to a first-line operation after the active notification system to switch to a thread to run, such a pattern is like a relay race, a person ran his way to the next person to the baton, under the individual continue to run down. The running time of a thread is controlled by the thread itself. Thread switching can predict that there is no multithreading synchronization problem, but it has a fatal weakness: If a thread is written with a problem and runs half the time, it can cause the whole system to crash.
Figure 2-5-6-1
To illustrate the difference between the two modes for more image. Look at Figure 2-5-6-1. The left side is a preemptive thread dispatch, assuming that three threads are required to run. The path that the processor runs is that the thread runs a time slice and then forces a time slice to be switched to two threads, then cuts to the line Cheng, then goes back to the line Cheng, so loops until the three threads are all running. Collaborative thread scheduling is not the way to go. It will finish running the thread first. Threads repeatedly notify thread two to run. The line Cheng again notifies the line Cheng until the thread three runs out.
After learning about the two thread scheduling modes. Now consider what kind of thread scheduling mode Java is using. The discussion of this issue involves the implementation of the JVM , which specifies that each thread has a priority and that the higher the priority, the more priority it runs. However, the high priority does not mean that it can occupy the running time slice on its own, it may be the more running time slice with higher priority, but the lower priority is less run time but not allocated to run time. The specification of the JVM is not strictly defined for the scheduling policy. I think it is because of the many different scheduling strategies. It is not realistic for the JVM to encapsulate all the details to provide a unified strategy. So it gives a definition that is not rigorous but uniform enough.
Back to the problem, the thread schedule used byJava is preemptive scheduling, which is embodied in the JVM as having the CPU rights of a thread with a high priority in the running pool, assuming that the threads are selected randomly in the same way as the thread priority in the run pool. But be aware that there is actually an absolute point in time where only one thread is running (this is relative to a CPU , assuming your machine is multicore or maybe multiple threads are running at the same time), Until this thread enters a non-operational state or a thread with a higher priority enters the running thread pool, it makes it out of the CPU , and higher-priority threads preempt the CPUof the low-priority thread.
Java The thread will allocate CPU time slices by priority, so when do the threads give up the use of the CPU ? Can be categorized into three situations:
-
The current running thread actively discards the CPU. The JVM temporarily abandons the CPU operation (the JVM operating system based on the time slice rotation schedule does not let the thread Cheng Yongju abandon the CPU, or abandon the runtime of this time slice), such as call yield () method.
-
The current running thread is plugged into a blocked state for some reason, such as blocking on I/O .
-
The current running thread ends. The task inside the Run () method is completed.
The third of the three cases is very good understanding, the task run out of the natural CPU, the first two cases with two examples, the first look at using yield to abandon the CPU what situation:
public class Yeildthread {
publicstatic void Main (string[] args) {
mythreadmt = new MyThread ();
Mt.start ();
while (true) {
System.out.println ("Main thread ");
}
}
}
Class MyThread extends Thread {
Publicvoid Run () {
while (true) {
System.out.println (" Discarded Threads ");
thread.currentthread (). yield ();
}
}
}
Intercepting a segment of output such as the following, the output "main thread" than "abandoned thread" run more opportunities, because the MT thread each time the loop to the main thread. It is because the yield operation does not always give up the CPU, just give up the time slice and give the rest of the time to other threads.
Main thread
Main thread
Main thread
Main thread
Main thread
Main thread
Discarded threads
Main thread
Main thread
Main thread
Main thread
Main thread
Main thread
Main thread
The second example is to save code that will be represented by pseudo-code, the sample is simple but has been able to explain the problem, the running program will have two threads to work,Iothread each encounter I/O congestion will discard the current time slice, and the main thread is the JVM The allotted time slices run normally.
public class Ioblockthread {
publicstatic void Main (string[] args) {
Iothread iothread = new Iothread ( );
Iothread.start ();
main thread task run
}
}
Class Iothread extends Thread {
Publicvoid Run () {
while (true) {
I/O clogging
}
}
}
Java the thread's scheduling mechanism is implemented by the JVM , if there are several threads. You want some threads to have a longer run time. or some threads to allocate less run time, this involves "thread priority",Java divides the thread priority into ten levels, and when the thread is created, it assumes that there is no clear declaration to use the default priority. The JVM allocates the probability of running time based on the priority of each thread.
There are three constants thread.min_priority,thread.norm_priority, andthread.max_priority , respectively, representing the minimum priority value (1 ), the default priority value (5), and the maximum priority value (ten).
Because the implementation of the JVM is based on the host operating system, there must be some mapping between the Java priority values and the native thread priorities of the various operating systems, which is sufficient to encapsulate the priority of all operating systems to provide uniform priority semantics. For example, the 1-10 priority value in Linux may be mapped with the 0-99 priority value, while the Windows System has 7 priorities to map.
Thread scheduling policy determines the upper-level multi-threaded operation mechanism, theJVM 's thread scheduler implements preemptive scheduling, each thread run time by it allocation management, it will follow the recommendation of thread priority to run the thread to allocate time, the higher the priority. The longer the CPU is likely to get.
Click order Author book "Tomcat Kernel Design Anatomy"
Java Multi-Threading scheduling strategy