iOS GCD 学习

iOS开发中,多线程应用方式一般有如下几种

1 比较高层的,封装好的API:NSThread。用来创建非常驻线程以及常驻线程,默认支持NSRunLoop机制。

2 比较高层的,封装好的API:NSOperation。用来管理他的是NSOperationQueue。每一个NSOperation都是一个独立的线程。同样你可以设置其为常住线程比方while(1)这种恶心的代码。

3 比较底层的,简单封装的 C函数形式的API:GCD---Grand Central Dispatch,据说这个是比较底层的,针对多核cpu做的多线程模型,效率比上面两个高一些。具体不太清楚,感兴趣的自己做实验,用事实说话吧。

4 通用的跨平台的线程模型:POSIX线程模型,也是C函数形式的API,pthread族。


下面,我具体谈谈我对GCD的一些理解,其它的一些东西,网上太多了,都是抄来抄去的,不过确实能带入门,还是表示感谢。

dispatch_queue_t 类型的这个queue到底是个什么东西,字面意思理解:首先是一个queue(队列),用来做什么的呢,存放operation(操作/计算模块)的,说白了就是存放函数的。

这一点理解可以从他的应用方式看得出来:

dispatch_asyn(myQueue,^(param1,param2){

//the fucntion body

NSLog(@"excuting the block body");

}));

那问题来了:假设 一瞬间 上面这个语句执行了100次,那这100个函数块儿到底是怎么个执行顺序呢?又是不是在同一个线程上执行的呢?

这就引出了 queue的类型:顺序queue  DISPATCH_QUEUE_SERIAL 和并发queue  DISPATCH_QUEUE_CONCURRENT.


1 DISPATCH_QUEUE_SERIAL 测试

    dispatch_queue_t serialQ = dispatch_queue_create("myserialq", DISPATCH_QUEUE_SERIAL);

    for (int i=0; i<10; i++)
    {
        dispatch_async(serialQ, ^{
            NSLog(@"block%d thread %@",i,[NSThread currentThread]);
        });
       
    }
打印log如下:

2015-12-11 17:08:17.477 noarct[10712:242196] block0 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}

2015-12-11 17:08:17.478 noarct[10712:242196] block1 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}

2015-12-11 17:08:17.478 noarct[10712:242196] block2 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}

2015-12-11 17:08:17.478 noarct[10712:242196] block3 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}

2015-12-11 17:08:17.478 noarct[10712:242196] block4 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block5 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block6 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block7 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block8 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block9 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block10 thread <NSThread: 0x7fdc19526cb0>{number = 2, name = (null)}


可见:DISPATCH_QUEUE_SERIAL 类型的queue上执行 多个代码块儿是,是顺序执行的,一个一个执行的,并且都是在同一个Thread上执行的。也就是说DISPATCH_QUEUE_SERIAL 只有一个线程。

2 DISPATCH_QUEUE_CONCURRENT 测试

    dispatch_queue_t concurrentQ = dispatch_queue_create("myconq", DISPATCH_QUEUE_CONCURRENT);

    for (int i=0; i<10; i++)
    {
        dispatch_async(concurrentQ, ^{
            NSLog(@"block%d thread %@",i,[NSThread currentThread]);
        });
       
    }

打出的log如下:

2015-12-11 17:13:12.842 noarct[10788:246299] block7 thread <NSThread: 0x7f93b59115f0>{number = 6, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246300] block8 thread <NSThread: 0x7f93b3c15cc0>{number = 10, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246287] block0 thread <NSThread: 0x7f93b3c08a60>{number = 2, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246301] block9 thread <NSThread: 0x7f93b58023d0>{number = 11, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246296] block4 thread <NSThread: 0x7f93b3c023e0>{number = 8, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246297] block5 thread <NSThread: 0x7f93b3d32e20>{number = 9, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246298] block6 thread <NSThread: 0x7f93b5aa7870>{number = 7, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246295] block3 thread <NSThread: 0x7f93b5934480>{number = 5, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246288] block1 thread <NSThread: 0x7f93b5918080>{number = 3, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246291] block2 thread <NSThread: 0x7f93b3d32d40>{number = 4, name = (null)}

如果多运行几次,上面的顺序还会变化。我们可以看到,

DISPATCH_QUEUE_CONCURRENT 类型的并发queue,在装入多个函数模块的时候,他的执行不一定是根据装入顺序来的,也不一定是在同一个线程上执行的,至于他需要启动多少个线程来执行 他目前需要执行的函数块儿,是由系统自己决定的,上面看到瞬间加入10个计算单元到这个queue,没有任何其它负载的情况下,他会立即处理这10个计算单元,于是就启动了10个线程分别执行。假设,我们额外做一些其它计算,他会启动多少个线程呢,尝试如下

3

    dispatch_queue_t concurrentQ = dispatch_queue_create("myconq", DISPATCH_QUEUE_CONCURRENT);

    for (int i=0; i<10; i++)
    {
        dispatch_async(concurrentQ, ^{
            NSLog(@"block%d thread %@",i,[NSThread currentThread]);
        });
        <span style="color:#ff0000;"><em>NSLog(@"main %@",[NSThread currentThread]);</em></span>
    }
打印log如下

2015-12-11 17:20:44.029 noarct[10851:249661] main <NSThread: 0x7f8bfbf05000>{number = 1, name = main}

2015-12-11 17:20:44.029 noarct[10851:249705] block0 thread <NSThread: 0x7f8bfdbbc1b0>{number = 2, name = (null)}

2015-12-11 17:20:44.029 noarct[10851:249661] main <NSThread: 0x7f8bfbf05000>{number = 1, name = main}

2015-12-11 17:20:44.029 noarct[10851:249706] block1 thread <NSThread: 0x7f8bfbc337a0>{number = 3, name = (null)}

2015-12-11 17:20:44.030 noarct[10851:249661] main <NSThread: 0x7f8bfbf05000>{number = 1, name = main}

2015-12-11 17:20:44.030 noarct[10851:249705] block2 thread <NSThread: 0x7f8bfdbbc1b0>{number = 2, name = (null)}

2015-12-11 17:20:44.030 noarct[10851:249661] main <NSThread: 0x7f8bfbf05000>{number = 1, name = main}

2015-12-11 17:20:44.030 noarct[10851:249706] block3 thread <NSThread: 0x7f8bfbc337a0>{number = 3, name = (null)}

2015-12-11 17:20:44.030 noarct[10851:249661] main <NSThread: 0x7f8bfbf05000>{number = 1, name = main}

2015-12-11 17:20:44.030 noarct[10851:249705] block4 thread <NSThread: 0x7f8bfdbbc1b0>{number = 2, name = (null)}

2015-12-11 17:20:44.030 noarct[10851:249661] main <NSThread: 0x7f8bfbf05000>{number = 1, name = main}

2015-12-11 17:20:44.030 noarct[10851:249706] block5 thread <NSThread: 0x7f8bfbc337a0>{number = 3, name = (null)}

2015-12-11 17:20:44.030 noarct[10851:249661] main <NSThread: 0x7f8bfbf05000>{number = 1, name = main}

2015-12-11 17:20:44.030 noarct[10851:249705] block6 thread <NSThread: 0x7f8bfdbbc1b0>{number = 2, name = (null)}

2015-12-11 17:20:44.112 noarct[10851:249661] main <NSThread: 0x7f8bfbf05000>{number = 1, name = main}

2015-12-11 17:20:44.112 noarct[10851:249705] block7 thread <NSThread: 0x7f8bfdbbc1b0>{number = 2, name = (null)}

2015-12-11 17:20:44.112 noarct[10851:249661] main <NSThread: 0x7f8bfbf05000>{number = 1, name = main}

2015-12-11 17:20:44.112 noarct[10851:249706] block8 thread <NSThread: 0x7f8bfbc337a0>{number = 3, name = (null)}

2015-12-11 17:20:44.112 noarct[10851:249661] main <NSThread: 0x7f8bfbf05000>{number = 1, name = main}

2015-12-11 17:20:44.112 noarct[10851:249705] block9 thread <NSThread: 0x7f8bfdbbc1b0>{number = 2, name = (null)}

可见,在有其他计算负载的时候,这个并发queue就没有那么放肆的开启n个线程来做他事儿了,线程数量有所减少。
是否可以设置这种并发queue的并发最大执行数量呢?这个,感兴趣的同学可以自己研究。


好了,就暂时说这么多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值