search

Home  >  Q&A  >  body text

javascript - A comparison of node and java concurrency scenarios, please help explain

AssumptionSingle coreA node program is installed under the CPU. The program is very simple. It is an http server. When a request comes in, the result is returned with a delay of 5 seconds (it can be understood that it is done in 5ssynchronously Issues)

When 3 requests come in at the same time, which scenario will the result be:
Scenario 1: The first request gets the result after 5 seconds, the second and third requests after 10s and 15s Get the results
Scenario 2: All requests will get the results after 5 seconds

If you change to java tomcat, what kind of scenario will it be? Thank you everyone!

曾经蜡笔没有小新曾经蜡笔没有小新2789 days ago592

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-17 10:10:00

    If it is 同步的 delay 5 seconds, and node.js is a simple single-thread operation, without adding cluster and other optimizations

    const start = Date.now();
    while(Date.now() - start < 5000) {
        ;   
    }
    return res.send();

    So, when multiple requests are initiated at the same time, scenario 1

    But usually in node.js it is rare to encounter complete blocking 同步等待 5 秒, if it is the following situation:

    setTimeOut(() => { return res.send(); }, 5000);

    The result is of course场景2

    As for Java, concurrency is supported by multi-threading. When the performance is sufficient, for example, the concurrency is only 3, it can be roughly regarded as: Scenario 2. Of course, due to the thread overhead involved, after the concurrency increases, its memory usage will far exceed the node.js model

    In fact, this is why we say node.js is more suitable for IO-intensive and low-computing scenarios. You can refer to my other answer

    reply
    0
  • Cancelreply