Note: If you are a beginner and you find it difficult to understand things written in this article. Feel free to ask questions on my Reddit page or Twitter.
To Read How to use GCD (Grand Central Dispatch) in Swift? - Synchronous & Asynchronous Programming Tutorial. Click here.
Remember, you can do it. 😉
I hope you read my first article about GCD (Synchronous & Asynchronous Programming), let’s continue with Concurrent Queue vs Serial Queue. There are going to be many more articles on GCD coming soon, so subscribe now if you haven’t.
There are times we need to use multiple queues to execute a single functionality, and Concurrent and Serial Queues are about the number of queues you can perform at once.
Let’s dive into the differences and examples of these queues for your better understanding.
What is a Serial Queue?
A Serial queue allows us to perform only one task at a time, no matter the way of execution, i.e. Synchronous or Asynchronous.
All the queues need to wait for the completion of the previous queue. By default, DispatchQueue is a serial queue.
E.g.
let queue = DispatchQueue(label: "com.swiftpal.dispatch.serial")
queue.async() {
Thread.sleep(forTimeInterval: 3) // Wait for 3 seconds
print("Task 1 Done")
}
queue.async() {
Thread.sleep(forTimeInterval: 1) // Wait for 1 second.
print("Task 2 Done")
}
/* Output:
Task 1 Done
Task 2 Done
*/
As you can see in the Output, the code is executed in the order it was defined, even though we used the async( ) function, the first thread took 3 seconds for completion and after that, the second thread was executed which took another second to complete.
In short, the code executed by Serial DispatchQueue is not parallel and has to wait for the first task to complete. This way of execution is also known as First In, First Out (FIFO).
What is a Concurrent Queue?
A concurrent queue helps us execute multiple tasks at the same time, but there’s a limit to the number of tasks depending on the system. If used incautiously, it can cause Thread explosion and Deadlock.
E.g.
// Concurrent queue created
let queue = DispatchQueue(label: "com.swiftpal.dispatch.concurrent", attributes: .concurrent)
queue.async() {
print("Task 1 Pending")
Thread.sleep(forTimeInterval: 3) // Wait for 3 seconds
print("Task 1 Done")
}
queue.async() {
print("Task 2 Pending")
Thread.sleep(forTimeInterval: 1) // Wait for 1 second.
print("Task 2 Done")
}
/* Output:
Task 1 Pending
Task 2 Pending
Task 2 Done
Task 1 Done
*/
To create a concurrent queue, you need to specify it while initializing the DispatchQueue object. For reference, look into the code above. We are setting a new property named attributes with the value of .concurrent.
If you look at the output, you can see that both the task ran parallel, but the second task finished first regardless of the first task status of completion.
So, concurrent is efficient and uses the best of multi-core, but we need to keep a tab on the number of tasks executed to avoid our device from freezing.
HOW DO WE CONTROL THE COUNT? SUBSCRIBE.
There is so much more to this, and I’ll try my best to teach you everything I know about GCD.
To Read, How to group dispatch queues in Swift? Click Here.
If you have any suggestions or questions, Feel free to connect me on Twitter. 😉