当前位置:网站首页>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
边栏推荐
- I Build a simple microservice project
- Msgraphmailbag - search only driveitems of file types
- 2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
- leetcode刷题:二叉树08(N叉树的最大深度)
- Graduation project: design seckill e-commerce system
- PPt 教程,如何在 PowerPoint 中将演示文稿另存为 PDF 文件?
- STM32外接DHT11显示温湿度
- C language bidirectional linked list first edition
- Brief explanation of depth first search (with basic questions)
- [Logitech] m720
猜你喜欢
leetcode刷题:二叉树04(二叉树的层序遍历)
Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
Keysight N9320B射频频谱分析仪解决轮胎压力监测方案
普源DS1000Z系列数字示波器在通信原理实验中的应用方案
R语言dplyr中的Select函数变量列名
Msgraphmailbag - search only driveitems of file types
R语言中如何查看已安装的R包
微信公众号无限回调授权系统源码
干货!基于GAN的稀有样本生成
随机推荐
02 specific implementation of LS command
Katalon中控件的参数化
ROS2中CMake编译选项的设置
Katalon使用script实现查询List大小
What does software testing do? Find defects and improve the quality of software
ctf-pikachu-XSS
How to add custom API objects in kubernetes (1)
思考的小记录
软件测试是干什么的 发现缺陷错误,提高软件的质量
[Yugong series] go teaching course 002 go language environment installation in July 2022
Flink学习8:数据的一致性
干货!基于GAN的稀有样本生成
【webrtc】m98 ninja 构建和编译指令
统计遗传学:第三章,群体遗传
Leetcode skimming: binary tree 08 (maximum depth of n-ary tree)
浅谈一篇优质的小红书文案需要具备什么
指针数组和数组指针
2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
网络 - VXLAN
程序员远程办公喜忧参半| 社区征文