Sync vs Async
Sync and async operations are about execution order a next task in relation to the current task.
Let's take a look at example where we have three tasks(Task 1, Task 2, Task 3) and we will operate by Task 2. Task is an atomic operation - method call in a stack (method frame).
Synchronous
Implies that tasks will be executed one by one. A next task is started only after current task is finished. Task 3 is not started until Task 2 is finished.
Sync + Single Thread = Sequential
DispatchQueue.main.sync
causes deadlock if you call it from the main thread
main() {
task1()
task2()
task3()
}
Sync + Multi Thread = Parallel
Blocked means that a thread is just waiting(although it could do something useful. e.g. Java ExecutorService[About] and Future[About])
DispatchQueue.global().sync()
main() {
task1()
Future future = ExecutorService.submit(task2())
future.get() //<- blocked operation
task3()
}
Asynchronous
Implies that task returns control immediately with a promise to execute a code and notify about result later(e.g. callback, feature). Task 3 is executed even if Task 2 is not finished. async callback, completion handler[About]
Async + Single Thread = Concurrent
Callback Queue (Message Queue) and Event Loop(RunLoop, Looper)[About] are used. Event Loop checks if Thread Stack is empty and if it is true it pushes first item from the Callback Queue into Thread Stack and repeats these steps again. Simple examples are button click, post event...
Timer.scheduledTimer(withTimeInterval: 2, repeats: false)
main() {
task1()
ThreadMain.handler.post(task2());
task3()
}
Async + Multi Thread = Concurrent and Parallel
For example when you need to make some calculations on another thread without blocking. You are able use result of Task 2 using a blocking method get() or using async callback through a loop.
DispatchQueue.global().async()
main() {
task1()
new Thread(task2()).start();
//or
Future future = ExecutorService.submit(task2())
task3()
}
Examples
For example in Mobile world where we have UI/main thread and we need to download something we have several options:
- sync block - block UI thread and wait when downloading is done. UI is not responsive.
- async callback - create a new tread with a async callback to update UI(is not possible to access UI from non UI thread). Callback hell.
- async coroutine[About] - async task with sync syntax. It allows mix downloading task (suspend function) with UI task.
[Concurrency vs Parallelism]
[iOS GCD]