The methods of Java thread scheduling are: 1. Collaborative thread scheduling. Multi-threaded systems adopt collaborative scheduling, and the execution time of the thread is controlled by the thread itself. 2. Preemptive thread scheduling. Using a preemptive scheduling multi-thread system, the execution time of each thread will be allocated by the system.
The operating environment of this tutorial: Windows 7 system, Java 10 version, DELL G3 computer.
1. Cooperative thread scheduling
The multi-threaded system adopts cooperative scheduling. The execution time of the thread is controlled by the thread itself. After the thread completes its work, it actively notifies the system to switch to another thread. The advantage of cooperative multithreading is that it is simple to implement, because the thread will switch threads after completing its own work. The switching operation is known to the thread itself, so there is generally no thread synchronization problem. Its shortcomings are also obvious: the thread execution time cannot be controlled, and if the code of a thread is written incorrectly and the system is not allowed to switch threads, the program will always block.
2. Preemptive thread scheduling
Using a preemptive scheduling multi-thread system, the execution time of each thread will be allocated by the system, and thread switching is not determined by the process itself. For example, in Java, there is a Thread::yield() method that can actively give up execution time, but if you want to actively obtain execution time, the thread itself has no way. In this way, thread scheduling is implemented, and the execution time of the thread is controlled by the system. There is no problem that one thread causes the entire process or even the entire system to block.
The above are the two methods of Java thread scheduling. I hope they can be helpful to everyone.