当前位置:网站首页>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
边栏推荐
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
- 【罗技】m720
- Flink learning 8: data consistency
- [csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
- RHCSA 07 - 用户与群组管理
- Keysight N9320B射频频谱分析仪解决轮胎压力监测方案
- [microservice openfeign] use openfeign to remotely call the file upload interface
- DP83848+网线热拔插
- Two commonly used graphics can easily realize data display
猜你喜欢
PostgreSQL users cannot create table configurations by themselves
leetcode刷题:二叉树06(对称二叉树)
I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
Two commonly used graphics can easily realize data display
一个漂亮的API文档生成工具
leetcode刷题:二叉树08(N叉树的最大深度)
毕业设计项目
微信脑力比拼答题小程序_支持流量主带最新题库文件
统计遗传学:第三章,群体遗传
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
随机推荐
01 QEMU starts the compiled image vfs: unable to mount root FS on unknown block (0,0)
STM32外接DHT11显示温湿度
一个漂亮的API文档生成工具
Global exposure and roller shutter exposure of industrial cameras
Unity draws the trajectory of pinball and billiards
仿《游戏鸟》源码 手游发号评测开服开测合集专区游戏下载网站模板
Huawei cloud Kunpeng engineer training (Guangxi University)
C语言单向链表练习
ctf-pikachu-XSS
Two commonly used graphics can easily realize data display
DP83848+网线热拔插
(指针)编写函数void fun(int x,int *pp,int *n)
What does software testing do? Find defects and improve the quality of software
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
(指針)自己寫一個比較字符串大小的函數,功能與strcmp類似。
毕业设计:设计秒杀电商系统
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
【webrtc】m98 ninja 构建和编译指令
Pointer array and array pointer