Synchronization and Blocking

Synchronous vs Asynchronous and Blocking vs Non-Blocking

For example, A calls B.

Synchronous/Asynchronous focus on how B passes result to A

Synchronous - A keeps waiting for the result from B. A and B are Synchronous

Asynchronous - B returns immediately. A does not wait for the result from B. When the result is ready. A will be notified. A and B are Asynchronous in this case.

Blocking/Non-Blocking focus on A’s status

Blocking - A keeps waiting for the reply from B. A is Blocking.

Non-Blocking - B returns immediately. A does not have to wait for the reply from B. A is Non-Blocking. A may check periodically to see if result is ready OR A will get notified by B when result is ready.

Synchronous Blocking operations are the simplest and most common. Java’s InputStream.read() is a synchronous blocking operation.

Asynchronous Non-Blocking operations are widely used in Node.js. See how Node.js reads file using Asynchronous Non-Blocking method.

1
2
3
4
const fs = require('fs');
fs.readFile('/file.md', (err, data) => {
if (err) throw err;
});