34

I am reading 'Operation System Concepts With Java'. I am quite confused by the concept of blocking and synchronous, what are the differences between them?

Mat
  • 202,337
  • 40
  • 393
  • 406
diligent
  • 2,282
  • 8
  • 49
  • 64
  • Please refer to this blog http://voinici.ceata.org/~sana/blog/?p=248 and http://stackoverflow.com/questions/2625493/asynchronous-vs-non-blocking – Zain Khan Dec 07 '11 at 14:30

4 Answers4

32

Blocking may or may not be the same as synchronous, depending on the context. When we talk about method calls, then a synchronous call can also be said to be blocking (I'll get back to this in a bit), because the thread calling the method cannot proceed forward until the method returns. The antonym in this case would be asynchronous.

In lock terminology, a lock is said to be blocking if the thread waiting to acquire it is put in a suspended mode until the lock becomes available (or until a timeout elapses). The antonym in this case is a non-blocking lock, meaning that the thread returns immediately even if it cannot acquire the lock. This can be used to implement the so called spinning lock, where you keep polling the state of the lock while keeping the thread active.

Having said this, you can extrapolate the difference between the concepts: synchronous generally means an activity that must wait for a reply before the thread can move forward. Blocking refers to the fact that the thread is placed in a wait state (generally meaning it will not be scheduled for execution until some event occurs). From here you can conclude that a synchronous call may involve blocking behavior or may not, depending on the underlying implementation (i.e. it may also be spinning, meaning that you are simulating synchronous behavior with asynchronous calls).

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • 1
    Hi , can you give an example in which a blocking call is not synchronous ? Thanks – Always_Beginner Jan 05 '17 at 09:51
  • 2
    @Always_Beginner: A blocking call by definition will always be synchronous because it implies that the control flow blocks while waiting for something to complete. A synchronous call may not be blocking however. – Tudor Jan 05 '17 at 12:48
15

Blocking - operation are said to have blocking behavior if it waits for some event to get complete. For example: if a lock is not available a thread may enter a wait state on event till lock is available. Such an operation is said to be blocking.

Synchronous - Synchronous call can be easily understood with an example of http protocol where client waits for reply from server an then proceeds. Synchronous call can be blocking or non blocking.

Asynchronous - A method can asynchronous call other method. After a call it can continue to execute its next instruction. When called method completes it execution it will send an reply/callback to caller method of it's success or failure.

Non-blocking - Non blocking behavior is like checking the condition at that instance. For example- in case of locks if it is not available it will not wait till it is available like blocking operation. Also we need to repeatedly check the availability of locks as there will be no callback like asynchronous calls.

Summary: Blocking is always synchronous.

Synchronous call have blocking operations if it waits for some event to get complete, caller method may enter wait state.

Synchronous call is non blocking, if it repeatedly check for some event to occur before proceeding for next instruction. Caller method does not enter wait state on some event to complete.

Asynchronous call cannot be blocking and it involves callback from called method which needs to handle.

Disha Agrawal
  • 165
  • 2
  • 11
  • 1
    if I put await on Asynchronous call wouldn't that make it blocking ? – abdelhak.ajbouni Mar 10 '20 at 14:14
  • This [blog post](https://luminousmen.com/post/asynchronous-programming-blocking-and-non-blocking) (although examples are in Python) contradicts your statements that Asynchronous can NOT be blocking and that Synchronous is ALWAYS blocking. – NikolaS Aug 06 '20 at 09:20
6

I would classify them as follows:

Blocking - Thread will wait on action untill success or failure (highlight on 'will wait', failure is commonly a timeout)

Synchronous - Thread will complete the action, either by success or failure, before reaching any line after it (highlight on action completion)

Non-blocking - Thread will not wait to complete the action, executes action immediately

Asynchronous - Another thread (either logical or physical) will complete the action or inform it is ready using a callback, will not wait before performing following commands. Note: from here the name asynchronous originates, since you cant be sure in which order the commands will execute

Bar Wachtel
  • 73
  • 1
  • 4
2

synchronous means that the work is done in the thread that calls the function and the method does not return until it is finished.

asynchronous methods return immediately because another thread does the work and raises a flag or fires an event when the work is done.

blocking means that the thread executing a blocking event will wait until the event has occurred. for example you try to read from a socket and none sends you a message. the blocking call will not return until the message has been revived from the socket.

well and nonblocking means the opposite to blocking with implies that nonblocking calls are asynchronous.

Michael Haidl
  • 5,384
  • 25
  • 43
  • thx kronos. it seems that synchronous and blocking has no differences, they both need to wait one work or event done ,then they can continue doing next work or event. Is that right? – diligent Dec 07 '11 at 14:38