美文网首页
小驴拉磨之iOS进阶-多线程(6)—— GCD 并发队列

小驴拉磨之iOS进阶-多线程(6)—— GCD 并发队列

作者: 小驴拉磨 | 来源:发表于2017-11-16 14:37 被阅读8次
11111.jpg

并发队列,异步执行

//MARK:并发队列,异步执行
-(void)GCDSerialQueuesfour{
    
    //1.队列-并发 DISPATCH_QUEUE_CONCURRENT 并发
    dispatch_queue_t  q = dispatch_queue_create("cc_queue",DISPATCH_QUEUE_CONCURRENT);
    
    //2.异步执行任务
    for(int i = 0;i < 10;i++)
    {
        dispatch_async(q,^{
            NSLog(@"%@ %d",[NSThread currentThread],i);
        });
    }
    
    NSLog(@"come here");
    
}
  • 打印结果

2017-11-16 14:25:17.299034+0800 GCDDome-串行队列[4319:2567001] come here
2017-11-16 14:25:17.299172+0800 GCDDome-串行队列[4319:2567027] <NSThread: 0x1c4264680>{number = 3, name = (null)} 0
2017-11-16 14:25:17.299226+0800 GCDDome-串行队列[4319:2567027] <NSThread: 0x1c4264680>{number = 3, name = (null)} 2
2017-11-16 14:25:17.299258+0800 GCDDome-串行队列[4319:2567027] <NSThread: 0x1c4264680>{number = 3, name = (null)} 3
2017-11-16 14:25:17.299299+0800 GCDDome-串行队列[4319:2567029] <NSThread: 0x1c0661f00>{number = 4, name = (null)} 1
2017-11-16 14:25:17.299313+0800 GCDDome-串行队列[4319:2567027] <NSThread: 0x1c4264680>{number = 3, name = (null)} 4
2017-11-16 14:25:17.299342+0800 GCDDome-串行队列[4319:2567027] <NSThread: 0x1c4264680>{number = 3, name = (null)} 5
2017-11-16 14:25:17.299378+0800 GCDDome-串行队列[4319:2567027] <NSThread: 0x1c4264680>{number = 3, name = (null)} 6
2017-11-16 14:25:17.299403+0800 GCDDome-串行队列[4319:2567027] <NSThread: 0x1c4264680>{number = 3, name = (null)} 7
2017-11-16 14:25:17.299427+0800 GCDDome-串行队列[4319:2567027] <NSThread: 0x1c4264680>{number = 3, name = (null)} 8
2017-11-16 14:25:17.300349+0800 GCDDome-串行队列[4319:2567029] <NSThread: 0x1c0661f00>{number = 4, name = (null)} 9

  • 总结
会开多条线程,线程数量是由GCD来决定
不会顺序执行
come here的位置不确定,一般会靠上的位置

并发队列,同步执行

//MARK:并发队列,同步执行
-(void)GCDSerialQueuesFive{
    
    //1.队列-并发 DISPATCH_QUEUE_CONCURRENT 并发
    dispatch_queue_t  q = dispatch_queue_create("cc_queue",DISPATCH_QUEUE_CONCURRENT);
    
    //2.同步执行任务
    for(int i = 0;i < 10;i++)
    {
        dispatch_sync(q,^{
            NSLog(@"%@ %d",[NSThread currentThread],i);
        });
    }
    
    NSLog(@"come here");
    
}
  • 打印结果

2017-11-16 14:30:00.932762+0800 GCDDome-串行队列[4322:2569434] <NSThread: 0x1c0074ac0>{number = 1, name = main} 0
2017-11-16 14:30:00.932816+0800 GCDDome-串行队列[4322:2569434] <NSThread: 0x1c0074ac0>{number = 1, name = main} 1
2017-11-16 14:30:00.932840+0800 GCDDome-串行队列[4322:2569434] <NSThread: 0x1c0074ac0>{number = 1, name = main} 2
2017-11-16 14:30:00.932862+0800 GCDDome-串行队列[4322:2569434] <NSThread: 0x1c0074ac0>{number = 1, name = main} 3
2017-11-16 14:30:00.932883+0800 GCDDome-串行队列[4322:2569434] <NSThread: 0x1c0074ac0>{number = 1, name = main} 4
2017-11-16 14:30:00.932904+0800 GCDDome-串行队列[4322:2569434] <NSThread: 0x1c0074ac0>{number = 1, name = main} 5
2017-11-16 14:30:00.932925+0800 GCDDome-串行队列[4322:2569434] <NSThread: 0x1c0074ac0>{number = 1, name = main} 6
2017-11-16 14:30:00.932945+0800 GCDDome-串行队列[4322:2569434] <NSThread: 0x1c0074ac0>{number = 1, name = main} 7
2017-11-16 14:30:00.932969+0800 GCDDome-串行队列[4322:2569434] <NSThread: 0x1c0074ac0>{number = 1, name = main} 8
2017-11-16 14:30:00.933010+0800 GCDDome-串行队列[4322:2569434] <NSThread: 0x1c0074ac0>{number = 1, name = main} 9
2017-11-16 14:30:00.933019+0800 GCDDome-串行队列[4322:2569434] come here

  • 总结
不会开几条线程
会顺序执行
come here在最后的位置

小结

开不开线程,取决去任务的函数,同步不开,异步才能开;
开几条线程,取决于队列,串行开一条,并发可以开多条(异步执行)

相关文章

网友评论

      本文标题:小驴拉磨之iOS进阶-多线程(6)—— GCD 并发队列

      本文链接:https://www.haomeiwen.com/subject/oxplvxtx.html