Concurrency with thread, queue, sync, async in iOS
Concurrency in any platform leads to head booming because of insufficient knowledge or little knowledge of the basic concept. Without wasting much time lets jump to the topic. We are going to discuss
- Thread
- Queue (sync and async task)
Threads
Threads are something that is responsible for executing a scheduled task. When we launch our app, the main thread handles UI work and lives with the app. In any app, we have data, business logic and UI. Fetching data should be done in a background thread, business logic execution can be done in the background thread to keep the main thread available for UI updates.
Queue
Queue manages the execution of task serially or concurrently on either app main thread or background thread. We can have a serial queue and concurrent queue based on our requirement. A task we submit to these queues can be sync or async. Task submitted with sync, queue will wait for the completion of task. Task submitted with Async, queue will execute next item without waiting for completion of task.
Now we will talk about the different prospect of sync and async task with both serial and concurrent queue.
What will happen if we have sync task in concurrent queue, will it work as serial queue or concurrent queue?
Well let see the output of this code
Queue start
Thread start <NSThread: 0x600002b30040>{number = 1, name = main}
Thread working <NSThread: 0x600002b30040>{number = 1, name = main}
Finish 100
Finish 101
Finish 102
Finish 103
Finish 104
Finish 105
Thread working <NSThread: 0x600002b30040>{number = 1, name = main}
Finish 200
Finish 201
Finish 202
Finish 203
Finish 204
Finish 205
Thread working <NSThread: 0x600002b30040>{number = 1, name = main}
Finish 300
Finish 301
Finish 302
Finish 303
Finish 304
Finish 305
Thread working <NSThread: 0x600002b30040>{number = 1, name = main}
Finish 400
.
.
.
Queue finish
Sync will let us execute one task at once, once the current task is over it will start the next task. If we look into the above output, tasks are also executed in the main thread that’s totally understandable because in the playground main thread doesn’t have any UI task to do.
Now before moving forward in our discussion, let have a look at how async will behave
Below is the output
Queue start
Thread start <NSThread: 0x6000015a43c0>{number = 1, name = main}
Thread working <NSThread: 0x6000015a48c0>{number = 7, name = (null)}
Finish 100
Thread working <NSThread: 0x6000015ad000>{number = 5, name = (null)}
Finish 101
Finish 200
Finish 201
Queue finish
Thread working <NSThread: 0x6000015b0280>{number = 4, name = (null)}
Finish 102
Finish 300
Finish 103
Finish 301
Finish 202
Finish 302
Finish 203
Finish 303
Async will not wait for the current task to get completed, the next task starts without waiting for the current task completion. This will be helpful in the faster execution of tasks in a queue.
Lets take a simple example to understand this
There is a bank that have 4 departments investment, financial, loan, credit card. Now suppose there is a queue in front of reception and receptionist responsibility is to direct every person to the respective department.
Assume department as thread every person as a task, receptionist as queue responsible for scheduling task to threads. In the below example receptionist doesn’t wait for person1 to go out of bank and then send person2 to the department. Receptionist will send person1 to credit card then immediately send person2 to the finance department and so on.
This is the classic example of async task schedule to queue, upon the availability of threads queue will schedule a task to available thread.
Now lets see how sync behave
Assume we are in a pandemic, only one person at a time is allowed to go inside the bank, next person is allowed once a person in will go out of the bank. Bank has availability of threads(department) but due to pandemic they can execute only one task(person) at once. This example is a good way to understand sync in queue. Threads are available to execute task but due to sync block only queue will wait for the completion of task.
This doesn't mean that queue is behaving same as serial queue because serial queue execute all task sequentially, but concurrent queue will wait only for sync block task to complete. All async tasks, executed before sync task can get complete in any order.
Thanks for reading this. Let me know your feedback.
Happy coding!