简体   繁体   English

快速队列中的调度队列优先级3

[英]priority of Dispatch Queues in swift 3

I have read the tutorial about GCD and Dispatch Queue in Swift 3 在Swift 3中阅读了有关GCD和Dispatch Queue的教程

But I'm really confused about the order of synchronous execution and asynchronous execution and main queue and background queue. 但我对同步执行和异步执行以及主队列和后台队列的顺序感到困惑。

I know that if we use sync then we execute them one after the precious one, if we use async then we can use QoS to set their priority, but how about this case? 我知道如果我们使用同步,那么我们在珍贵的一个之后执行它们,如果我们使用异步,那么我们可以使用QoS来设置它们的优先级,但是这种情况怎么样?

func queuesWithQoS() {
    let queue1 = DispatchQueue(label: "com.appcoda.myqueue1")
    let queue2 = DispatchQueue(label: "com.appcoda.myqueue2")

    for i in 1000..<1005 {
                print(i)
    }


    queue1.async {
        for i in 0..<5{
            print(i)
        }
    }

    queue2.sync {
        for i in 100..<105{
            print( i)
        }
    }
}

在此输入图像描述

The outcome shows that we ignore the asynchronous execution. 结果表明我们忽略了异步执行。 I know queue2 should be completed before queue1 since it's synchronous execution but why we ignore the asynchronous execution and what is the actual difference between async, sync and so-called main queue? 我知道queue2应该在queue1之前完成,因为它是同步执行但是为什么我们忽略了异步执行,async,sync和所谓的主队列之间的实际区别是什么?

You say: 你说:

The outcome shows that we ignore the asynchronous execution. 结果表明我们忽略了异步执行。 ... ...

No, it just means that you didn't give the asynchronously dispatched code enough time to get started. 不,这只是意味着您没有给异步调度代码足够的时间来开始。

I know queue2 should be completed before queue1 since it's synchronous execution ... 我知道queue2应该在queue1之前完成,因为它是同步执行的...

First, queue2 might not complete before queue1. 首先,queue2可能无法在queue1之前完成。 It just happens to. 它恰好发生了。 Make queue2 do something much slower (eg loop through a few thousand iterations rather than just five) and you'll see that queue1 can actually run concurrently with respect to what's on queue2. 使queue2做得慢得多(例如循环几千次而不是五次),你会发现queue1实际上可以同时运行在queue2上的内容。 It just takes a few milliseconds to get going and the stuff on your very simple queue2 is finishing before the stuff on queue1 gets a chance to start. 它只需要几毫秒的时间就可以完成,而非常简单的queue2上的东西在queue1上的东西有机会启动之前就完成了。

Second, this behavior is not technically because it's synchronous execution. 其次,这种行为在技术上并不是因为它是同步执行。 It's just that async takes a few milliseconds to get it's stuff running on some worker thread, whereas the synchronous call, because of optimizations that I won't bore you with, gets started more quickly. 只是异步需要几毫秒才能让它在某个工作线程上运行,而同步调用因为我不会厌烦你的优化,会更快地启动。

but why we ignore the asynchronous execution ... 但为什么我们忽略异步执行......

We don't "ignore" it. 我们不“忽视”它。 It just takes a few milliseconds to get started. 开始只需几毫秒。

and what is the actual difference between async, sync and so-called main queue? 什么是异步,同步和所谓的主队列之间的实际区别?

"Async" merely means that the current thread may carry on and not wait for the dispatched code to run on some other thread. “异步”仅仅意味着当前线程可以继续而不是等待调度代码在某个其他线程上运行。 "Sync" means that the current thread should wait for the dispatched code to finish. “同步”表示当前线程应该等待分派的代码完成。

The "main thread" is a different topic and simply refers to the primary thread that is created for driving your UI. “主线程”是一个不同的主题,只是指为驱动UI而创建的主线程。 In practice, the main thread is where most of your code runs, basically running everything except that which you manually dispatch to some background queue (or code that is dispatched there for you, eg URLSession completion handlers). 在实践中,主线程是大多数代码运行的地方,基本上运行除手动调度到某个后台队列(或者为您调度的代码,例如URLSession完成处理程序)之外的所有内容。

sync and async are related to the same thread / queue. syncasync与同一个线程/队列相关。 To see the difference please run this code: 要查看差异,请运行以下代码:

func queuesWithQoS() {
    let queue1 = DispatchQueue(label: "com.appcoda.myqueue1")

    queue1.async {
        for i in 0..<5{
            print(i)
        }
    }
    print("finished")

    queue1.sync {
        for i in 0..<5{
            print(i)
        }
    }
    print("finished")
}

在此输入图像描述

The main queue is the thread the entire user interface (UI) runs on. 主队列是运行整个用户界面(UI)的线程。

First of all I prefer to use the term "delayed" instead of "ignored" about the code execution, because all your code in your question is executed. 首先,我更喜欢使用术语“延迟”而不是“忽略”代码执行,因为您的问题中的所有代码都已执行。

QoS is an enum , the first class means the highest priority, the last one the lowest priority, when you don't specify any priority you have a queue with default priority and default is in the middle: QoS是一个枚举 ,第一个类表示最高优先级,最后一个表示最低优先级,当您没有指定任何优先级时,您有一个具有默认优先级的队列,默认位于中间:

  • userInteractive userInteractive
  • userInitiated 用户引发
  • default 默认
  • utility 效用
  • background 背景
  • unspecified 不明

Said that, you have two synchronous for-in loops and one async, where the priority is based by the position of the loops and the kind of the queues (sync/async) in the code because here we have 3 different queues (following the instructions about your link queuesWithQoS() could be launched in viewDidAppear so we can suppose is in the main queue) 说,你有两个同步的for-in循环和一个async,其中优先级是基于循环的位置和代码中的队列类型(sync / async),因为这里我们有3个不同的队列 (跟随关于你的链接 queuesWithQoS()可以在viewDidAppear启动,所以我们可以假设在主队列中)

The code show the creation of two queues with default priority, so the sequence of the execution will be: 代码显示了两个具有默认优先级的队列的创建,因此执行的顺序将是:

  1. the for-in loop with 1000..<1005 in the main queue 主队列中的1000 .. <1005的for-in循环
  2. the synchronous queue2 with default priority 具有默认优先级的同步queue2
  3. the asynchronous queue1 (not ignored, simply delayed) with default priority 具有默认优先级的异步queue1 (不会被忽略,只是延迟)

Main queue have always the highest priority where all the UI instructions are executed. 在执行所有UI指令的情况下,主队列始终具有最高优先级。

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

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