13

Node.js, is an asynchronous I/O. what does this actually mean?

Is there different between I create an async function by spawning another thread to do the process?

e.g.

void asyncfuntion(){
    Thread apple = new Thread(){
       public void run(){
           ...do stuff
       }
    }
    apple.start()
}

If there is difference, can I do a asynchronous I/O in javascript?

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
  • 2
    Do you want to do this in Java or in Javascript? You mention node, so I presume you want Javascript. ... that is to say, you ___do___ realize that Java !== Javascript, right? – jcolebrand Sep 29 '11 at 05:25
  • Basically, i know the term asynchronous I/O from node.js, just want to know the different between async IO and fuction. – TheOneTeam Sep 29 '11 at 05:34
  • 1
    But node.js is only the most recent popular implementation of asynchronous I/O. Is there something in particular about it you don't understand? A function is a bit of code that does a task, and asynchronous I/O is a way of fetching data from the disk without blocking. – jcolebrand Sep 29 '11 at 05:43
  • @jcolebrand: i think i am clear now – TheOneTeam Sep 29 '11 at 06:02

6 Answers6

18

Asynchronous I/O

Asynchronous I/O (from Wikipedia)

Asynchronous I/O, or non-blocking I/O, is a form of input/output processing that permits other processing to continue before the transmission has finished.

What this means is, if a process wants to do a read() or write(), in a synchronous call, the process would have to wait until the hardware finishes the physical I/O so that it can be informed of the success/failure of the I/O operation.

On asynchronous mode, once the process issues a read/write I/O asynchronously, the system calls is returned immediately once the I/O has been passed down to the hardware or queued in the OS/VM. Thus the execution of the process isn't blocked (hence why it's called non-blocking I/O) since it doesn't need to wait for the result from the system call, it will receive the result later.

Asynchronous Function

Asynchronous functions is a function that returns the data back to the caller by a means of event handler (or callback functions). The callback function can be called at any time (depending on how long it takes the asynchronous function to complete). This is unlike the synchronous function, which will execute its instructions before returning a value.

...can I do a asynchronous I/O in java?

Yes, Java NIO provides non-blocking I/O support via Selector's. Also, Apache MINA, is a networking framework that also includes non-blocking I/O. A related SO question answers that question.

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • @Gentleman: so it is possible to do a synchronous function with an asynchronous I/O as well, isn't it? – TheOneTeam Sep 29 '11 at 05:10
  • @Kit Ho, sure! Just look for non-blocking examples on the web. – Buhake Sindi Sep 29 '11 at 05:21
  • 3
    That may be akin to asking Kit to just look up examples of heart surgery online and do them. _Knowing_ it is one thing, trying to find it when you have no idea what you're looking at is another. – jcolebrand Sep 29 '11 at 05:26
6

There are several great articles regarding asynchronous code in node.js:

yojimbo87
  • 65,684
  • 25
  • 123
  • 131
  • First link is dead. – Mike M Aug 04 '17 at 09:26
  • 1
    [Asynchronous Code Design with Node.js](https://shinesolutions.com/2011/08/26/asynchronous-code-design-with-node-js/) is this the right link of the first one? – lin Aug 30 '17 at 03:05
  • [Asynchronous Code Design with Node.js](https://shinesolutions.com/2011/08/26/asynchronous-code-design-with-node-js/) is this the right link of the first one? – lin Aug 30 '17 at 03:08
4

In addition to @The Elite Gentleman's answer, node doesn't spawn threads for asynchronous I/O functions. Everything under node runs in a single threaded event loop. That is why it is really important to avoid synchronized versions of some I/O functions unless absolutely necessary, like fs.readSync

You may read this excellent blog post for some insight: http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/

Mehdi
  • 1,075
  • 1
  • 11
  • 24
1

I was investigating the same question since this async IO pattern was very new to me. I found this talk on infoq.com which made me very happy. The guy explains very well where async IO actually resides (the OS -> the kernel) and how it is embedded in node.js as the primary idiom for doing IO. Enjoy!

http://www.infoq.com/presentations/Nodejs-Asynchronous-IO-for-Fun-and-Profit

bennidi
  • 2,092
  • 21
  • 27
0

From https://en.wikipedia.org/wiki/Asynchronous_I/O

Synchronous blocking I/O

A simple approach to I/O would be to start the access and then wait for it to complete.

Would block the progress of a program while the communication is in progress, leaving system resources idle.

Asynchronous I/O

Alternatively, it is possible to start the communication and then perform processing that does not require that the I/O be completed.

Any task that depends on the I/O having completed ... still needs to wait for the I/O operation to complete, and thus is still blocked,

but other processing that does not have a dependency on the I/O operation can continue.

Example:

https://en.wikipedia.org/wiki/Node.js

Node.js has an event-driven architecture capable of asynchronous I/O

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
0

node.js enables a programmer to do asynchronous IO by forcing the usage of callbacks. Now callbacks are similar to the old asynchronous functions that we have been using for a long time to handle DOM events in javascript! e.g.

asyncIOReadFile(fileName, asynFuncReadTheActualContentsAndDoSomethingWithThem);
console.log('IDontKnowWhatIsThereInTheFileYet')
function asynFuncReadTheActualContentsAndDoSomethingWithThem(fileContents) {
    console.log('Now I know The File Contents' + fileContents)
}
//Generally the output of the above program will be following
'IDontKnowWhatIsThereInTheFileYet'
//after quite a bit of time
'Now I know The File Contents somebinarystuff'
Narendra Yadala
  • 9,554
  • 1
  • 28
  • 43