Advantages and disadvantages of multithreading

Source: Internet
Author: User

When to use multithreading technology and when to avoid it is an important subject we need to master. Multi-Threading technology is a double-edged sword, which needs to consider its advantages and disadvantages when using.
Multithreaded processing can run multiple threads at the same time. Because multithreaded applications divide programs into separate tasks, you can significantly improve performance in the following ways:
(1) Multithreading technology makes the program response faster, because the user interface can be in other work while still active;

(2) The processor time can be ceded to other tasks when the task is not currently being processed;

(3) The task that takes up a lot of processing time can give the processor time to other tasks on a regular basis;

(4) Can stop the task at any time;

(5) Each task's priority can be set individually to optimize performance.

Whether you need to create multiple threads depends on various factors. Multithreading is best used in the following situations:
(1) Time consuming or heavy processor-intensive tasks blocking user interface operation;

(2) Each task must wait for an external resource (such as a remote file or Internet connection).


In the same way, multithreading also has many drawbacks that need to be fully considered when considering multiple threads. The main drawbacks of multithreading include:
(1) Waiting for the use of shared resources causes the program to run slower. These shared resources are primarily exclusive resources, such as printers.

(2) Managing threads requires additional CPU overhead. The use of threads can impose an additional burden on the system for context switching. When this burden exceeds a certain level, the characteristics of multithreading are mainly manifested in its shortcomings, such as the use of independent threads to update each element in the array.

(3) The deadlock of the thread. That is, long waits or resource contention, as well as deadlocks and other multi-threading symptoms.

(4) Read or write to the public variable at the same time. When multiple threads need to write to a public variable, the latter thread tends to modify the data held by the previous thread, thus allowing the parameters of the previous thread to be modified, and, when the read and write operations of the public variable are non-atomic, the uncertainty of the time on different machines, Results in an error in the operation of the data within one thread, resulting in an inexplicable error that the programmer cannot predict.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Advantages and disadvantages of multithreading

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.