A process is an application that is running in the system ( a program can correspond to multiple processes ).
each process is independent , and each process runs within its dedicated and protected memory space.
For example, the simultaneous opening of thunder, Xcode, the system will start 2 processes respectively.
1 processes to perform a task, you must have a thread ( the process is composed of threads, and all tasks of the process are thread-complete , with at least 1 threads per 1 processes).
The execution of a task in 1 threads is serial. Threads are serial (one-to-one), and to perform multiple tasks in 1 threads, you can perform these tasks sequentially, one at a time, that is, 1 threads can only perform one task at a time.
Process and Thread comparisons:
1. Threads are the smallest unit of CPU calls (performing tasks).
2. The process is the unit in which the CPU allocates resources and schedules. (The CPU allocates resources on a per-process basis, and is not allocated in terms of threads.) But the task in the process is done by the thread, so the thread is the smallest unit of CPU execution.
3. A program can correspond to multiple processes, a process can have multiple threads, but at least one thread.
4. threads in the same process share the resources of the process .
Multithreading:
Multiple threads can be opened in 1.1 processes, and each thread can perform different tasks in parallel (at the same time) . ( threads are serialized internally , but threads and threads can be performed concurrently).
2. Process-workshop, thread-workshop workers
3. Multithreading technology can improve the efficiency of program execution
The principle of Multithreading:
1. At the same time, the CPU can only handle 1 threads, only 1 threads are working (executing)
2. Multi-threaded concurrency (simultaneous) execution, in fact, the CPU is quickly dispatched between multiple threads (switching)
3. If the CPU schedules threads fast enough, it creates the illusion of multi-threaded concurrency (multi-core CPUs are really multithreaded)
4. Too many threads can cause CPU switching to consume too much (switch to save context) and the CPU is exhausted. Usually 3-5 a thread.
Advantages and disadvantages of multithreading:
Advantages of Multithreading:
1. Can properly improve the execution efficiency of the program, can appropriately improve resource utilization (CPU, memory utilization).
Disadvantages of Multithreading:
1. Creating threads is a cost (space overhead, time overhead), the main cost of iOS includes: Kernel data structure (about 1KB), stack space (sub-thread 512KB, main thread 1MB, can also use-setstacksize: set, but must be a multiple of 4K, and the minimum is 16K), Creating a thread takes approximately 90 milliseconds to create the time.
If a large number of threads are turned on, the performance of the program is reduced.
The more threads, the greater the overhead of the CPU on the dispatch thread.
2. Programming is more complex: such as communication between threads, multi-threaded data sharing (data security).
Main thread:
After an iOS program runs, the default is to turn on 1 threads, called the "main thread" or "UI thread."
Main thread function: Display \ Refresh UI interface, handle UI events (such as click events, scrolling events, drag events, etc.).
Main thread Usage Note: Do not put more time-consuming operations into the main thread, time-consuming operation will be stuck in the main thread, seriously affect the smoothness of the UI, give users a "card" bad experience.
Child Threads: Place time-consuming actions on child threads (background threads, non-main thread).
////VIEWCONTROLLER.M//01-Understanding-time-consuming operations////Created by Xiaomage on 16/2/18.//2016 little brother. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //1. Get the main thread, the main thread can not be manually opened, the main thread only one, is the system open,Nsthread *mainthread =[Nsthread Mainthread]; NSLog (@"%@", Mainthread);//<nsthread:0x608000066140>{number = 1, name = main}//2. Get the current thread, and the default is the main thread if no threads are turned on. Nsthread *currentthread =[Nsthread CurrentThread]; NSLog (@"%@", CurrentThread);//<nsthread:0x608000066140>{number = 1, name = main}//3. Determine the main thread//3.1 Number = = 1//Class 3.2 MethodsBOOL Ismainthreada =[Nsthread Ismainthread]; //3.3 Object MethodsBOOL ismainthreadb =[CurrentThread Ismainthread]; NSLog (@"%ZD---%zd", ISMAINTHREADA,ISMAINTHREADB);// the}-(Ibaction) Btnclick: (ID) Sender {//The Click event of a button is the main thread processing. for(Nsinteger i =0; I <10000; i++) {NSLog (@"%zd----%@", I,[nsthread CurrentThread]);//The main thread entered this method, time-consuming, the main thread out of the way, the outside of the other UI control can not be clicked. } }@end
ios29--Multithreading