iOS 多线程GCD

今天回顾一下GCD多线程的使用。

在GCD中有两个非常重要的概念:任务和队列。

任务有两种执行方式: 同步执行 和 异步执行,二者的区别是是否会创建新的线程。

同步任务(同步操作):dispatch_sync

异步任务(异步操作):dispatch_async


队列用于存放任务,队列有三种:串行队列、并发队列、主队列

下面就分别列举下这几种任务和队列的组合


1.异步操作 + 串行队列

/**
 * 异步操作 + 串行队列
 * result:开启一个子线程 顺序执行
 */
- (void)asyncandserial {
    
    // 创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"one--%@",[NSThread currentThread]);
        }
    });
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"two--%@",[NSThread currentThread]);
        }
    });
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"three--%@",[NSThread currentThread]);
        }
    });
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"four--%@",[NSThread currentThread]);
        }
    });
    
}
打印LOG



结果:开启一个子线程 顺序执行


2.异步操作 + 并发队列

/**
 * 异步操作 + 并发队列
 * result:开启多个子线程,并发执行
 */
- (void)asyncandconcurrent {
    
    // 创建全局并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"one--%@",[NSThread currentThread]);
        }
    });
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"two--%@",[NSThread currentThread]);
        }
    });
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"three--%@",[NSThread currentThread]);
        }
    });
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"four--%@",[NSThread currentThread]);
        }
    });

}
打印LOG


结果:开启多个子线程,并发执行

3.异步操作 + 主队列

/**
 * 异步操作 + 主队列
 * result:在主线程 顺序执行
 */
- (void)asyncandmain {
    
    // 创建主线程
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"one--%@",[NSThread currentThread]);
        }
    });
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"two--%@",[NSThread currentThread]);
        }
    });
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"three--%@",[NSThread currentThread]);
        }
    });
    
    // 开启异步操作
    dispatch_async(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"four--%@",[NSThread currentThread]);
        }
    });
}

打印LOG


结果:在主线程 顺序执行


4.同步操作 + 串行队列

/**
 * 同步操作 + 串行队列
 * result:主线程 顺序执行
 */
- (void)syncandserial {
    
    // 创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"one--%@",[NSThread currentThread]);
        }
    });
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"two--%@",[NSThread currentThread]);
        }
    });
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"three--%@",[NSThread currentThread]);
        }
    });
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"four--%@",[NSThread currentThread]);
        }
    });

}
打印LOG


结果:在主线程 顺序执行


5.同步操作 + 并发队列

/**
 * 同步操作 + 并发队列
 * result:主线程 顺序执行
 */
- (void)syncandconcurrent {
    
    // 创建并发队列
    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"one--%@",[NSThread currentThread]);
        }
    });
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"two--%@",[NSThread currentThread]);
        }
    });
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"three--%@",[NSThread currentThread]);
        }
    });
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"four--%@",[NSThread currentThread]);
        }
    });
}


打印LOG


结果:在主线程 顺序执行


6.同步操作 + 主队列

/**
 * 同步操作 + 主队列
 */
- (void)syncandmain {
    
    // 创建主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"one--%@",[NSThread currentThread]);
        }
    });
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"two--%@",[NSThread currentThread]);
        }
    });
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"three--%@",[NSThread currentThread]);
        }
    });
    
    // 开启同步操作
    dispatch_sync(queue, ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            NSLog(@"four--%@",[NSThread currentThread]);
        }
    });
}


编译结果报错:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值