浅析多线程GCD

GCD的基本使用代码

// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

// ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

#pragma mark --------------------
#pragma mark Events
/*
队列,都是用来存放任务
并发队列:允许多个任务同时执行
    1)直接create dispatch_queue_create
    2)全局并发队列
 
串行队列:只能一个接着一个的执行
    1)直接create dispatch_queue_create
    2)主队列:
        1)所有在主队列中的任务都会被放在主线程中执行
        2)主队列中的任务在执行之前会先检查主线程的状态,
          如果发现主线程当前正在执行任务那么会暂停队列中任务的调度
 同步:必须要得到该方法的返回值才能够继续往下执行--->如果我没有执行完毕,那么后面的将永远无法执行
 异步:可以继续往下执行,等前面的任务执行完毕之后再回头执行-->我无所谓,你可以先执行后面的代码
 */

// 同步:只能在当前线程中执行任务,不具备开启新线程的能力
// 异步:可以在新的线程中执行任务,具备开启新线程的能力
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    异步函数+主队列
//    [self asyncMian];
//    同步函数+主队列
//    [self syncMian];
//    异步函数+并发队列
//    [self asyncConCurrent];
//    异步函数+串行队列
//    [self asyncSerial];
    // 同步函数+并发队列
//    [self syncConCurrent];
    // 同步函数+串行
    [self syncSerial];
}

#pragma mark --------------------
#pragma mark - Methods
// 异步函数+主队列:不会开线程,所有的任务串行执行,在主线程执行
/*
 1---<NSThread: 0x600000077f80>{number = 1, name = main}
 2---<NSThread: 0x600000077f80>{number = 1, name = main}
 3---<NSThread: 0x600000077f80>{number = 1, name = main}
 4---<NSThread: 0x600000077f80>{number = 1, name = main}
 */
- (void)asyncMian {
    // 获得主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    // 封装任务
    dispatch_async(queue, ^{
        NSLog(@"1---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"2---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"3---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"4---%@", [NSThread currentThread]);
    });
}

// 同步函数+主队列:
// 会死锁
- (void)syncMian {
    // 获得主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    NSLog(@"%s",__func__);
    // 封装任务
    dispatch_sync(queue, ^{
        NSLog(@"1---%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"2---%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"3---%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"4---%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"5---%@",[NSThread currentThread]);
    });
}

// 异步函数+并发队列
/*
// 会不会开线程:会
// 如果开线程,那么开几条?多条(并不是有几个任务就开几条线程)
// 队列内部任务如何执行:并发
 */

/*
 start------
 end------
 2---<NSThread: 0x600000074440>{number = 4, name = (null)}
 1---<NSThread: 0x608000071040>{number = 3, name = (null)}
 3---<NSThread: 0x600000076dc0>{number = 5, name = (null)}
 */
- (void)asyncConCurrent {
    //1.创建队列•
    /*
     第一个参数:标签 队列的名称 C语言
     第二个参数:队列的类型
     */
    //dispatch_queue_t queue = dispatch_queue_create("com.seemygo.www.download", DISPATCH_QUEUE_CONCURRENT);
    
    // 获得全局并发队列
    /*
     第一个参数:优先级
     第二个参数:留给未来使用的0
     */
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    NSLog(@"start------");
    // 2.使用异步函数添加操作到队列中
    // 该方法完成了以下操作:1)封装任务 2)把任务添加到队列
    dispatch_async(queue, ^{
        NSLog(@"1---%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"2---%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"3---%@",[NSThread currentThread]);
    });

    NSLog(@"end------");
}

// 异步函数+串行队列
/*
//会不会开线程:会
//如果开线程,那么开几条?1
//队列内部任务如何执行:串行
 */
/*
 start------
 end------
 1---<NSThread: 0x60000026bb00>{number = 3, name = (null)}
 2---<NSThread: 0x60000026bb00>{number = 3, name = (null)}
 3---<NSThread: 0x60000026bb00>{number = 3, name = (null)}
 4---<NSThread: 0x60000026bb00>{number = 3, name = (null)}
 */
-(void)asyncSerial {
    //1.创建队列
    /*
     第一个参数:标签 队列的名称 C语言
     第二个参数:队列的类型
     */
    dispatch_queue_t queue = dispatch_queue_create("com.seemygo.www.download", DISPATCH_QUEUE_SERIAL);
    NSLog(@"start------");
    //2.使用异步函数添加操作到队列中
    //该方法完成了以下操作:1)封装任务 2)把任务添加到队列
    dispatch_async(queue, ^{
        NSLog(@"1---%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"2---%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"3---%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"4---%@",[NSThread currentThread]);
    });
    
    NSLog(@"end------");
}

// 同步函数+并发队列
/*
//会不会开线程:不会
//如果开线程,那么开几条 X
//队列内部任务如何执行:串行
 */
/*
 start------
 1---<NSThread: 0x600000063740>{number = 1, name = main}
 2---<NSThread: 0x600000063740>{number = 1, name = main}
 3---<NSThread: 0x600000063740>{number = 1, name = main}
 4---<NSThread: 0x600000063740>{number = 1, name = main}
 end------
 */
- (void)syncConCurrent {
    //1.创建队列
    /*
     第一个参数:标签 队列的名称 C语言
     第二个参数:队列的类型
     */
    dispatch_queue_t queue = dispatch_queue_create("com.seemygo.www.download", DISPATCH_QUEUE_CONCURRENT);
    
     NSLog(@"start------");
    //2.使用同步函数添加操作到队列中
    //该方法完成了以下操作:1)封装任务 2)把任务添加到队列
    dispatch_sync(queue, ^{
        NSLog(@"1---%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"2---%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"3---%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"4---%@",[NSThread currentThread]);
    });
    
     NSLog(@"end------");
}

// 同步函数+串行
/*
//会不会开线程:不会
//如果开线程,那么开几条 X
//队列内部任务如何执行:串行
 */
/*
 start------
 1---<NSThread: 0x60000007cb00>{number = 1, name = main}
 2---<NSThread: 0x60000007cb00>{number = 1, name = main}
 3---<NSThread: 0x60000007cb00>{number = 1, name = main}
 4---<NSThread: 0x60000007cb00>{number = 1, name = main}
 end------
 */
-(void)syncSerial
{
    //1.创建队列
    /*
     第一个参数:标签 队列的名称 C语言
     第二个参数:队列的类型
     */
    dispatch_queue_t queue = dispatch_queue_create("com.seemygo.www.download", DISPATCH_QUEUE_SERIAL);
    NSLog(@"start------");
    //2.使用同步函数添加操作到队列中
    //该方法完成了以下操作:1)封装任务 2)把任务添加到队列
    dispatch_sync(queue, ^{
        NSLog(@"1---%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"2---%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"3---%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"4---%@",[NSThread currentThread]);
    });
    NSLog(@"end------");
}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容