iOS开发之GCD多线程补充

//异步  串行  (偶尔用)开启一条子线程  任务在开启的线程中挨个执行

- (void)asynSerial {
    
    
    dispatch_queue_t queue = dispatch_queue_create("sdf", NULL);
    //将任务添加到串行队列中 异步执行
    
    dispatch_async(queue, ^{
        NSLog(@"%@---妈妈米亚1--%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@---妈妈米亚2--%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@---妈妈米亚3--%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@---妈妈米亚4--%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"%@---妈妈米亚5--%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    
    

}

//2.

//GCD中有一个特殊的队列 人们称它为主队列

//GCD中任务的执行需要借助队列 而队列和任务也是GCD的侧重点  主队列是主线程中的队列 所以不管是异步函数还是同步函数 都不会生成新的子线程 也就是说异步函数在主队列中丧失了开启线程的能力 但是异步执行的时候 线程的任务被提出来等待了 在函数执行完后仍然会执行任务  同步执行就会卡住不执行(等他等我的局面)

- (void)synMainQueue {

    NSLog(@"begin");
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        NSLog(@"nishiwodeai");
    });
    NSLog(@"end");
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2016-03-18 09:53  aRenOuBa  阅读(141)  评论(0编辑  收藏  举报