Objective
A thread is used to perform a task, and the thread finishes executing task A to perform task B. Multithreading is produced in order to perform two tasks at the same time.
For the example of our daily life:
(1) I opened a video software, I opened a thread a let it perform the download task, I opened a thread B, used to play the video. After I have opened two threads, these two tasks can be executed simultaneously, if I open a thread, only the download task is completed, to perform the playback task.
(2) The thread is equivalent to the waiter in the shop, a waiter can only do one thing, more than a few waiters, you can do a lot of things at the same time.
Before we learn about multithreading, let's start by introducing processes and threads
Process
A process is a program that runs in the operating system. The application's execution instance cannot execute the task the resource created at runtime can contain multiple threads as the process terminates
Thread
A thread is a separate unit of execution within a process a thread can execute only one task at a time that contains the main thread (user interface thread) and child (worker or background thread) threads that are executed and destroyed
main Thread: Automatically created and started when the application starts, usually used to process the user's input and respond to various events and the termination of the main thread of the message means the end of the program
Child Threads : There is a thread created to help the main thread to execute the program's background processing task, if child thread A creates a child thread B, after the creation of the two are independent of each other, the effect between multiple sub-threads can be executed concurrently
Thread status : Five thread states click the Open link
Multithreading
(1) Most apps currently need to connect to the server, and accessing the server may be fast or slow. If the operation of an app access server does not operate on a sub-thread, the software cannot respond to the user's actions during the app's access to the server, and the app can respond to the user's actions only after the app has been accessed, which causes thread blocking, which is a common lag. A thread can only perform one task at a time, but a process may have multiple threads. Multiple threads can be opened to perform different tasks, thus improving the execution efficiency of the program and avoiding thread blocking.
(2) Each thread can be set priority, the operating system according to the priority of the thread to schedule the CPU time, high priority threads, the chance of priority calls will be greater, the same level, the execution of the thread execution has an impact on the succession
(3) At the same time, the CPU can only process one thread, and only one thread is working. Multi-threaded parallel execution, in fact, each thread is constantly switching, because the time to perform the switchover quickly, resulting in the illusion of execution at the same time, the principle, such as a A, a two thread;
①a execution to a certain time period to switch, but a task is not completed, the system will be a current execution of the location and data in the way to save it in a stack
② then the b thread executes, b execution time is up, and its position status is saved to the stack of B.
The ③ system automatically finds the stack of a, restores the data that was saved before a, and resumes execution from a previously disconnected state, so the loop
(4) The system each open a thread has a relatively large overhead, if the thread open too much, not only will occupy a lot of memory and make the city and countryside become more complex, and will increase the burden of the CPU, such software, you can save the winter to buy warm hand treasure money.
Multithreading Benefits
(1) Improve program execution efficiency, avoid the lag caused by thread blocking
(2) Can improve resource utilization (CPU, memory) properly
The lack of multithreading
(1) Open thread takes up a certain amount of memory space
(2) The more threads, the greater the overhead of CPU thread scheduling
(3) More complex programming: such as communication between threads, multi-threaded data sharing
Summarize
Thread-to-process relationships
A thread is the basic unit of CPU execution, a process can have multiple threads, but can only perform one task at a time.
The process is the running software, is dynamic.
An operating system can correspond to multiple processes, a process can have multiple threads, but at least one thread
Threads in the same process share resources in a process
Main Thread
Automatically created as soon as a process starts
Displaying and refreshing the UI interface
Handling UI Events
The role of child threads
Handle time-consuming operations
Child threads cannot be used to refresh the UI
three kinds of multithread programming technology1. NsthreadNsthread is lightweight, multithreaded development, and it is not complicated to use, but using nsthread requires you to manage the thread's declaration cycle.2. Cocoa nsoperation multithreaded development using nsoperation and Nsoperationqueue is similar to a thread pool, as long as a nsoperation (which requires the use of its subclasses nsinvocationoperation in real development, Nsblockoperation) put it in Nsoperationqueue this queue will start in turn. Nsoperationqueue is responsible for managing and executing all nsoperation, making it easier to manage the total number of threads and to control dependencies between threads.
Nsoperation has two commonly used subclasses for creating threading operations: Nsinvocationoperation and Nsblockoperation, which are essentially no different, but the latter using block form for code organization, using relatively convenient.
3. GCD (Grand Central Dispatch)GCD is based on C language development of a set of multi-threaded development mechanism, but also the current Apple website recommended multithreading development method.
GCD automatically manages the thread's life cycle (creating threads, scheduling tasks, destroying threads), and programmers just need to tell gcd what they want to do, without having to write any thread-management code
GCD is the highest level of abstraction in these three multithreaded development methods, and is the most convenient to use, just based on C language development, not like the first two are object-oriented development, but completely process-oriented. The most significant advantage of this mechanism compared to the previous two multithreaded development methods is that it is more efficient for multicore operations and will automatically take advantage of more CPU cores (such as dual-core, quad-core).
GCD also has a queue similar to Nsoperationqueue, GCD Unified management of tasks in the entire queue. However, the queues in GCD are divided into two categories: Parallel queue and serial queue.
1. Serial queue: Only one thread, the actions added to the queue are executed sequentially in order of addition.
2, concurrent queue: There are multiple threads, the operation in the future he will be the threads on the available processors, while ensuring that the advanced task priority processing.
In fact, there is a special queue in GCD that is the home row, which is used to perform the operation tasks on the main thread.
Introduction to the multithreading theory of iOS development