3

I don't understand the difference in java multithread system and Nodejs mutltithread system in terms of peformance and resource sharing. As NodeJS use event loop single thread for your program but behind the scene it assign task to different threads like file reading or db queries. so there it uses multithread and threadpool (similar to Java?). But whenever we compare the performance, NodeJS apps are far better than other multi threading system.

How actually NodeJS handle multithread programming challenges like overflow or locking thread. How does it share resources between threads for example I am accessing same file at same time with two I/O so there will be two thread accessing to one resource, does it apply in NodeJS multithreading system? Or I have misunderstood this point?

AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
Shad
  • 969
  • 1
  • 10
  • 18
  • 2
    "But whenever we compare the performance, NodeJS apps are far better than other multi threading system." - reference desired. – ChiefTwoPencils Feb 15 '16 at 08:39
  • 2
    Accessing the same file multiple times at the same time (by firing off multiple I/O operations against the same file at once) is an interesting question. – Thilo Feb 15 '16 at 08:42
  • 4
    @ChiefTwoPencils Just adding few benchmark reference although very old https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/ https://dzone.com/articles/performance-comparison-between https://strongloop.com/strongblog/node-js-is-faster-than-java/ – Shad Feb 15 '16 at 08:47
  • Java and C++ tags have nothing to do here – AdamSkywalker Feb 15 '16 at 08:58
  • @AdamSkywalker thanks for removing those tag, I don't want to make it java vs node discussion, just wanted more clear understanding on NodeJS Multithreading system. I refer Java here because just wanted to compare multithreading in other system with NodeJS – Shad Feb 15 '16 at 09:14
  • Java thread model is based on native OS multi-threading model with time slicing and other tricks. NodeJS uses event-loop as far as I know, but I don't know how lock problems are solved or do they even exist. – AdamSkywalker Feb 15 '16 at 09:49
  • NodeJS uses event loop but it is multithreaded, this guy has explained it better http://softwareengineeringdaily.com/2015/08/02/how-does-node-js-work-asynchronously-without-multithreading – Shad Feb 15 '16 at 10:16
  • Very Interesting, I am from C++ background, I have did Java now working on NodeJS and really it is question for me also how the NodeJS is multithreaded and how does differ from Java or C++ multithreading system. –  Feb 16 '16 at 11:23
  • 1
    Please add the Java and C++ tag again because NodeJS V8 engine is written in C++ and if someone from C++ or Java background working on NodeJS core can provide answer. So by adding these tag, more chances to reach Java and C++ guys. I am very much interested to see answers on this. –  Feb 16 '16 at 11:27
  • @chris just follow the link in a previous comment. it has the answers. – AdamSkywalker Feb 16 '16 at 13:24
  • @AdamSkywalker There is good explanation that nodejs is multithreaded in background but there is no comparison that how nodejs and traditional IIS, tomcat or Java multithreads handling is different, and if Nodejs also uses multithreads to handle the I/O but if that's the case how come people say that Node.js uses less threads than a traditional approach? – Shad Feb 17 '16 at 07:18

2 Answers2

3

Nodejs is using libuv for this purposes, which is written on C.

That's why you can't compare Java and Nodejs, we can say, that Nodejs is using low-level mechanism to make async IO.

libuv designed for nodejs, but it can be used in any projects.

You mentioned async disc operations - you can find good post about it here.

Short version:

use asynchronous disk I/O, instead of the synchronous disk calls in the disk thread in 0.16.x versions.

What does this means? It means that you can use same approach(async low-level IO operations) and i bet you can raise same speed with, for example, Java.

The other thing you mentioned - event loop. There are nothing hard - it is easy for understanding, for example you can read this good post.

alexey
  • 1,381
  • 9
  • 19
  • http://stackoverflow.com/questions/10680601/nodejs-event-loop, I found this question on libuv and event loop. It will add more detail to your answer. Thanks for pointing me to libuv. – Shad Apr 12 '16 at 03:23
3

here is my 2 pence worth ...

Multi-threading capability

  • Truth : Node.js ( currently ) does not provide native support for multi-threading in the sense of low level execution/processing threads. Java, and its implementations /frameworks, provides native support for multi-threading, and extensively too ( pre-emption, multi-tenancy, synchronous multi-threading, multi-tasking, thread pools etc )

  • Pants on Fire(ish) : lack of multi-threading in Nodejs is a show stopper. Nodejs is built around an event driven architecture , where events are produced and consumed as quickly as possible. There is native support for functional call backs. Depending on the application design, this highlevel functionality can support what could otherwise be done by thread. s

  • For serverside applications, at an application level , what is important is the ability to perform multiple tasks, concurrently :ie multi-tasking. There are several ways to implement multi-tasking . Multi-threading being one of them, and is a natural fit for the task. That said, the concept of “multi -threading “ is is a low level platform aspect. For instance multi-threaded platform such as java , hosted/running on a single core process server ( server with 1 CPU processor core) still supports multi-multi at the application level, mapped to multi-threading at the low level , but in reality , only a single thread can execute at any ontime. On a multi-core machine with sa y 4 cores , the same multi-tasking at application level is supported , and with up to 4 threads can executing simultaneously at any given time. The point is, in most cases, what really matters is the support for mult-tasking, which is not always synonymous with multi-threading.

  • Back to node.js , the real discussion should be on application design and architecture , and more specifically, support for MULTI-TASKING. In general, there is a whole paradigm shift between serverside applications and clientside or standalone applications, more so in terms of design, and process flow. Among other things, server side applications need to run along side other applications( onthe server ), need to be resilient and self contained (not affect the rest o f the server when the application fails or crashes ) , perform robust exception handling ( ie recover from errors, even critical ones ) and need to perform multiple tasks .

  • Just the ability to support multi-tasking is a critical capability for any server side technology . And node.js has this capability, and presented in a very easy to use packaging . This all means that design for sever side applications need to focus more on multi-tasking, and less on just multi-threading. Yes granted, working on a server-side platform that supports multi-threading has its obvious benefits ( enhanced functionality , performance) but that alone does not resolve the need to support multi-tasking at the application level . Any solid application design for server side applications ,AND node.js must be based on multi-tasking through event generation and consumption ( event processing). In node.js , the use of function callbacks, and small event processors (as functions ), with data checkpointing ( saving processing data , in files or data bases) across event processing instances is key.
young chisango
  • 103
  • 1
  • 6