'Does async programming mean multi-threading?

lets talk about JavaScript code which has setInterval methods every 2 sec.

I also have a onblur animation event for some control.

In a case where onblur occurs (+ animation), I might get the setInterval function.

Question:
Does async programming mean multi-threading? (in any way?)



Solution 1:[1]

No. It means literally what it means-- asynchronous. Understanding the difference between asynchronous programming and thread-based programming is critical to your success as a programmer.

In a traditional, non-threaded environment, when a function must wait on an external event (such as a network event, a keyboard or mouse event, or even a clock event), the program must wait until that event happens.

In a multi-threaded environment, many individual threads of programming are running at the same time. (Depending upon the number of CPUs and the support of the operating system, this may be literally true, or it may be an illusion created by sophisticated scheduling algorithms). For this reason, multi-threaded environments are difficult and involve issues of threads locking each other's memory to prevent them from overrunning one another.

In an asychronous environment, a single process thread runs all the time, but it may, for event-driven reasons (and that is the key), switch from one function to another. When an event happens, and when the currently running process hits a point at which it must wait for another event, the javascript core then scans its list of events and delivers the next one, in a (formally) indeterminate (but probably deterministic) order, to the event manager.

For this reason, event-driven, asynchronous programming avoids many of the pitfalls of traditional, multi-threaded programming, such as memory contention issues. There may still be race conditions, as the order in which events are handled is not up to you, but they're rare and easier to manage. On the other hand, because the event handler does not deliver events until the currently running function hits an idle spot, some functions can starve the rest of the programming. This happens in Node.js, for example, when people foolishly do lots of heavy math in the server-- that's best shoved into a little server that node then "waits" to deliver the answer. Node.js is a great little switchboard for events, but anything that takes longer than 100 milliseconds should be handled in a client/server way.

In the browser environment, DOM events are treated as automatic event points (they have to be, modifying the DOM delivers a lot of events), but even there badly-written Javascript can starve the core, which is why both Firefox and Chrome have these "This script is has stopped responding" interrupt handlers.

Solution 2:[2]

A single threaded event loop is a good example of being asynchronous in a single threaded language.

The concept here is that you attach doLater callback handlers to the eventLoop. Then the eventLoop is just a while(true) that checks whether the specific timestamp for each doLater handler is met, and if so it calls the handler.

For those interested, here is a naive (and horribly inefficient toy) implementation of a single threaded event loop in JavaScript

This does mean that without any kind of OS thread scheduler access of your single thread, your forced to busy wait on the doLater callbacks.

If you have a sleep call you could just do sleep until the next doLater handler which is more efficient then a busy wait since you deschedule your single thread and let the OS do other things.

Solution 3:[3]

Only in the sense that it executes code haphazardly and risks race conditions. You will not get any performance benefits from using timeouts and intervals.

However, HTML5's WebWorkers do allow for real multithreading in the browser: http://www.html5rocks.com/en/tutorials/workers/basics/

Solution 4:[4]

  1. No asynchronous programming doesn't mean multithreading specifically.

  2. For achieving multiple tasks at the same time we use multi-threading and other is event loop architecture in node js.

  3. JavaScript is synchronous and single threaded and that is why node js is also single threaded but with event loop node do a non-blocking I/O operations. Node.js uses the libuv library that uses a fixed-sized thread pool that handles the execution of parallel tasks.

Thread is a sequence of code instructions and also the sub unit of process.

In multi-threading, processes have multiple threads. In single threading, processes have single thread.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elf Sternberg
Solution 2
Solution 3 Jeffrey Sweeney
Solution 4 Bilal Yaqoob