iOS GCD 队列 线程

时间:2022-11-30 05:22:51
 GCD  队列  线程
<pre name="code" class="objc">//串行方法 全为同步
- (void)serial:(UIButton *)but
{
    NSLog(@"串行开始");
//    2种方法获得串行队列
// 一是 获取mainQueue,mainQueue是程序自带的一个serialQueue,此queue的所有task在主线程执行。缺点容易卡死主线程
//    dispatch_queue_t queue = dispatch_get_main_queue();//获取主线程队列
    
    //二是自己创建serialQueue 自己创建的serialQueue会在子线程执行task
    //GCD 可以非常简单切换线程
    
    //串行队列无并发   所有方法都是同步执行  执行优先级为: 普通 > 同步 > 异步
    
    dispatch_queue_t queue = dispatch_queue_create("com.lanou.myserialQueue", DISPATCH_QUEUE_SERIAL);
    

    dispatch_sync(queue, ^{
        NSLog(@"hello world1 %@--->%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"hello world2 %@--->%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"hello world3 %@--->%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    NSLog(@"串行结束");
}

2015-12-28 11:22:06.874 5-12LessonGCD[6502:103060] 串行开始
2015-12-28 11:22:06.876 5-12LessonGCD[6502:103060] hello world1 <NSThread: 0x7fd983d04910>{number = 1, name = main}--->1
2015-12-28 11:22:06.876 5-12LessonGCD[6502:103060] hello world2 <NSThread: 0x7fd983d04910>{number = 1, name = main}--->1
2015-12-28 11:22:06.876 5-12LessonGCD[6502:103060] hello world3 <NSThread: 0x7fd983d04910>{number = 1, name = main}--->1
2015-12-28 11:22:06.876 5-12LessonGCD[6502:103060] 串行结束


 
 
<pre name="code" class="objc">//串行方法  全为异步
- (void)serial:(UIButton *)but
{
    NSLog(@"串行开始");
//    2种方法获得串行队列
// 一是 获取mainQueue,mainQueue是程序自带的一个serialQueue,此queue的所有task在主线程执行。缺点容易卡死主线程
//    dispatch_queue_t queue = dispatch_get_main_queue();//获取主线程队列
    
    //二是自己创建serialQueue 自己创建的serialQueue会在子线程执行task
    //GCD 可以非常简单切换线程
    
    //串行队列无并发   所有方法都是同步执行  执行优先级为: 普通 > 同步 > 异步
    
    dispatch_queue_t queue = dispatch_queue_create("com.lanou.myserialQueue", DISPATCH_QUEUE_SERIAL);
    

    dispatch_async(queue, ^{
        NSLog(@"hello world1 %@--->%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"hello world2 %@--->%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"hello world3 %@--->%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    NSLog(@"串行结束");
}

2015-12-28 10:58:14.371 5-12LessonGCD[5020:79140] 串行开始2015-12-28 10:58:14.371 5-12LessonGCD[5020:79140] 串行结束2015-12-28 10:58:14.372 5-12LessonGCD[5020:79180] hello world1 <NSThread: 0x7fa76061afd0>{number = 3, name = (null)}--->02015-12-28 10:58:14.373 5-12LessonGCD[5020:79180] hello world2 <NSThread: 0x7fa76061afd0>{number = 3, name = (null)}--->02015-12-28 10:58:14.373 5-12LessonGCD[5020:79180] hello world3 <NSThread: 0x7fa76061afd0>{number = 3, name = (null)}--->0
 
//串行方法 情况3 同步 异步皆有
- (void)serial:(UIButton *)but
{
    NSLog(@"串行开始");
//    2种方法获得串行队列
// 一是 获取mainQueue,mainQueue是程序自带的一个serialQueue,此queue的所有task在主线程执行。缺点容易卡死主线程
//    dispatch_queue_t queue = dispatch_get_main_queue();//获取主线程队列
    
    //二是自己创建serialQueue 自己创建的serialQueue会在子线程执行task
    //GCD 可以非常简单切换线程
    
    //串行队列无并发   所有方法都是同步执行  
    
    dispatch_queue_t queue = dispatch_queue_create("com.lanou.myserialQueue", DISPATCH_QUEUE_SERIAL);
    

    dispatch_async(queue, ^{
        NSLog(@"hello world1 %@--->%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"hello world2 %@--->%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"hello world3 %@--->%d",[NSThread currentThread], [NSThread isMainThread]);
    });
    NSLog(@"串行结束");
}<pre name="code" class="objc">2015-12-28 11:12:16.597 5-12LessonGCD[5956:93193] 串行开始
2015-12-28 11:12:16.599 5-12LessonGCD[5956:93235] hello world1 <NSThread: 0x7f8b80e06cd0>{number = 2, name = (null)}--->0
2015-12-28 11:12:16.599 5-12LessonGCD[5956:93193] hello world2 <NSThread: 0x7f8b80c02520>{number = 1, name = main}--->1
2015-12-28 11:12:16.599 5-12LessonGCD[5956:93193] 串行结束
2015-12-28 11:12:16.599 5-12LessonGCD[5956:93235] hello world3 <NSThread: 0x7f8b80e06cd0>{number = 2, name = (null)}--->0


</pre><br /><pre name="code" class="objc">//  后台执行:
 dispatch_async(dispatch_get_global_queue(0, 0), ^{
      // something
 });

 // 主线程执行:
 dispatch_async(dispatch_get_main_queue(), ^{
      // something
 });

 // 一次性执行:
 static dispatch_once_t onceToken;
 dispatch_once(&onceToken, ^{
     // code to be executed once
 });

 // 延迟2秒执行:
 double delayInSeconds = 2.0;
 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
     // code to be executed on the main queue after delay
 });

 // 自定义dispatch_queue_t
 dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL);
 dispatch_async(urls_queue, ^{  
   // your code 
 });
 dispatch_release(urls_queue);

 // 合并汇总结果
 dispatch_group_t group = dispatch_group_create();
 dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
      // 并行执行的线程一
 });
 dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
      // 并行执行的线程二
 });
 dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
      // 汇总结果
 });