简体   繁体   English

队列优先级似乎不正确

[英]Queue Priority seems not right

so I am testing out some threading codes 所以我正在测试一些线程代码

private func queuesWithQos(){
    let queue1 = DispatchQueue(label: "com.appcoda.queue1", qos: .userInitiated)
    let queue2 = DispatchQueue(label: "com.appcoda.queue2", qos: .utility)

    queue1.async {
        print("Queue1 thread: \(Thread.current)")
        for i in 0..<10{
            print("Queue1:", i)
        }
    }

    queue2.async {
        print("Queue2 thread: \(Thread.current)")
        for i in 100..<110{
            print("Queue2", i)
        }
    }

    DispatchQueue.main.async {
        print("I am in main queue")
    }
}

And, I think that "I am in main queue" in this case should be printed first right since it is main thread and has the highest priority? 而且,我认为在这种情况下应该首先打印“我在主队列中”,因为它是主线程并且具有最高优先级? But apparently, it is printed last? 但是显然,它是最后印刷的吗?

I don't get it? 我不明白吗?

Can someone explain to me? 有人可以向我解释吗?

Thanks! 谢谢!

The tasks that you have put inside the different queues are fairly simple. 您放入不同队列中的任务非常简单。 In fact, they are so simple that the entire block will get executed before context switching happens and the control gets passed to some other queue. 实际上,它们是如此简单,以至于整个块将在上下文切换发生之前执行,并且控件将传递到其他队列。

Try increasing the range of the for loops to, say 1000000; 尝试将for循环的范围增加到1000000。 you should be able to notice the difference then. 您应该能够注意到差异。 For fair comparison, put a similar loop in the main queue as well. 为公平起见,在主队列中也放置一个类似的循环。

The problem is not the small amount of work in the queues. 问题不在于队列中的工作量少。 In your case even if you increase the number of prints to whatever, chances are that the I am in main queue will never be called first. 就您而言,即使您将打印数量增加到任何数量,也有可能永远不会先调用I am in main queue

If you run your code multiple times, you'll see that the I am in main queue shows at different states because the async command submits the job to a queue, it doesn't matter if it's main or background queue, it won't be executed now, it will take a few milliseconds depending on the load of the queue to be executed. 如果您多次运行代码,则会看到“ I am in main queue显示为不同的状态,因为async命令将作业提交到队列中,无论是主队列还是后台队列,都不会如果要立即执行,则需要花费几毫秒的时间,具体取决于要执行的队列的负载。

In your case, the "I am in main queue" job, takes just a few milliseconds to be scheduled for execution, but it's enough time for the other jobs to start executing their jobs (and maybe completing them). 在您的情况下,“我在主队列中”作业计划执行仅需几毫秒的时间,但是其他作业有足够的时间开始执行其作业(并可能完成它们)。 That's why you see the other logs first, it's that scheduling time delay. 这就是为什么您首先看到其他日志的原因,这是计划时间延迟。

Also you have to keep in mind that the main queue is in charge of UI display, so it might need more time to execute your job, if there is a rendering in process or other blocking operations. 另外,您还必须记住,主队列负责UI显示,因此,如果正在进行渲染或其他阻止操作,则可能需要更多时间来执行您的工作。

If you want to have the "I am in main queue" executed first, simply remove it from the dispatch queue. 如果要首先执行“我在主队列中”,只需将其从调度队列中删除。 That's something like saying "execute this NOW". 就像说“立即执行”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM