Multithreading Summary Tour (3): Advantages and disadvantages of multithreading

Source: Internet
Author: User

Description: A C # program called Multithreading can be done in 2 ways: explicitly creating and running multi-threading, or using the. NET framework to secretly use multithreaded features-such as the BackgroundWorker class, the thread pool , Threading Timer, remote server, or Web services or ASP. In the latter case, people have no choice but to use multithreading; a single-threaded ASP. NET Web server is not too cool, even if there is such a thing; Fortunately, multithreading is quite common in application servers, and the only thing worth worrying about is the static variable problem that provides the appropriate locking mechanism.

First, the advantages of multithreading:

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) Individual tasks can be prioritized to optimize performance

Second, the disadvantage of multithreading:

(1) thread is also a program, so threads need to occupy memory, the more threads occupy more memory;

(2) Multithreading needs coordination and management, so CPU time to track the thread;

(3) The access to shared resources between threads affects each other, and the problem of competing shared resources must be resolved;

(4) Too many threads can lead to too much control, which can cause a lot of bugs in the end.

Third, when to use multithreading

(1) Time consuming or heavy processor-intensive tasks blocking user interface operation;

Multithreaded programs are typically used to perform time-consuming tasks in the background. The main thread keeps running, and the worker thread does its background work. For Windows Forms programs, if the main thread attempts to perform lengthy operations, the keyboard and mouse operations become dull and the program loses its response. For this reason, you should add a worker thread when running a time-consuming task in a worker thread, even if there is a good hint in "processing ..." on the main thread to prevent the work from continuing. This avoids the program's "no corresponding" prompt by the operating system to persuade the user to force the process to end the program, which results in an error. The modal dialog box also allows the "cancel" feature to be implemented, allowing the continuation of receiving events, while the actual task has been completed by the worker thread. BackgroundWorker happens to assist in this function.

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

In programs that do not have a user interface, such as Windows Service, multithreading is potentially time-consuming when a task is implemented, because it is particularly meaningful to wait for a response from another computer, such as an application server, a database server, or a client. Accomplishing tasks with a worker thread means that the main thread can do other things right away.

Iv. when not to use multithreading

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.

Multithreading Summary Tour (3): 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.