当前位置:网站首页>Why use node
Why use node
2022-07-04 04:23:00 【Careteen】
Catalog
Node What problems can be solved
- Java It's a multithreaded language , There are some shortcomings in dealing with high concurrency ;Node It's single threaded , Can handle this problem well .
- When it comes to threads , Then let's talk about the concepts and differences between process and thread .
What is a process
When a program starts to run , It's a process , Processes include memory and system resources used by running programs and programs .
And a process is composed of multiple threads .
What is thread
A thread is an execution flow in a program , Each thread has its own special register ( Stack pointer 、 Program counter, etc ), But the code area is shared , That is, different threads can perform the same function .
What is multithreading
Multithreading refers to the program contains multiple execution flows , That is, you can run multiple different threads to perform different tasks in one program at the same time , That is to say, a single program is allowed to create multiple threads executing in parallel to complete their own tasks .
Advantages of multithreading
Can improve CPU Utilization ratio . In multithreaded programs , When a thread has to wait ,CPU You can run other threads instead of waiting , This greatly improves the efficiency of the program .
The disadvantages of multithreading
- Threads are also programs , So threads need to take up memory , More threads occupy more memory ;
- Multithreading needs coordination and management , So we need to CPU Time tracking thread ;
- Access to shared resources between threads will affect each other , We must solve the problem of competing for shared resources ;
- Too many threads lead to too complex control , In the end, it may cause a lot of problems Bug;
What is single thread
seeing the name of a thing one thinks of its function , There is only one thread in a program , Any operation is based on this thread , Then there is a blocking problem .
Synchronous and asynchronous
Synchronous and asynchronous focus on the message notification mechanism
- Synchronization is when a call is made , Before we get the results , The call does not return , Once called back , You get the return value . In short, the caller actively waits for the result of the call
- Asynchronous is the opposite , After the caller makes the call, the call returns directly , So no results returned . In other words, when an asynchronous procedure call is issued , Callers don't get immediate results , But after the call is made , The callee passes through the State 、 The notification or callback function handles the call .
Synchronous asynchronous / Blocking non blocking
The above is a boring concept in books , The following is an example in life to understand the relationship between synchronous asynchronous and blocking non blocking .
Now there's a scene , A boy confessed to a girl face to face . In the program, the boy corresponds to the client, that is, the caller , The girl corresponds to the caller on the server .
The following situations may happen after boys confess to girls :
- The girl said that you wait for me to reply to you later . This waiting process for girls ( Callees ) It's synchronous , But for boys ( caller ) It is blocked .
- The girl said you should go back and do something else first , I'll think about it for two days and give you an answer . These two days for girls ( Callees ) It's asynchronous , For boys ( caller ) That is, non blocking , He can confess to other girls or do other things during this period , Let the girl know when she thinks about it .
The above mentioned synchronization blocking 、 Asynchronous non-blocking , Of course, there are two other situations ( Synchronous nonblocking 、 Asynchronous blocking ), We won't discuss .
Node What is it?
stay Node I wrote one in libuv library , The idea of asynchronous non blocking is adopted , The bottom layer is actually implemented by multithreading .
Node Using event driven 、 Non-blocking type I/O Model of , Make it lightweight and efficient
Node One of the biggest reasons for this is the package manager npm
, It is the largest open source library ecosystem in the world .
Node Use scenarios
- Now all companies are promoting Fore and aft end separation in , Use Node Make the middle layer .
- Fore and aft end separation , Then there must be cross domain problems , Use Node Make a layer of interface encapsulation , This process will not cross domains ,Node Also in front-end control , For the front end, it will not span . That is to solve cross domain problems .
- Of course, it can pass
Nginx
Solving cross domain problems . introduce Node It is also mainly to make the back-end responsibilities more focused on logical processing , Some data conversion can be handled by the front end , Make the responsibilities more clear .
- When applications need to deal with high concurrency scenarios , In multithreaded languages, such as Java It consumes a lot of memory , Is not recommended .
- Chat server : A lot of concurrent input and output
- E-commerce website : No complex logic , A lot of concurrent output ( A large number of users browse the website at the same time )
Node Basics
todo
- nrm
- nvm
Node series - Next section Details of the event cycle
边栏推荐
猜你喜欢
Activiti7 task service - process variables (setvariable and setvariablelocal)
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
软件测试是干什么的 发现缺陷错误,提高软件的质量
02 specific implementation of LS command
Katalon中控件的参数化
精品网址导航主题整站源码 wordpress模板 自适应手机端
Ppt tutorial, how to save a presentation as a PDF file in PowerPoint?
量子力学习题
统计遗传学:第三章,群体遗传
Idea modify body color
随机推荐
[book club issue 13] packaging format of video files
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
批处理初识
Pointer array and array pointer
Keysight N9320B射频频谱分析仪解决轮胎压力监测方案
2020 Bioinformatics | TransformerCPI
LevelDB源码解读-SkipList
Idea configuration 360zip open by default -- external tools
hbuildx中夜神模拟器的配置以及热更新
R语言中如何查看已安装的R包
毕业设计项目
Rhcsa-- day one
SDP中的SPA
Spa in SDP
leetcode刷题:二叉树05(翻转二叉树)
ROS2中CMake编译选项的设置
Getting started with the go language is simple: go implements the Caesar password
Restore the subtlety of window position
Parameterization of controls in katalon
RHCSA 08 - automount配置