ConcurrencyMidMCQ
What is the difference between a `serial` and `concurrent` queue in GCD?
Test your knowledge:
Explanation & Code
Answer:
- Serial queue — executes tasks one at a time, in order. Second task waits for first to finish.
- Concurrent queue — executes multiple tasks simultaneously on different threads.
Code Example:
// Serial queue — tasks run one after another
let serialQueue = DispatchQueue(label: "com.app.serial")
serialQueue.async { print("Task 1") }
serialQueue.async { print("Task 2") } // always after Task 1
// Concurrent queue — tasks run in parallel
let concurrentQueue = DispatchQueue(label: "com.app.concurrent",
attributes: .concurrent)
concurrentQueue.async { print("Task A") }
concurrentQueue.async { print("Task B") } // may run before Task A
// Global queues are concurrent with different priorities
DispatchQueue.global(qos: .userInitiated).async { ... }
DispatchQueue.global(qos: .background).async { ... }
// Main queue is serial — always use for UI updates
DispatchQueue.main.async {
self.tableView.reloadData()
}
Rate your understanding: