Lets first understand what are the four kind of calls before we understand the differences.
Synchronous - Thread will complete the action, either by success or failure. ie the work is done in the thread that calls the function and the method does not return until it is finished.
Blocking - Thread will wait on action untill success or failure . Failure is commonly a timeout.Examples would be
- When a java thread is making jdbc request to rdbms. The thread is simply waiting for response on socket.
- Wait and notify mechanism. Thread blocks/waits for notification by another thread.
- Fanout pattern where thread blocks , waiting for a group of threads to finish a job.
- File read operations can be blocking calls.
Non-blocking - Thread will not wait to complete the action. Non blocking io is a very good example of non blocking calls. Event loop in nodejs is non blocking in nature..(non blocking sockets using epoll). Note that there is no call back function like asynchronous calls hence polling is required.
Asynchronous - Another thread/threadpool/process will complete the action . The action may simply be added to queue which is being processed .In a nutshell there is delegation. ie asynchronous methods return immediately because another thread does the work and raises a flag or fires an event or executes a call back function when the work is done. Asynchnous calls are very common , for example
- Used for lower criticality tasks Eg while executing a transaction , notifications may be sent asynchnously using queue and threadpool or just threadpool. This will reduce the response time to user.
Difference between non blocking and asynchronous
In non blocking call there is no task delegation. In asychronous call there is task delegation to thread/threadpool/process/queue and call back function may be provided which will be called after task is executed.
Similarity between non blocking and asynchronous
Both non blocking and asynchnous methods will return immediately.