简体   繁体   中英

concurrency (dispatchqueue) test isn't going as expected ios/swift

So I have this code in a Swift 4 playground:

//: Playground - noun: a place where people can play

import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

DispatchQueue.main.async {
    for _ in 1...5 { print("Main") }
}

DispatchQueue.global().async {
    for _ in 1...5 { print("Background") }
}

DispatchQueue.global(qos: .userInteractive).async {
    for _ in 1...5 { print("User Interactive") }
}

Why does this print out:

User Interactive
Background
Background
User Interactive
User Interactive
Background
User Interactive
Background
User Interactive
Background
Main
Main
Main
Main
Main

I understand why the "User Interactive" and "Background" are printing out in a random order, but why is "Main" printing out after? Shouldn't it be prioritized because it's in the main queue? I guess I still don't fully understand how GCD and QOS works in Swift 4.

EDIT I now added these two print outs and a few lines:

print("BEGINNING OF CODE")

DispatchQueue.main.async {
    for _ in 1...5 { print("Main") }
}

DispatchQueue.global().sync {
    for _ in 1...5 { print("Sync Global") }
}

DispatchQueue.global(qos: .background).async {
    for _ in 1...5 { print("Background") }
}
DispatchQueue.global(qos: .userInteractive).async {
    for _ in 1...5 { print("User Interactive") }
}

print("END OF CODE")

And it prints this out:

BEGINNING OF CODE
Sync Global
Sync Global
Sync Global
Sync Global
Sync Global
END OF CODE
User Interactive
Background
User Interactive
User Interactive
User Interactive
User Interactive
Background
Background
Background
Background
Main
Main    
Main
Main
Main

My question now is: shouldn't the background queues be printed out before the "END OF CODE"? If the last two blocks were enqueued and immediately started executing, why aren't they printing out before? Is it because the main queue has a higher priority? But shouldn't it run asynchronously?

Global queues are non FIFO type .. They occupy a thread soon as they find one that is available. Thats why they run faster..

The main queue task runs serially on main thread.. usually called from background when UI needs updation

The code in the playground is being run on the main queue. So the first for loop is enqueued on the main queue and it won't be run until the end of your code is reached.

The other two blocks are enqueued onto background queues and can start running immediately.

The code you have running on the background queues is so simple and quick that it happens to finish before the first block has a chance to run.

Add a sleep inside one of the two background blocks and you will see that the "Main" output comes out sooner. Your test gives misleading results because it is to simple and too fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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