Limit thread priority and scheduling
The java thread model involves thread priorities that can be changed dynamically. In essence, the priority of a thread is a number from 1 to 10. The larger the number, the more urgent the task is. The JVM standard calls the higher priority thread first and then the lower priority thread. However, the standard’s processing of threads with the same priority is random. How these threads are handled depends on the underlying operating system policy. In some cases, threads with the same priority run time-sharing; In other cases, the thread will run until the end. Remember, Java supports 10 priorities, and the grassroots operating system may support much less priorities, which will cause some confusion. Therefore, priority can only be used as a rough tool. The final control can be accomplished by judiciously using the yield () function. In general, do not rely on thread priority to control the state of threads.
This article explains how to use threads in Java programs. More important issues such as whether threads should be used depend largely on the application at hand. One way to decide whether to use multithreading in an application is to estimate the amount of code that can run in parallel. And remember the following:
Using multithreading does not increase CPU capacity. However, if the JVM’s local thread implementation is used, different threads can run simultaneously on different processors (in multi CPU machines), so that multi CPU machines can be fully utilized.
If the application is computationally intensive and constrained by CPU function, only multi CPU machines can benefit from more threads.
Multithreading is often advantageous when an application must wait for slow resources, such as a network connection or database connection, or when the application is non interactive.
The software based on Internet must be multithreaded; Otherwise, the user will feel that the application response is slow. For example, multithreading can make programming easier when developing servers that support a large number of clients. In this case, each thread can serve different customers or customer groups, thus reducing the response time.
Some programmers may have used threads in C and other languages where there is no language support for threads. These programmers may often be made to lose confidence in threads.