Advantages and disadvantages of Multithreading

Source: Internet
Author: User

When multithreading technology is used and when to avoid it is an important topic we need to master. Multithreading is a double-edged sword. Its advantages and disadvantages must be fully considered during use.
Multi-threaded processing can run multiple threads at the same time. Multi-threaded applicationsProgramDividing programs into multiple independent tasks can significantly improve performance in the following aspects:
(1) multithreading technology makes the response speed of the program faster, because the user interface can be active while performing other work;

(2) When no task is processed, the processor time can be given to other tasks;

(3) tasks that occupy a large amount of processing time can regularly give the processor time to other tasks;

(4) the task can be stopped at any time;

(5) You can set the priority of each task to optimize the performance.

Whether multiple threads need to be created depends on various factors. In the following cases, multithreading is the best choice:
(1) time-consuming or a large number of processor-consuming tasks block user interface operations;

(2) Each task must wait for external resources (such as remote files or Internet connections ).

In the same way, multithreading has many disadvantages, so we need to fully consider multithreading. The main disadvantages of multithreading include:
(1) The program runs slowly when resources are shared. These shared resources are primarily dedicated resources, such as printers.

(2) additional CPU overhead is required for thread management. The use of threads puts an extra burden on context switching. When such a burden exceeds a certain level, multithreading is characterized mainly by its shortcomings. For example, an independent thread is used to update each element in the array.

(3) deadlock of the thread. That is, long wait, resource competition, deadlock, and other multi-thread symptoms.

(4) Simultaneous reading or writing of public variables. When multiple threads need to write public variables, the latter thread often modifies the data stored in the previous thread to modify the parameters of the previous thread. In addition, when the read/write operations of public variables are non-atomic, the uncertainty of the interrupt time on different machines may lead to errors in data operations in a thread, resulting in inexplicable errors, such errors are unpredictable to programmers.

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.