iOS GCD自定义并发队列执行序列 [英] iOS GCD custom concurrent queue execution sequence

查看:160
本文介绍了iOS GCD自定义并发队列执行序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个问题有疑问,根据苹果公司的文件,


并发
并发队列(也称为作为一种全局调度队列)同时执行一个或多个任务,但任务仍按添加到队列中的顺序启动。当前正在执行的任务在受管理的不同线程上运行通过调度队列。在任何给定时间执行的任务的确切数量是可变的,并且取决于系统条件。
在iOS 5及更高版本中,您可以通过将DISPATCH_QUEUE_CONCURRENT指定为队列类型来自己创建并发调度队列。另外,有四个预定义的全局并发队列供您的应用程序使用。有关如何获取全局并发队列的更多信息,请参见获取全局并发调度队列。


然后我使用样例代码

  dispatch_queue_t concurrentQueue; 
concurrentQueue = dispatch_queue_create( com.gcd.concurrentQueue,
DISPATCH_QUEUE_CONCURRENT);

dispatch_async(concurrentQueue,^ {
NSLog(@ First job);
});
dispatch_async(concurrentQueue,^ {
NSLog(@ Second job);
});

dispatch_async(concurrentQueue,^ {
NSLog(@ Third job);
});

但是结果似乎不像它们添加的顺序,这里是结果,

  2015-06-03 18:36:38.114 GooglyPuff [58461:1110680]第一份工作
2015-06-03 18 :36:38.114 GooglyPuff [58461:1110682]第三份工作
2015-06-03 18:36:38.114 GooglyPuff [58461:1110679]第二份工作

所以我的问题是,不是第一,第二,第三是
吗?



欢迎任何建议,谢谢您的帮助。

解决方案

并发是指它们同时运行,并且不应对它们在任何给定时刻的进展情况以及先完成的情况做出任何假设。这就是并发的全部含义和含义:在一个并发操作的一行代码和下一行之间-甚至是在 一行代码中-其他任何并发操作都可能发生。 p>

因此,为回答您的特定问题,这些任务可能以已知的顺序开始开始,但是很快就完成了,在此之后, progress 被不可预测地交错。您的NSLog调用就是这一进展的一部分;他们没有也不能告诉您任务何时开始!


I have question regarding this issue , According to Apple's documents

Concurrent Concurrent queues (also known as a type of global dispatch queue) execute one or more tasks concurrently, but tasks are still started in the order in which they were added to the queue. The currently executing tasks run on distinct threads that are managed by the dispatch queue. The exact number of tasks executing at any given point is variable and depends on system conditions. In iOS 5 and later, you can create concurrent dispatch queues yourself by specifying DISPATCH_QUEUE_CONCURRENT as the queue type. In addition, there are four predefined global concurrent queues for your application to use. For more information on how to get the global concurrent queues, see Getting the Global Concurrent Dispatch Queues.

And i do a test, using the sample code ,

  dispatch_queue_t concurrentQueue;
    concurrentQueue = dispatch_queue_create("com.gcd.concurrentQueue",
                                                 DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(concurrentQueue, ^{
         NSLog(@"First job ");
    });
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Second job");
    });

    dispatch_async(concurrentQueue, ^{
        NSLog(@"Third job ");
  });

But the results seems not as the order in which they are added, here is the results,

2015-06-03 18:36:38.114 GooglyPuff[58461:1110680] First job 
2015-06-03 18:36:38.114 GooglyPuff[58461:1110682] Third job
2015-06-03 18:36:38.114 GooglyPuff[58461:1110679] Second job

So my question is , shouldn't it be First, Second , Third ?

Any advice is welcome , and thanks for your help.

解决方案

"Concurrent" means they run at the same time and no assumptions should be made about where in their progress any of them will be at any given moment and which will finish first. That is the whole meaning and implication of concurrency: between one line of code and the next in one concurrent operation - even during one line of code - anything else from any other concurrent operation might be happening.

So, in answer to your particular question, these tasks may have started in a known order, but that happened very quickly, and after that point their progress is interleaved unpredictably. And your NSLog calls are part of that progress; they do not, and cannot, tell you when the tasks started!

这篇关于iOS GCD自定义并发队列执行序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆