4.0 multithreading, 4.0 Multithreading

Source: Internet
Author: User

4.0 multithreading, 4.0 Multithreading

4.0-1.1 Processes

  • Concept: process refers to an application running in the system (each APP in the operating system is a process)
  • Nature: each process is independent, and each process runs in its dedicated and protected memory space.

Processes open up independent memory space for applications. (This memory space is independent and protected! Process and process do not interfere with each other !)

  • For example, if both QQ and Xcode are enabled, the system starts two processes.
  • View processes: You can use the activity monitor to view processes enabled in the Mac system.
  • All UI operations and UI events are processes.

 

4.0-1.2 threads
  • To execute a task, one process must have at least one thread (each process must have at least one thread)
  • A thread is the basic execution unit of a process. All tasks of a process (Program) are executed in the thread (thread Execution Process/code in the application)
For example, playing music with a cool dog and downloading movies with thunder all need to be executed in the thread.
 
  • Main thread: after an application is started, a thread is enabled by default.
  • Sub-thread: A thread other than the main thread (manually enabled)
Single thread-serial:
Concept: the execution of tasks in one thread is serial (the code in the thread is executed in order! Only one code block can be executed at a time !)
Resolution: If you want to execute multiple tasks in one thread, you can only execute these tasks one by one. That is to say, within the same time, one thread can only execute one task.
For example, you can only download three files (File A, file B, and file C) in one thread.
Conclusion: Therefore, the thread can also be considered as one execution path in the process.
Q: Why can I solve the problem of getting stuck in the main thread after a new thread is started? A: This is because the thread and thread are concurrently executed )!
Multiple Threads-parallel: Multithreading
Concept: Multiple Threads can be enabled in one process, and each thread can execute different tasks in parallel (at the same time ).
Advantage: multithreading technology can improve program execution efficiency
For example, if three threads are enabled simultaneously to download three files (File A, file B, and file C ),
4.0-1.3 [difference] process & Thread  A process is composed of many threads. A process can contain many threads and each thread can execute different codes! Differences between processes and threads:
(1) scheduling: A thread is the basic unit of scheduling and allocation, and a process is the basic unit of resource ownership.
(2) concurrency: not only can concurrent execution be performed between processes, but also between multiple threads of the same process.
(3) Owning resources: a process is an independent unit of resources. A thread does not own system resources, but can access resources belonging to a process.
(4) system overhead: when a process is created or withdrawn, because the system allocates and recycles resources for it, the overhead of the system is significantly greater than the overhead when a thread is created or withdrawn.
4.0-2.1 multithreading Principle Multithreading principle: (Why can multiple threads be concurrently executed ?)
Essence: At the same time, the CPU can only process one thread, and only one thread is concurrently executed at work (execution, in fact, the CPU is quickly scheduled between multiple threads (switching)
Principle: If the CPU scheduling thread time is fast enough, it creates the illusion of multi-thread concurrent execution.
Think: what happens if there are many threads? Answer: if there are many threads, they will switch back and forth between multiple threads, consuming a lot of CPU resources.
  • The number of times each thread is scheduled will decrease
  • The thread execution efficiency will decrease
The CPU will be scheduled between N threads, and the CPU will be exhausted, consuming a lot of CPU resources. The frequency of scheduling and execution of each thread will be reduced (the thread execution efficiency will be reduced)

The default stack size of the iOS 8.0 main thread is also 512 KB.

Advantages and disadvantages of 4.0-2.2 Multithreading Advantages of multithreading:
(1) The program execution efficiency can be appropriately improved:
  • Concurrent execution (simultaneous execution): The thread and thread are executed at the same time! (Tasks are executed simultaneously in different threads)
(2) appropriately improving resource utilization (CPU and memory usage)
Disadvantages of multithreading: (since multithreading is so cool, is the more threads the better ?)
(1) enabling a thread requires a certain amount of memory space (by default, the main thread occupies 1 MB, and the sub-thread occupies KB of stack space). If a large number of threads are enabled, it will occupy a large amount of memory space and reduce program performance.
(2) The more threads are enabled, the CPU needs to switch back and forth between multiple threads. The larger the CPU overhead on the scheduling thread, the more tired the CPU!
(3) enabling the thread will increase the complexity of the Code (this will add a lot of code to the application! After the Code increases, the complexity of the Program increases !) Program Design is more complex: for example, communication between threads and multi-thread data sharing
Suggestion: in mobile application development, generally only 3 ~ Five threads! 4.0-2.3 Applications of multithreading in iOS development  Main thread/UI thread:
Concept: After an iOS program runs, one thread is enabled by default, which is called the "main thread" or "UI thread" (the thread created by the program startup)
Purpose:
  • Display/refresh UI
  • Process UI events (such as click events, rolling events, and drag-and-drop events)
Note:
  • Do not place time-consuming operations in the main thread
  • The time-consuming operations should be executed in the background thread; otherwise, the UI smoothness will be affected. This will give the user a bad 'choppy 'Experience, undermining the user experience.
  • All network access operations are time-consuming!
Solution:
  • Place time-consuming operations in sub-threads (background threads, non-main threads)
  • In this way, our main thread will not be blocked. Generally, 3 ~ 6 threads
Benefits:
  • When the user clicks the button, the response is displayed.
  • Able to process time-consuming operations and events with UI controls at the same time
  4.0-2.4 multi-thread implementation solution in iOS
If you think this article is helpful to you, click "recommended" in the lower right corner, ^ _ ^
Author: Loto)
Source: http://www.cnblogs.com/shorfng/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent and provide a connection to the original article on the article page.   If you have any questions, please send an e-mail to shorfng@126.com to contact me.    

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.