While I was learning about the event loop in Node.js I heard the definition of ''multi threading'' and I really don't know what they mean or what they mean. If someone could explain it to me I would really appreciate it. First of all, Thanks.
While I was learning about the event loop in Node.js I heard the definition of ''multi threading'' and I really don't know what they mean or what they mean. If someone could explain it to me I would really appreciate it. First of all, Thanks.
Before defining multithread , let 's take a look at what a program is.
A program is a set of data and functions; no more no less. In most languages, data is accessed through variables.
A variable, in turn, can be global (it can be accessed from anywhere in the program), or local (it can only be accessed from the function in which it was defined).
A thread or thread of execution can be considered a specific point of execution of a program. At any given time, a thread can only be executing a certain code statement; From that specific point, we can access all the global variables, but only the local variables of the function we are in.
In addition, a thread has other extra information associated with it: the current call stack (the information of the calling functions), together with the arguments used.
A single threaded application , such as Nodejs, only has one thread ; There is only one execution point at any given time.
A multi-threaded application , on the other hand, has more than one point of execution at a given time. It still has a single set of global variables, but it has more than one function executing simultaneously , and each execution point has its own calling function information, its own set of arguments, and its own set of local variables.
If we run a multi-threaded application on a machine with more than 1 CPU , the performance of that application will be higher than a single- threaded application ; each thread can be executed on one CPU, so you can execute more than one task at a time.
If we only have 1 CPU, it distributes its time among all the threads of all the programs in execution, so we will not obtain an advantage from the use of several threads.
The main drawback of multi-threaded applications is maintaining order in accessing shared resources (such as global variables). Focusing on these, they can be accessed from all threads (unlike local ones). What happens if one thread changes a global variable, while another thread is reading or writing it? You can check out the classic Philosophers' Dinner Problem .
There is no method for deciding whether a program will be better off using one thread or multiple threads. Almost all problems can be approached from either of the two methods. With multiple threads, in theory , you'll get better performance; with a thread, greater simplicity and ease of work.
Likewise, a poorly designed multi-threaded program may be slower than a single-threaded one. And, for certain tasks (that can be divided into blocks of work, without depending on each other), the use of several threads provides a much higher performance .
To put it simply, a thread is the basic unit of execution: When a program is executed with at least one thread (also called "thread of execution") it can be said to be "single thread".
A single threaded program is not doing parallel/concurrent work. One way to perform several tasks at the same time (in parallel, concurrently) is from the program to generate new threads, for example the new Thread() instructions in java create new threads that will be executed "in parallel" then we have a "multi thread" environment where in addition to the execution thread of the main program, the children threads launched by it are running.
In the case of web servers, they traditionally created a new thread to attend to a new user request. If there were thousands of user requests, the server created thousands of threads in response to those requests.
Managing so many threads is not free for the server, threads have their life cycles, memory management, synchronization issues, etc. Also, within a thread, the execution cycle is "blocking" (an instruction must finish executing before moving on to the next one).
With the appearance of NodeJS, a more efficient service model based on the Event Loop with a single thread is sought to meet all user requirements. To have "non-blocking" execution, "expensive" operations such as input/output accesses are logged along with a callback function which the server should call when the input/output operation is complete.
Thanks to this strategy, at least two things are gained with respect to multi-thread web servers: a) The time between the I/O operation and the callback is used so that the event loop can continue serving user requests. b) By having a simple thread attending to all user requests, the use of resources after setup is constant. (no resources are spent to create/destroy/synchronize threads)
That's a pretty superficial answer, hope it helps anyway.
A multithreaded application is an application that has several subprograms running independently and simultaneously.
In Node.js, there is only one thread of JavaScript execution.
Here it explains better how it works https://bytearcher.com/articles/why-in-nodejs-lot-of-asynchronous-operations/