Small notes about multithreading

Source: Internet
Author: User

The use of multithreading can improve the efficiency of the work, for example, we download the larger files (such as a song), if not using multi-threaded, then, after clicking on the download, the program starts to download, before the download is complete, the page is not able to perform other operations, the user will appear in the app when using the card, This is not the case when you use multi-threading to place time-consuming operations in child threads

1. What is multithreading

Multiple threads can be opened in 1 processes, each thread can perform different tasks in parallel (at the same time)

Multithreading technology can improve the efficiency of program execution

2. The principle of multithreading

At the same time, the CPU can only handle 1 threads, and only 1 threads are working (executing)
Multi-threaded concurrency (simultaneous) execution, in fact, the CPU is quickly dispatched between multiple threads (switching)
If the CPU schedules threads fast enough, it creates the illusion of multi-threaded concurrency execution
Think: What happens if the threads are very, very much?
CPU will be dispatched between N multithreading, the CPU will be exhausted, consumes a lot of CPU resources
The frequency at which each thread is scheduled to execute is reduced (thread execution efficiency is reduced)

3. Advantages and disadvantages of multithreading

Advantages of multithreading

To improve the execution efficiency of the program appropriately

Can appropriately improve resource utilization (CPU, memory utilization)

Disadvantages of multithreading

Open threads need to occupy a certain amount of memory (by default, the main thread takes up 1M, the child threads occupy 512KB), if you open a large number of threads, will consume a lot of memory space, reduce the performance of the program

The more threads, the greater the overhead of the CPU on the dispatch thread

Programming is more complex: such as communication between threads, multi-threaded data sharing

4. Multithreading in the development of iOS application

Main thread: After an iOS program runs, the default is to turn on 1 threads, called the "main thread" or "UI thread"

Main functions of the primary thread

Display \ Refresh UI interface

Handling UI events (such as click events, scrolling events, drag events, etc.)

Use of the main thread note: Do not put more time-consuming operations into the main thread.

Time-consuming operations can get stuck in the main thread, seriously affecting the smoothness of the UI, giving users a "card" bad experience

Original address: http://www.cnblogs.com/wendingding/p/3805088.html

Small notes about multithreading

Related Article

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.