当前位置:网站首页>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
NginxSolving 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
边栏推荐
- VIM add interval annotation correctly
- Flink学习8:数据的一致性
- Keysight N9320B射频频谱分析仪解决轮胎压力监测方案
- [microservice openfeign] use openfeign to remotely call the file upload interface
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
- C language bidirectional linked list first edition
- laravel admin里百度编辑器自定义路径和文件名
- Huawei cloud Kunpeng engineer training (Guangxi University)
- 资深开发人员告诉你,怎样编写出优秀的代码?
猜你喜欢

leetcode刷题:二叉树09(二叉树的最小深度)

【微服务|openfeign】@FeignClient详解

Getting started with the go language is simple: go implements the Caesar password

干货!基于GAN的稀有样本生成

Leetcode skimming: binary tree 08 (maximum depth of n-ary tree)

Leetcode brush question: binary tree 06 (symmetric binary tree)

laravel admin里百度编辑器自定义路径和文件名

Leetcode skimming: binary tree 07 (maximum depth of binary tree)

Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..

Redis:哈希hash类型数据操作命令
随机推荐
LevelDB源码解读-SkipList
96% of the collected traffic is prevented by bubble mart of cloud hosting
架构实战营 - 第 6 期 模块九之毕业设计
[microservices openfeign] two degradation methods of feign | fallback | fallbackfactory
Flink learning 6: programming model
【微服务|openfeign】使用openfeign远程调用文件上传接口
JS实现文字滚动 跑马灯效果
RHCSA 08 - automount配置
leetcode刷题:二叉树05(翻转二叉树)
【读书会第十三期】多媒体处理工具 FFmpeg 工具集
Common methods of threads
Leetcode skimming: binary tree 09 (minimum depth of binary tree)
资深开发人员告诉你,怎样编写出优秀的代码?
Three years of graduation, half a year of distance | community essay solicitation
Interpretation of leveldb source code skiplist
程序员远程办公喜忧参半| 社区征文
网络 - VXLAN
R语言dplyr中的Select函数变量列名
Redis cluster uses Lua script. Lua script can also be used for different slots
One click compilation and deployment of MySQL