iOS 代码解析

片段1

for(int i =0;i<10;i++)
    {
        NSLog(@"i=%d",i);
        dispatch_queue_t mySerialQueue = dispatch_queue_create("com.biostime.xxx", NULL);
        __block int d = i;
        dispatch_async(mySerialQueue, ^{
            NSRunLoop *loop = [NSRunLoop currentRunLoop];
            NSLog(@"%p excute1 i = %d d=%d",loop,i,d);
            d=9;
            NSLog(@"%p excute2 i = %d d=%d",loop,i,d);
        });
        NSLog(@"i=%d",i);
    }

 

=============

2015-05-25 23:15:10.758 shareObjectDemo[4165:100897] i=0
2015-05-25 23:15:10.760 shareObjectDemo[4165:100897] i=0
2015-05-25 23:15:10.760 shareObjectDemo[4165:100999] 0x7face9612c80 excute1 i = 0 d=0
2015-05-25 23:15:10.760 shareObjectDemo[4165:100897] i=1
2015-05-25 23:15:10.761 shareObjectDemo[4165:100897] i=1
2015-05-25 23:15:10.761 shareObjectDemo[4165:100999] 0x7face9612c80 excute2 i = 0 d=9
2015-05-25 23:15:10.761 shareObjectDemo[4165:100897] i=2
2015-05-25 23:15:10.762 shareObjectDemo[4165:100897] i=2
2015-05-25 23:15:10.762 shareObjectDemo[4165:100999] 0x7face9612c80 excute1 i = 1 d=1
2015-05-25 23:15:10.762 shareObjectDemo[4165:100897] i=3
2015-05-25 23:15:10.762 shareObjectDemo[4165:100999] 0x7face9612c80 excute2 i = 1 d=9
2015-05-25 23:15:10.763 shareObjectDemo[4165:100897] i=3
2015-05-25 23:15:10.763 shareObjectDemo[4165:100999] 0x7face9612c80 excute1 i = 3 d=3
2015-05-25 23:15:10.763 shareObjectDemo[4165:100897] i=4
2015-05-25 23:15:10.763 shareObjectDemo[4165:100999] 0x7face9612c80 excute2 i = 3 d=9
2015-05-25 23:15:10.764 shareObjectDemo[4165:100897] i=4
2015-05-25 23:15:10.762 shareObjectDemo[4165:101000] 0x7face94950e0 excute1 i = 2 d=2
2015-05-25 23:15:10.764 shareObjectDemo[4165:100999] 0x7face9612c80 excute1 i = 4 d=4
2015-05-25 23:15:10.764 shareObjectDemo[4165:100897] i=5
2015-05-25 23:15:10.764 shareObjectDemo[4165:101000] 0x7face94950e0 excute2 i = 2 d=9
2015-05-25 23:15:10.765 shareObjectDemo[4165:100999] 0x7face9612c80 excute2 i = 4 d=9
2015-05-25 23:15:10.787 shareObjectDemo[4165:100897] i=5
2015-05-25 23:15:10.788 shareObjectDemo[4165:101000] 0x7face94950e0 excute1 i = 5 d=5
2015-05-25 23:15:10.788 shareObjectDemo[4165:100897] i=6
2015-05-25 23:15:10.789 shareObjectDemo[4165:100897] i=6
2015-05-25 23:15:10.789 shareObjectDemo[4165:101000] 0x7face94950e0 excute2 i = 5 d=9
2015-05-25 23:15:10.790 shareObjectDemo[4165:100897] i=7
2015-05-25 23:15:10.790 shareObjectDemo[4165:100897] i=7
2015-05-25 23:15:10.790 shareObjectDemo[4165:100998] 0x7face9614150 excute1 i = 6 d=6
2015-05-25 23:15:10.791 shareObjectDemo[4165:100897] i=8
2015-05-25 23:15:10.791 shareObjectDemo[4165:100998] 0x7face9614150 excute2 i = 6 d=9
2015-05-25 23:15:10.791 shareObjectDemo[4165:100897] i=8
2015-05-25 23:15:10.791 shareObjectDemo[4165:101000] 0x7face94950e0 excute1 i = 7 d=7
2015-05-25 23:15:10.792 shareObjectDemo[4165:100897] i=9
2015-05-25 23:15:10.793 shareObjectDemo[4165:100897] i=9
2015-05-25 23:15:10.792 shareObjectDemo[4165:101000] 0x7face94950e0 excute2 i = 7 d=9
2015-05-25 23:15:10.793 shareObjectDemo[4165:100999] 0x7face9612c80 excute1 i = 9 d=9
2015-05-25 23:15:10.792 shareObjectDemo[4165:100998] 0x7face9614150 excute1 i = 8 d=8
2015-05-25 23:15:10.795 shareObjectDemo[4165:100999] 0x7face9612c80 excute2 i = 9 d=9
2015-05-25 23:15:10.795 shareObjectDemo[4165:100998] 0x7face9614150 excute2 i = 8 d=9
 解析:

都说dispatch_queue_create创建的是串行队列,那么,只要id相同,执行的任务就一定是串行的吗?

错。

2015-05-25 23:15:10.762 shareObjectDemo[4165:101000] 0x7face94950e0 excute1 i = 2 d=2
2015-05-25 23:15:10.764 shareObjectDemo[4165:100999] 0x7face9612c80 excute1 i = 4 d=4

这两行能说明问题。excute1 i = 4 d=4并不是等excute1 i = 2 执行完毕(执行0x7face94950e0 excute2 i = 2 d=9)后才执行。

而且仔细观察loop的地址,就算id相同,也并不是在同一个线程中执行。某种机制,会触发创建新的子线程。

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。