Java multi-thread programming restriction priority

Source: Internet
Author: User

Restrict thread priority and Scheduling

The Java thread model involves the thread priority that can be dynamically changed. Essentially, the priority of a thread is a number ranging from 1 to 10. The larger the number, the more urgent the task is. The JVM standard calls a thread with a higher priority before calling a thread with a lower priority. However, the processing of threads with the same priority is random. How to handle these threads depends on the operating system policy at the grassroots level. In some cases, threads with the same priority run at a time. In other cases, the threads run until the end. Keep in mind that Java supports 10 priorities, and the base-level operating system may have a much lower priority, which may cause some confusion. Therefore, priority can only be used as a rough tool. The final control can be done by wise using the yield () function. Generally, do not rely on the thread priority to control the thread status.

Summary

This article describes how to use a thread in a Java program. A more important issue like whether to use a thread depends on the application at hand. One way to determine whether to use multithreading in an application is to estimate the amount of code that can be run in parallel. Remember the following points:

Multithreading does not increase the CPU capability. However, if the local thread of JVM is used for implementation, different threads can run simultaneously on different processors (in multiple CPU machines), so that multiple CPU machines can be fully utilized.

If applications are computing-intensive and restricted by CPU functions, only multiple CPU machines can benefit from more threads.

Multithreading is usually advantageous when the application must wait for slow resources (such as network connections or database connections), or when the application is non-interactive.

Internet-based software must be multi-threaded; otherwise, the user will feel the application is slow to reflect. For example, multithreading can make programming easier when developing a server that supports 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, and there is no language support for threads in those languages. These programmers may usually lose confidence in the thread.

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.