对于多线程的理解:
多线程: 利用多核处理器的优势,实现多个操作的并发执行,使系统资源得到充分利用,提高运行效率,减少主线程的工作量,将非UI层面的耗时操作放在其他线程中执行,使得UI流畅,交互响应迅速。
几个概念:
进程:
1、系统中运行的一个应用程序
2、进程是系统资源分配和调度的基本单位
3、系统中或许有多个进程,进程间相互独立,有各自的空间,但是共享系统的资源
线程:
1、一个进程至少有一个线程,称为主线程
2、进程中的所有任务都是在线程中执行的
3、进程中的多个线程共享进程中的资源
多线程:
一个进程中开启多条线程,每个线程负责执行一个任务,这样就提高了运行效率
多线程原理:
1、同一时间,CPU只能处理一条线程,只能有一个线程工作
2、多线程的并发执行,实际上是CPU在多条线程之间来回切换
3、CPU快速的来回切换,就造成了很多线程并发执行的假象
任务:
1、任务分为同步任务 和 异步任务
2、同步任务不会开启新的线程,而是在当前线程执行
3、异步任务可能会开启新的线程(当前没有空闲线程的情况下)
队列:
1、任务在进入线程执行之前存放在队列里等待
2、队列分为串行队列 和 并行队列
3、串行队列中的任务,不管同步异步,都是串行执行(注意:主队列不管是同步还是异步任务,都不会开启新线程,创建的串行队列,添加异步任务会创建新线程,但是结果都是串行执行)
4、并行队列中的任务,根据同步异步的不同,决定谁先执行,谁继续等待
5、并行队列同步任务,会等待主线程空闲后执行
6、并行队列异步任务,在主线程不阻塞的情况下,在没有空闲线程的情况下,会创建新的线程执行,有空闲线程的情况下就在此空闲线程中执行
7、一个比较特殊的队列:主队列,也是串行队列,主队列就一个可执行任务的线程就是主线程,UI的操作必须放在主线程()中进行,主线程控制着整个应用程序的运行
代码验证:以GCD为例
上半部分 :串行队列
在主队列(串行队列)中添加同步任务
dispatch_sync(dispatch_get_main_queue(), ^{
for (int i=0; i<5; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
});
结论:系统崩溃,因为在主队列开启同步任务时,主队列是串行队列,里面的线程是有顺序的,先执行完一个线程才执行下一个线程。而主队列始终就只有一个主线程,主线程还是无限循环的,是不会执行完毕的,除非关闭应用程序。
因此在主线程开启一个同步任务,同步任务会想抢占执行的资源,而主线程任务一直在执行某些操作,不肯放手。两个的优先级都很高,最终导致死锁,阻塞线程了。
在主队列(串行队列)中添加异步任务
for (int i=-5; i<0; i++) { //主队列 主线程阻塞
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
//添加一个异步任务 , 开启线程
dispatch_async(dispatch_get_main_queue(), ^{
for (int i=5; i<10; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
});
dispatch_async(dispatch_get_main_queue(), ^{
for (int i=10; i<15; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
});
//打印结果:
<NSThread: 0x60000006d480>{number = 1, name = main},====-5
<NSThread: 0x60000006d480>{number = 1, name = main},====-4
<NSThread: 0x60000006d480>{number = 1, name = main},====-3
<NSThread: 0x60000006d480>{number = 1, name = main},====-2
<NSThread: 0x60000006d480>{number = 1, name = main},====-1
<NSThread: 0x60000006d480>{number = 1, name = main},====5
<NSThread: 0x60000006d480>{number = 1, name = main},====6
<NSThread: 0x60000006d480>{number = 1, name = main},====7
<NSThread: 0x60000006d480>{number = 1, name = main},====8
<NSThread: 0x60000006d480>{number = 1, name = main},====9
<NSThread: 0x60000006d480>{number = 1, name = main},====10
<NSThread: 0x60000006d480>{number = 1, name = main},====11
<NSThread: 0x60000006d480>{number = 1, name = main},====12
<NSThread: 0x60000006d480>{number = 1, name = main},====13
<NSThread: 0x60000006d480>{number = 1, name = main},====14
结论:1、串行队列 顺序执行 2、主队列的串行队列,即使添加异步任务也不会开启子线程
在自定义的串行队列中添加同步任务
dispatch_queue_t serialQueue = dispatch_queue_create("chuanxingduilie",
DISPATCH_QUEUE_SERIAL);
for (int i=-5; i<0; i++) { //主队列 主线程阻塞
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
// 串行队列
dispatch_sync(serialQueue, ^{
for (int i=0; i<5; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
});
打印结果:
<NSThread: 0x60000007afc0>{number = 1, name = main},====-5
<NSThread: 0x60000007afc0>{number = 1, name = main},====-4
<NSThread: 0x60000007afc0>{number = 1, name = main},====-3
<NSThread: 0x60000007afc0>{number = 1, name = main},====-2
<NSThread: 0x60000007afc0>{number = 1, name = main},====-1
<NSThread: 0x60000007afc0>{number = 1, name = main},====0
<NSThread: 0x60000007afc0>{number = 1, name = main},====1
<NSThread: 0x60000007afc0>{number = 1, name = main},====2
<NSThread: 0x60000007afc0>{number = 1, name = main},====3
<NSThread: 0x60000007afc0>{number = 1, name = main},====4
结论:只要是串行队列中添加同步任务,都不会开启子线程,都是在主线程中执行
在自定义的串行队列中添加异步任务
dispatch_queue_t serialQueue = dispatch_queue_create("chuanxingduilie", DISPATCH_QUEUE_SERIAL);
for (int i=-5; i<0; i++) { //主队列 主线程阻塞
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
//串行队列 同步任务 主线程
dispatch_sync(serialQueue, ^{
for (int i=0; i<5; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
});
//串行队列非主队列 异步任务 , 开启线程
dispatch_async(serialQueue, ^{
for (int i=5; i<10; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
});
//串行队列非主队列 异步任务 , 开启线程
dispatch_async(serialQueue, ^{
for (int i=10; i<15; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
});
打印结果:
<NSThread: 0x60000007da80>{number = 1, name = main},====-5
<NSThread: 0x60000007da80>{number = 1, name = main},====-4
<NSThread: 0x60000007da80>{number = 1, name = main},====-3
<NSThread: 0x60000007da80>{number = 1, name = main},====-2
<NSThread: 0x60000007da80>{number = 1, name = main},====-1
<NSThread: 0x60000007da80>{number = 1, name = main},====0
<NSThread: 0x60000007da80>{number = 1, name = main},====1
<NSThread: 0x60000007da80>{number = 1, name = main},====2
<NSThread: 0x60000007da80>{number = 1, name = main},====3
<NSThread: 0x60000007da80>{number = 1, name = main},====4
<NSThread: 0x600000269a40>{number = 3, name = (null)},====5
<NSThread: 0x600000269a40>{number = 3, name = (null)},====6
<NSThread: 0x600000269a40>{number = 3, name = (null)},====7
<NSThread: 0x600000269a40>{number = 3, name = (null)},====8
<NSThread: 0x600000269a40>{number = 3, name = (null)},====9
<NSThread: 0x600000269a40>{number = 3, name = (null)},====10
<NSThread: 0x600000269a40>{number = 3, name = (null)},====11
<NSThread: 0x600000269a40>{number = 3, name = (null)},====12
<NSThread: 0x600000269a40>{number = 3, name = (null)},====13
<NSThread: 0x600000269a40>{number = 3, name = (null)},====14
结论:1、非主队列串行队列,添加异步任务会创建子线程 2、异步任务也可能不会开启新的子线程,如果有空闲线程的情况下(如代码中第二个异步任务)
3、串行队列任务都是顺序执行,不管是同步任务还是异步任务
下半部分:并行队列
在GCD全局队列global队列(并行队列)中开启同步任务
for (int i=-5; i<0; i++) { //主队列 主线程阻塞
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
//添加一个同步任务 , 不开启线程,在主线程中执行,等待主线程空闲后 才执行此处
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
for (int i=5; i<10; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
});
打印结果:
<NSThread: 0x60000006a240>{number = 1, name = main},====-5
<NSThread: 0x60000006a240>{number = 1, name = main},====-4
<NSThread: 0x60000006a240>{number = 1, name = main},====-3
<NSThread: 0x60000006a240>{number = 1, name = main},====-2
<NSThread: 0x60000006a240>{number = 1, name = main},====-1
<NSThread: 0x60000006a240>{number = 1, name = main},====5
<NSThread: 0x60000006a240>{number = 1, name = main},====6
<NSThread: 0x60000006a240>{number = 1, name = main},====7
<NSThread: 0x60000006a240>{number = 1, name = main},====8
<NSThread: 0x60000006a240>{number = 1, name = main},====9
结论:在并行队列中,添加同步任务不会开启新的线程,仍是在主线程中执行
在GCD全局队列global队列中开启异步任务
for (int i=-5; i<0; i++) { //主队列 主线程阻塞
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
//开启子线程,与主线程并发执行
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (int i=10; i<15; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
});
//添加一个同步任务 , 不开启线程
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
for (int i=5; i<10; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
});
打印结果:
<NSThread: 0x600000078ac0>{number = 1, name = main},====-5
<NSThread: 0x600000078ac0>{number = 1, name = main},====-4
<NSThread: 0x600000078ac0>{number = 1, name = main},====-3
<NSThread: 0x600000078ac0>{number = 1, name = main},====-2
<NSThread: 0x600000078ac0>{number = 1, name = main},====-1
<NSThread: 0x600000078ac0>{number = 1, name = main},====5
<NSThread: 0x600000262740>{number = 3, name = (null)},====10
<NSThread: 0x600000078ac0>{number = 1, name = main},====6
<NSThread: 0x600000262740>{number = 3, name = (null)},====11
<NSThread: 0x600000078ac0>{number = 1, name = main},====7
<NSThread: 0x600000262740>{number = 3, name = (null)},====12
<NSThread: 0x600000078ac0>{number = 1, name = main},====8
<NSThread: 0x600000078ac0>{number = 1, name = main},====9
<NSThread: 0x600000262740>{number = 3, name = (null)},====13
<NSThread: 0x600000262740>{number = 3, name = (null)},====14
结论: 1、 并行队列,线程可以并发执行 2、并行队列 添加同步任务,任务会排在主线程后面等待主线程空闲
总结:
1、主队列是特殊的串行队列,只有一个主线程,主线程在阻塞的前提下,后面所有的并发还是串行的操作都必须等待主线程空闲
2、不管是什么队列,同步任务绝不会开启新的线程,只能在主线程后排队,优先级较高,由此看来,主线程中的任务不一定来自主队列,但主队列的任务一定在主线程执行
3、异步任务,优先级低,在线程中没有执行顺序,主要看CPU闲不闲,在主队列中即使异步任务也不会开启新线程,但是在其他队列中会开启新的线程
NSOperationQueue
NSOperationQueue同样是多线程实现的主要方式,底层依旧是GCD,只不过本身更加面向对象
1、主队列 [NSOperationQueue mainQueue];
2、自定义队列 ,不设置maxConcurrentOperationCount 最大并发数的情况下,系统会根据CPU和资源情况决定队列的最大并发数,如果设置maxConcurrentOperationCount=1,就说明此队列是串行队列,
NSOperationQueue *serialQueue = [[NSOperationQueue alloc]init];
serialQueue.maxConcurrentOperationCount = 1;
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testInvocation) object:nil];
[serialQueue addOperation:operation];
[serialQueue addOperationWithBlock:^{
for (int i=5; i<10; i++) {
NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
}
}];
3、另外NSOperationQueue的任务类:NSBlockOperation和NSInvocationOperation都是异步任务,没有同步任务,当然,向串行队列中添加异步任务,同样可是达到串行的效果