Green Files

What is the difference between asynchronous and synchronous communication?

Day: 06/02/2022 - Time: 15:29:12

Synchronous and Asynchronous communications are two distinct transmission synchronization methods, each of which has its advantages and disadvantages.

Analogy

Let's imagine that a sender is sending a text message through a tube and that the message is sent using balls, each ball with a letter. When the balls arrive at the receiver, the message has to be reassembled, that is, the balls have to be placed in the correct order, in order to get the message again.

In Asynchronous communication, each ball has a sequence number, which allows it to be placed in its position. This allows balls to be sent and received in any order, as this sequence number identifies the position of each ball (letter) in the message. In synchronous communication, the balls must be sent and received synchronously, maintaining a well-defined order: the first ball (letter) to be sent must be the first to be received and so on.

Synchronous Communication

In a synchronous communication, the sender and receiver are synchronized by the same clock, the receiver continuously receives (even when no bits are transmitted) the data in time with which the sender sends them.

Simple example in JavaScript

function foo(){ console.log("foo"); } function bar(){ console.log("bar"); } function baz(){ console.log("baz"); } foo(); bar(); baz();

These functions will be executed in order, bar will not be executed until foo has finished and baz in turn will only be executed after bar has finished, thus there is a sync.

Asynchronous communication

In an asynchronous communication the synchronization is different, the sender sends the data stream and periodically inserts a signal element, commonly called a flag, so that it is possible to distinguish where it starts (the transmission start information is the start-bit) and ends (the end of transmission information is the stop-bit) the data block and what is its position in the transmitted data sequence.

Example

function foo(){ console.log("Running something asynchronous here"); } function bar(){ setTimeout(foo, 0); console.log("something around here"); console.log("..."); } bar();

Here there is not the same synchronization as in another example, one function will not wait for the other to finish. In this example we use the setTimeout function to enqueue foo which will execute 0 ms after the event loop, the event loop will be unavailable until it completes its current execution stack - in this case the remainder of bar, which includes the two console.log statements . After the bar ends the event loop is free, so foo can be executed.

Output:

something around here ... Running something asynchronous here

When and why should I use them? Much of the decision-making process on this involves common sense, a set of factors, an asynchronous communication can be used when the answer does not have to be delivered urgently, this type of communication is suitable for situations where it is not necessary to urgent data delivery, on the other hand, would not be appropriate to use this type of communication if you need immediate interaction. On the other hand, synchronous communication is ideal when spontaneity is needed, such as in a real-time conversation between two or more people.

Both synchronous and asynchronous communication have their advantages and disadvantages, using one or the other will depend on the needs of your application.

References:

https://pt.stackoverflow.com/questions/51268/qual-a-diferen%c3%a7a-entre-comunica%c3%a7%c3%a3o-ass%c3%adncrona-e-s%c3%adncrona

https://msdn.microsoft.com/en-us/library/ms734701(v=vs.110).aspx

http://en.wikipedia.org/wiki/Asynchronous_serial_communication

http://en.wikipedia.org/wiki/Comparison_of_synchronous_and_asynchronous_signalling

http://www.codeproject.com/Articles/127660/What-are-actually-synchronous-asynchronous-operati

GO UP
GO TO INDEX