Synchronous VS Asynchronous

If two processes are running Synchronously, it means that second process will wait until or unless first process complete.

If two processes are running Asynchronously, it means second process will not wait for first process result and will be executed.

I will explain a bit more detail in below for each communication.

Synchronous Communication

If two processes are running Synchronously, it means that second process will wait until or unless first process complete. Synchronous Transmission is efficient, reliable and is used for transferring a large amount of data. It provides real-time communication between connected devices. Chat Rooms, Video Conferencing, telephonic conversations, as well as face to face interactions, are some of the examples of Synchronous Transmission.

Check the sample php snippet below for synchronous communication.

<?php
echo(“Before the first file is read.”);
$fileContents = file_get_contents(“sample.txt”);

//You can’t do anything while the file is being read, your script is ‘stuck’.

echo(“After the first file has completed reading.”);

echo(“Before the second file is read.”);

$fileContents = file_get_contents(“sample2.txt”);
//You can’t do anything while the file is being read, your script is ‘stuck’.
echo(“After the second file has completed reading.”);

==========================
Output always looks like this:
Before the first file is read.
After the first file has completed reading.
Before the second file is read.
After the second file has completed reading.

Asynchronous Communication

If two processes are running Asynchronously, it means second process will not wait for second process result and will be executed. It is simple, fast, economical and does not require a 2-way communication. Letters, emails, forums, televisions and radios are some of the examples of Asynchronous Transmission.

Check the js snippet below for asynchronous communication

console.log(“Before the first file is read.”);

hypotheticalFileGetContents(“sample.txt”, function(fileContents){
//fileContents now contains the file contents, this function is only called when the file read in the background has finished
console.log(“After the first file has completed reading.”);
});

//You’ve now told it to start the first read, but it won’t ‘block’ your script execution. It will do the read in the background, and immediately move on with the rest of your code.

console.log(“Before the second file is read.”);

hypotheticalFileGetContents(“sample2.txt”, function(fileContents){
//fileContents now contains the file contents, this function is only called when the file read in the background has finished
console.log(“After the second file has completed reading.”);
});

=======================
Output could look something like this:
Before the first file is read.
Before the second file is read.
After the first file has completed reading.
After the second file has completed reading.

By Yuuma



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム