详解GCD<1>队列

2018-01-31  本文已影响0人  FreeBreath

GCD有三种queue

main queue: 主线程队列。是一个串行队列。一般用来更新UI。

global queue: 全局队列,是一个并行队列。使用方法相信大家都知道。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{

[self     test];

}

上面的意思就是开启一个异步线程,在全局队列中执行。

custom queue:自定义队列。有两种自定义队列。”

dispatch_queue_t serial_queue = dispatch_queue_create("com.reviewcode.www", DISPATCH_QUEUE_SERIAL);

dispatch_queue_t concurrent_queue = dispatch_queue_create("com.concurrent.www", DISPATCH_QUEUE_CONCURRENT);

serial_queue即是我自定义的一个串行队列。上面提到的主线程队列也是一个串行队列。

concurrent_queue是我自定义的一个并行队列。上面提到的global queue就是一个并行队列。

现在我们来讨论2个问题。

1.dispatch_async里使用串行队列和并行队列的效果。

2.dispatch_sync里使用串行队列和并行队列的效果。

串行队列和并行队列的创建如下。

serial_queue = dispatch_queue_create("com.reviewcode.www", DISPATCH_QUEUE_SERIAL);

concurrent_queue = dispatch_queue_create("com.LiSiGuang.www", DISPATCH_QUEUE_CONCURRENT);

讨论的问题:

“在dispatch_async使用串行队列”

代码如下:

-(void)testSerialQueueWithAsync

{

    for ( int index =0; index < 10; index++)

    {

        dispatch_async(serial_queue,^{

            NSLog(@"index =%d",index);

            NSLog(@" current thread is %@",[NSThread currentThread]);

        });

      }

          NSLog(@"Running on main Thread");

}

然后在viewDidLoad()方法里打印,打印结果如下。

index =0

Running on main Thread

current thread is<NSThred:0x600000466b40>{number = 4, name = (null)}

index =1

current thread is{number = 4, name = (null)}

index =2

current thread is{number = 4, name = (null)}

index =3

current thread is{number = 4, name = (null)}

index =4

current thread is{number = 4, name = (null)}

index =5

current thread is{number = 4, name = (null)}

index =6

current thread is{number = 4, name = (null)}

index =7

current thread is{number = 4, name = (null)}

index =8

current thread is{number = 4, name = (null)}

index =9

current thread is{number = 4, name = (null)}

打印结果的几个特征。

在dispatch_async使用的所有Thread均为同一个Thread。因为他们的指针地址完全相同。

输出结果是按顺序输出,符合我们对串行队列的期待。即FIFO。先进先出原则。

Running on main Thread这句话并没有在最后执行,而是会出现在随机的位置,这也符合我们对dispatch_async的期待,因为他会开辟一个新的线程执行,不会阻塞主线程。 ok,让我们测试下一个。

在dispatch_async中使用并行队列

2018-01-31 10:07:40.409923+0800 TextD[22242:494553] index =22018-01-31 10:07:40.409922+0800 TextD[22242:494552] index =02018-01-31 10:07:40.409922+0800 TextD[22242:494551] index =12018-01-31 10:07:40.409922+0800 TextD[22242:494487] Running on main Thread2018-01-31 10:07:40.409980+0800 TextD[22242:494554] index =32018-01-31 10:07:40.410040+0800 TextD[22242:494561] index =42018-01-31 10:07:40.410284+0800 TextD[22242:494551] current thread is{number = 3, name = (null)}2018-01-31 10:07:40.410285+0800 TextD[22242:494552] current thread is{number = 5, name = (null)}2018-01-31 10:07:40.410292+0800 TextD[22242:494553] current thread is{number = 4, name = (null)}2018-01-31 10:07:40.410349+0800 TextD[22242:494554] current thread is{number = 6, name = (null)}2018-01-31 10:07:40.410422+0800 TextD[22242:494551] index =52018-01-31 10:07:40.410435+0800 TextD[22242:494561] current thread is{number = 7, name = (null)}2018-01-31 10:07:40.410780+0800 TextD[22242:494562] index =62018-01-31 10:07:40.410881+0800 TextD[22242:494564] index =82018-01-31 10:07:40.410882+0800 TextD[22242:494563] index =72018-01-31 10:07:40.410929+0800 TextD[22242:494565] index =92018-01-31 10:07:40.412670+0800 TextD[22242:494551] current thread is{number = 3, name = (null)}2018-01-31 10:07:40.437759+0800 TextD[22242:494563] current thread is{number = 9, name = (null)}2018-01-31 10:07:40.437768+0800 TextD[22242:494562] current thread is{number = 8, name = (null)}2018-01-31 10:07:40.437787+0800 TextD[22242:494564] current thread is{number = 10, name = (null)}2018-01-31 10:07:40.437830+0800 TextD[22242:494565] current thread is{number = 11, name = (null)}

打印结果的特征如下:

输出的结果是乱序的,说明我们的输出语句是并发的,由多个线程共同执行的。

Running on main Thread这句话依然没有被阻塞,直接输出了。

每次打印语句的Thread均不相同。

仔细比对两次打印结果的异同点。提出问题。

串行队列如何保证在异步线程中遵守先进先出原则(从Demo里看,也就是顺序打印我们的结果)?

很简单,它会保证每次dispatch_async开辟线程执行串行队列中的任务时,总是使用同一个异步线程。这也是为什么我们的第一次打印结果中,NSThread总是同一个。

在dispatch_async中放入并行队列并执行的时候,为什么执行顺序总是乱序的?

因为在并行对列中,每执行一次任务的时候,dispatch_async总会为我们开辟一个新的线程(当然,开辟线程的[…]”

在dispatch_sync使用串行队列:

-(void)testSerialQueueWithsync

{    dispatch_queue_t serial_queue = dispatch_queue_create("com.reviewcode.www", DISPATCH_QUEUE_SERIAL);

    for ( int index =0; index < 10; index++)

    {        dispatch_sync(serial_queue,^{

            NSLog(@"index =%d",index);

            NSLog(@" current thread is %@",[NSThread currentThread]);

        }); 

  }

    NSLog(@"Running on main Thread");

}

1.dispatch_sync并没有开辟一个新的线程,直接在当前线程中执行代码(即main线程)。所以会阻塞当前线程。

2.Running on main Thread在最后输出。

也就是说,当使用dispatch_sync执行串行队列的任务时,不会开辟新的线程,会直接使用当前线程执行队列中的任务。

在dispatch_sync中使用并行队列

-(void)testConcurrentQueueWithsync

{    dispatch_queue_t Concurrent_queue = dispatch_queue_create("com.reviewcode.www", DISPATCH_QUEUE_CONCURRENT);

    for ( int index =0; index < 10; index++)

    {

        dispatch_sync(Concurrent_queue,^{

            NSLog(@"index =%d",index);

            NSLog(@" current thread is %@",[NSThread currentThread]);

        });

    }

    NSLog(@"Running on main Thread");

}

打印结果如下:

2018-01-31 10:30:19.728025+0800 TextD[22520:524950] index =02018-01-31 10:30:19.728294+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.728425+0800 TextD[22520:524950] index =12018-01-31 10:30:19.728605+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.728753+0800 TextD[22520:524950] index =22018-01-31 10:30:19.728875+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.728990+0800 TextD[22520:524950] index =32018-01-31 10:30:19.729108+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.729223+0800 TextD[22520:524950] index =42018-01-31 10:30:19.729351+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.729474+0800 TextD[22520:524950] index =52018-01-31 10:30:19.729597+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.729697+0800 TextD[22520:524950] index =62018-01-31 10:30:19.729831+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.729952+0800 TextD[22520:524950] index =72018-01-31 10:30:19.730147+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.730316+0800 TextD[22520:524950] index =82018-01-31 10:30:19.730617+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.730786+0800 TextD[22520:524950] index =92018-01-31 10:30:19.730986+0800 TextD[22520:524950] current thread is{number = 1, name = main}

2018-01-31 10:30:19.731201+0800 TextD[22520:524950] Running on main Thread

总结:

结果很奇怪,和在串行队列执行的效果一模一样。按我们的思考,并行队列里执行任务不应该是多个线程同时跑么?其实是由于dispatch_sync并不会开辟新的线程执行任务,所以导致了执行并行队列任务的线程总会是一个线程,自然,结果是一样的。

上一篇下一篇

猜你喜欢

热点阅读