GCD的基本使用

GCD全称Grand Central Dispatch,是Apple提供的一套底层API,提供了一种新的方法来进行并发程序编写,它的API包含在libdispatch库中.

觉得需要理解GCD中的三个要点:

1.同步异步(sync,async)

sync表示同步,不会开启新线程,任务是在当前线程中执行; async表示异步,具备开启新线程的能力,具体是否开启新线程需要看队列,比如:全局队列,串型队列会开启新线程,主队列就不会开启

2.队列

串型队列:

dispatch_queue_create("HJiang", DISPATCH_QUEUE_SERIAL)

 

并发队列:

dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);

dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) ,其中第一个参数目前有四种选择:

#define DISPATCH_QUEUE_PRIORITY_HIGH 2 优先级搞
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 默认级别 一般开发采用
#define DISPATCH_QUEUE_PRIORITY_LOW (-2) 优先级低
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN 优先级最低

 

主队列:

dispatch_get_main_queue()

 

3.任务

GCD中执行的任务,需要执行的代码写入此处就OK,GCD任务一般情况下都是在block中.

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
//    [self asyncConcurrent];
    [self asyncSerial];
//    [self asyncMain];
    
//    [self syncMain];
//    [self syncSerial];
//    [self syncConcurrent];
    
}

- (void)syncMain{
    // 线程阻塞
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}

- (void)syncSerial{
    // 在当前线程中顺序执行任务
    dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_SERIAL);
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}

- (void)syncConcurrent{
    // 在当前线程中顺序执行任务
    dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}



- (void)asyncMain{
    // Main线程中异步执行任务
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}

- (void)asyncSerial{
    // 开启一条新线程顺序执行任务
    dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}

- (void)asyncConcurrent{
    // 开启多条新线程顺序执行任务
    dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"i=%ld %@",i,[NSThread currentThread]);
        }
    });
    
    NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}

 

 

 

转载于:https://www.cnblogs.com/HJiang/p/7448213.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值