1. Process: An application that is running in the system
Threads: A process must be wired to perform tasks (at least one process per process)
2. Main thread: Display, refresh UI interface, handle UI events, interact with the main thread of the user
two. Several ways of multithreading (interview frequently asked)
1.Pthread: A set of common multi-threaded API, used for Unix\linux\window and other systems, cross-platform, the use of difficult, using C language, programmers manage their own life cycle.
2.NSthread: Using more object-oriented, easy to use, can directly manipulate thread objects, OC language, programmer semi-management, only with management to create, not worry about release.
3.GCD: Designed to replace threading technology such as Nsthread, leveraging the multi-core of the device to automatically manage the lifecycle
4.NSOperation: Based on gcd (bottom is gcd), more than gcd more simple and practical functions, using more object-oriented, automatic management lifecycle.
three. Thread Usage
-
- Create Pthread
- Create a new thread as soon as it is created
The system automatically calls the incoming function in the child thread
/*第一个参数: 线程的代号(当做就是线程)第二个参数: 线程的属性第三个参数: 指向函数的指针, 就是将来线程需要执行的方法第四个参数: 给第三个参数的指向函数的指针 传递的参数void *(*functionP)(void *)void * == id一般情况下C语言中的类型都是以 _t或者Ref结尾*/pthread_t threadId;// 只要create一次就会创建一个新的线程pthread_create(&threadId , NULL, &demo, "lnj");
- Nsthread
- Several ways to create
Mutual exclusion Lock
- Application scenario: Multi-threaded resource looting (multiple threads accessing a file/variable at the same time)
- Note the point:
-
- As long as the shackles will consume performance
-
- If you want to really lock the code, then multiple threads must use the same lock.
-
- When locking, try to narrow the range, because the larger the range, the lower the performance.
- Tip: If you remember a locked word quickly
- [Nsuserdefaults Standarduserdefaults] Synchronize method of rapid memory
@synchronized(self) { // 需要被锁定的代码 }
- Nsthread Inter-thread communication
- GCD
-
Tasks and queues
- tasks: What to do
- queues: Used to hold tasks
-
How to perform a task
- synchronization function Dispatch_sync
- does not have the ability to open a new thread
- async function Dispatch_async
has the ability to open new threads
- Synchronous and asynchronous primary impact: Can I open a new thread
- GCD Inter-thread communication
- Performing tasks with asynchronous functions
- Updating the UI with the home row back to the main thread
- The common methods in GCD
- Deferred execution
// [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO]; // 内部实现原理就是NSTimer // [self performSelector:@selector(run) withObject:nil afterDelay:2.0]; // 将需要执行的代码, 和方法放在一起, 提高代码的阅读性 // 相比NSTimer来说, GCD的延迟执行更加准确 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSLog(@"%@", [NSThread currentThread]); NSLog(@"run");
- Disposable code
- During the entire program run, only one time is executed
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"我被执行了"); });
/* 第一个参数: 需要执行几次任务 第二个参数: 队列 第三个参数: 当前被执行到得任务的索引 */ /* dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) { NSLog(@"%@, %zd",[NSThread currentThread] , index); }); */
Barrier
- To perform all previous tasks before executing barrier, you must meet two conditions
-
- All tasks are in the same queue
-
- The queue cannot be a global parallel queue, it must be a queue created by itself
- The tasks added before the barrier method are executed first, and only the tasks that are added before the barrier method are executed barrier
- And if the task is added after the barrier method, it must wait until the barrier method finishes executing
Group
- If you want to implement, wait for all the previous tasks to complete, and then perform a specific task, then you can gcd the middle-aged group to achieve
- As long as all the tasks in the current group have been executed, the system will automatically call Dispatch_group_notify
Multi-threading, easy to handle multithreading