I. Processes and Threads
1. What is a process: a process is an application that is running in the system
Each process is independent, and each process runs within its dedicated and protected memory space
2. What is a thread
1 processes to perform a task, a thread must be wired (at least 1 threads per 1 processes)
Threads are the basic unit of execution of a process, and all tasks of a process (program) are executed in the thread
Second, multi-threading
1. What is Multithreading: Multiple threads can be opened in a process, each thread can perform different tasks in parallel (at the same time)
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
3. 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)
4. Advantages and disadvantages of multithreading
Advantages of multithreading
1. The efficiency of the implementation of the procedure can be improved appropriately
2. Can appropriately improve resource utilization (CPU, memory utilization)
Disadvantages of multithreading
1. Open threads need to occupy a certain amount of memory space (by default, the main thread takes up 1M, 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
2. The more threads, the greater the overhead of the CPU on the dispatch thread
3. Programming is more complex: such as communication between threads, multi-threaded data sharing
5. Multithreading in the development of iOS application
1> 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 2> primary thread:
Display \ Refresh UI interface
Handling UI events (such as click events, scrolling events, drag events, etc.)
3> Main thread use 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
6. Code Examples
2// YYVIEWCONTROLLER.M 3// 01-Block main thread 4//5// Created by Apple on 14-6-23.6// Copyright (c) 2014 ITCA Se. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller ()-(ibaction) btnclick;13 @end14 @impl Ementation YYViewController17-(void) VIEWDIDLOAD20 { [super viewdidload];22}23 24 25//button click Event-(ibaction ) Btnclick { //1. Gets the current thread nsthread *current=[nsthread currentthread];29 //2. Use a For loop to perform some time-consuming operations for (int i=0; i<10000; i++) { //3. Output thread NSLog (@ "Btnclick---%d---%@", i,current); }34}35 @end
One: Multithreading--Multithreading simple introduction