ios讲解迷惑深入浅出之GCD

来源:互联网 发布:国泰安数据库好用吗 编辑:程序博客网 时间:2024/06/04 17:46
一. 创建串行对列(默认的和自定义的)
#pragma mark ----- 创建串行队列  (鸡肋)- (void)serialQueue{    // ===== 串行队列分两种(第1种. 主线程中的串行队列(主串行队列)   第2种. 自定义的串行队列)    /*************    第1种. 主线程中的串行队列(主串行队列)      *****************************************/        // 第1种. 主线程中的串行队列(主串行队列)    // dispatch_queue_GCD中表示一个队列    dispatch_queue_t mainQueue = dispatch_get_main_queue();  // 现在创建一个主队列, 实际上就是把主线程取出来了        // 往主队列中添加任务   // <#dispatch_queue_t queue#>  参数一: 要添加任务的队列    // <#^(void)block#>           参数二: 要执行的任务         //  往串行队列中添加任务1    dispatch_async(mainQueue, ^{        NSLog(@"第一个任务, 所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);    });            // 任务2    dispatch_async(mainQueue, ^{         NSLog(@"第 二 个任务, 所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);    });        // 任务3    dispatch_async(mainQueue, ^{         NSLog(@"第 三 个任务, 所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);    });    */                    /*************  第2种. 自定义的串行队列    ***********************************************/        // 第2种. 自定义的串行队列    // <#const char *label#>            参数一: 队列的标识符(注意: 是C语言的字符串 不要加@"")    // <#dispatch_queue_attr_t attr#>   参数二: 队列的执行类型(串/并)    // DISPATCH_QUEUE_SERIAL         代表串行的队列    dispatch_queue_t myQueue = dispatch_queue_create("com.nn.www", DISPATCH_QUEUE_SERIAL);       // 往自定义的串行上队列上添加任务    dispatch_async(myQueue, ^{        NSLog(@"(自定义) 第 一 个任务, 所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);    });        // 添加任务2    dispatch_async(myQueue, ^{         NSLog(@"(自定义) 第 二 个任务, 所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);    });            // 添加任务3    dispatch_async(myQueue, ^{        NSLog(@"(自定义) 第 三 个任务, 所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);    });        /*     // number 不是1 就不是主线程, 是1就是主线程     (自定义) 第 一 个任务, 所在线程:<NSThread: 0x7ff63862a780>{number = 2, name = (null)}, 是否是主线程:0     (自定义) 第 二 个任务, 所在线程:<NSThread: 0x7ff63862a780>{number = 2, name = (null)}, 是否是主线程:0     (自定义) 第 三 个任务, 所在线程:<NSThread: 0x7ff63862a780>{number = 2, name = (null)}, 是否是主线程:0     */              /**        延迟回到主线程执行     *  @param int64_t  <#delayInSeconds#>  延迟几秒的执行        dispatch_get_main_queue(),  延迟几秒后回到主线程执行任务     */    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        NSLog(@"延迟五秒执行");        NSLog(@"延迟任务, 所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);        /*         延迟任务, 所在线程:<NSThread: 0x7fdf40d27fd0>{number = 1, name = main}, 是否是主线程:1         */    });        /**     *  延迟执行     *     *  @param when#>  dispatch_time(DISPATCH_TIME_NOW, 6ull *NSEC_PER_SEC) description#>     *  @param queue#> 自定义的队列 description#>     */    // dispatch_after(<#dispatch_time_t when#>, <#dispatch_queue_t queue#>, <#^(void)block#>)    // 延迟执行    //  <#dispatch_time_t when#>    // C语言中, ull是字面量, 表示unsigned long类型        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6ull *NSEC_PER_SEC), myQueue, ^{        NSLog(@"延迟6秒执行");    });   }

二. 创建并行对列(默认的和自定义的)

<span style="font-size:14px;">#pragma mark - 并行队列--------- 并发执行任务 (只有一块开始, 但是无法控制一块结束)- (void)concurrentQueue{    /******************** 创建并行对列 开始*****************************************************/       // 创建默认的并行对列       // 参数一: 队列的执行顺序       <#long identifier#>    // 参数二: 预留参数, 一般填0    <#unsigned long flags#>    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);        // 添加任务1    dispatch_async(queue, ^{        for (int i = 0; i < 10 ; i++) {            NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d ---- %d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });            // 添加任务2    dispatch_async(queue, ^{        for (int i = 10; i < 20 ; i++) {            NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d ---- %d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });            // 添加任务3    dispatch_async(queue, ^{        for (int i = 20; i < 30; i++) {            NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d ---- %d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });    *//********************* 创建并行对列 结束****************************************************/                    /****************  自定义并行队列  开始 ***************************************************///  2.自定义并行队列,需要自己手动创建,并设置队列为并行        // 参数一 : 队列标识符(C语言的字符串)    // 参数二 : 执行顺序(并/串)    // DISPATCH_QUEUE_CONCURRENT 代表并行队列    dispatch_queue_t myQueue = dispatch_queue_create("com.nn.www", DISPATCH_QUEUE_CONCURRENT);            // 往队列中 添加任务1    dispatch_async(myQueue, ^{        for (int i = 0; i < 10 ; i++) {            NSLog(@"(自定义并行队列)第一个任务,所在线程:%@, 是否是主线程:%d ---- %d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }                });            // 添加任务2    dispatch_async(myQueue, ^{        for (int i = 10; i < 20 ; i++) {            NSLog(@"(自定义并行队列)第二个任务,所在线程:%@, 是否是主线程:%d ---- %d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });            // 添加任务3    dispatch_async(myQueue, ^{        for (int i = 20; i < 30; i++) {            NSLog(@"(自定义并行队列)第三个任务,所在线程:%@, 是否是主线程:%d ---- %d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });/****************  自定义并行队列  结束 ***************************************************/}</span><span style="font-size: 18px;"></span>





0 0