Priority dispatch queues in fast 3

I read a tutorial about GCD and the dispatch queue in Swift 3

But I'm really confused about the order of synchronous execution and asynchronous execution, as well as the main queue and background queue.

I know that if we use sync , then we execute them one by one, if we use async , then we can use QoS to set priority, but what about this case?

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) } } } 

enter image description here

The result shows that we are ignoring asynchronous execution. I know that queue2 should be completed before queue1, since this is synchronous execution, but why do we ignore asynchronous execution and what is the actual difference between asynchronous, synchronizing and so-called main queues?

+5
source share
3 answers

You speak:

The result shows that we are ignoring asynchronous execution ....

No, it just means that you did not give asynchronously sent code enough time to start.

I know that queue2 must be completed before queue1 as it is doing synchronous execution ...

First, queue2 may not end before queue1. It just happens. Make queue2 do something much slower (for example, looping through several thousand iterations, not just five), and you will see that queue1 can actually start at the same time with respect to what's on queue2. First, it takes only a few milliseconds, and the material on your simple queue2 ends before the start of queue1 starts.

Secondly, it is not technically because it is synchronous execution. It's just that async takes a few milliseconds to get it working on some kind of workflow, while a synchronous call due to optimizations that I won't hide from you will start faster.

but why do we ignore asynchronous execution ...

We do not "ignore" him. First, you need only a few milliseconds.

and what is the actual difference between asynchronous, synchronizing and so-called main queues?

"Async" simply means that the current thread may carry and not wait for the sent code to run on some other thread. "Synchronization" means that the current thread must wait for the completion of the sent code.

The main thread is another topic and simply refers to the main thread created to control your user interface. In practice, the main thread is where most of your code is executed, basically everything works, except that you manually send it to some background queue (or the code sent there for you, for example, URLSession completion handlers).

+3
source

sync and async are associated with the same thread / queue. To see the difference, 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") } 

enter image description here

The main queue is the thread in which the entire user interface (UI) is running.

+2
source

First of all, I prefer to use the term β€œdelayed” instead of β€œignored” about code execution, because all your code in your question is executed.

QoS is enum , the first class means the highest priority, the last - the lowest priority, when you do not specify any priority, you have a queue with a default value of priority , and by default in the middle:

  • userInteractive
  • userInitiated
  • default
  • utility
  • background
  • undefined

It is said that you have two synchronous for-in loops and one asynchronous mode, where the priority is based on the position of the loops and the type of queues (sync / async) in the code, because here we have 3 different queues (according to the instructions on the link queuesWithQoS() can be launched in viewDidAppear , so we can assume that it is in the main queue)

The code shows the creation of two queues with a default priority, so the sequence of execution will be:

  • for-in loop with 1000 .. <1005 in the main line
  • synchronous queue2 with default priority
  • asynchronous queue1 (not ignored, just delayed) with default priority

The main queue always has the highest priority, where all user interface instructions are executed.

+2
source

Source: https://habr.com/ru/post/1265915/


All Articles