1.

Explain difference between blocking and non blocking program?

Answer»

Blocking program 

  1. executes very much in sequence
  2. Easier to implement the logic

 Example: Let us create text file named blk.txt

blk.txt

 A technology that has emerged as the frontrunner for handling Big Data processing is Hadoop.

Create a javascript code as follows and save as blk.js

VAR fs = require("fs"); var data = fs.readFileSync('blk.txt'); console.log(data.toString()); console.log('Program Ended');

Execute the code. The result is 

A technology that has emerged as the frontrunner for handling Big Data processing is Hadoop.

Program Ended

In this example, program BLOCKS until it reads the file and then only it proceeds to next statement or end the program.

Non Blocking program

  1. Do not execute in sequence
  2. In CASE a program needs to use any data to be processed, it should be kept within the same block to MAKE it sequential execution.

Example: Use the same input file defined for blocking code example.

Create a javascript code as follows and save as nblk.js

var fs = require("fs"); fs.readFile('blk.txt', function (err,data) { if (err) return console.error(err); console.log(data.toString()); }); console.log("Program Ended"); 

Execute the code. The result is

Program Ended

A technology that has emerged as the frontrunner for handling Big Data processing is Hadoop.

This example shows that program doesn’t WAIT for file reading and prints “Program Ended” first. At the same time, the program without blocking continues to read the file.



Discussion

No Comment Found