知道了什么是线程。
线程是进程中独立执行的单位。线程是操作系统进行运算调度的最小单位,是进程中的一个实体,由cpu调度的分派的基本单位。也是进程中的一个独立执行单元JS是单线程的,因为设计成这样的,其在某一时间只能执行一个任务,会阻塞其他任务进行。当新的任务加入的时候,会放在任务队列的最后面,当其前面的都执行完了,才会执行这个任务。突然,有一个任务耗时很长,而后面的任务又在等待执行,那这样就会造成浏览器“假死”,这一现象就相当于你平常在浏览网站的时候,突然怎么点击都没有反应一样。为了避免假死,过了好久,浏览器会给你个弹框,会提示要能不能结束该进程。
这个时候,你就会想,当我进行I\O(输入\输出)操作的时候,JS岂不是要等着我输入,才会进行下面的操作吗?
其实不然,在设计者发现当用户要进行输入还未输入的时候,将这个任务先挂起来,当用户真的开始输入的时候,在来执行这个任务。这个会挂起任务,在等待输入,然后将挂起的任务开始执行的这个机制,被称为Event Loop!JS在运行的时候,其的引擎除了提供一个运行线程(因为JS是单线程,但是引擎并不是,引擎可以提供多线程,js脚本在线程上跑,别的线程在后台进行配合),还提供一个消息队列。这个相当于你在挂号一样的,你挂好号了,然后去看医生,医生那边知道你的消息,但是还没有叫你去看病一样。那你现在就在等待医生叫到你的号码,让你去看病。在程序中,就表现为你的号码,就是你挂的号相当于消息,而当医生叫到你,然后你在去医生那边看病这一行为为消息的响应,也就是程序中的回调函数。可以简单来说,你就是个回调函数,而触发你去医生那边看病,是不是因为医生叫到你的号码,你才去的,不然你只能排队。同时也要记得当医生叫到你的号码的时候,你没有去,医生是不是过一会就会叫下一个号。是不是说明你这个号码没有什么用处,因为这个号码的作用是让你去看病,但是你没有去看,说明这号码没有发挥自己的用处。这个对应在JS就是消息的遗失,一般消息跟回调函数是一起存在的,不然消息没有什么作用。
setTimeout(),会在指定时间向消息队列插入一条消息,这个相当于我们在看医生的时候,是不是看见有时候自己被插队一样。当医生手上还在看病人的时候,是不是看完这个病人再看他,在JS中就是当消息队列中所有消息都处理完,在处理setTimeout的消息一样。当然医生没有病人再看,你说让他插队看,也是跟排队看一样。
Event Loop机制:内部的一个循环,一轮一轮地处理消息队列中的消息,相当于执行消息的回调函数。
update:虽然之前写过。但是发现自己并不是完全搞明白。所以又重写了一篇。查了一天的资料在写的。11.23/2017
Importent thing
- 我这两天浏览文章的时候,才发现。js是单线程的,但是浏览器并不是的。以前就知道页面是如何在网页中展示的过程。简单提一下
- Client send request (the client is meaning web client)
- The server received the request of client
- The server analyze the request 。this process is the server get request's parameters ,then use middleware to handle those parameters
- When the serve finish the handle process,it will send the result to client
- If the client get result,the web begin invoke process to analyze and show the result to users in a friendly way 。
- During showing the web page, we should know how webClient to handle the result
- If the server send the client a directory includes html、css or js files,the web will invoke the GUI render engine thread to analyze files。First,the GUI engine analyze the html files and render the DOM(document object model) tree。If web find the tag named like these"link,script,img...",it will invoke the asynchronous http request thread to request this resource。Second,when the web received css files ,the GUI engine analyzes these files to make the DOM tree into the render tree. Finally,the web will invoke js files if these existed。
ok ,I forget something。
When client sent request to server,there is a course.
The client send request use url,and the url will be analyzed by DNS serve provider。The DNS serve provider will transform url
- Users input url,but web can't understand the url ,so web will transform url to the IP .
- First the web find host file if it has existed.The host file records IP infomation.If not,web will send the request of DNS(domain name server) to local DNS server .If local DNS server also can't find the demain name is correspond to the IP .The local DNS server also sends request to root server to get infomation.If not,the root will send request to the com server 。Finally,the com server will return the request service address to the local DNS server and the local DNS server get the relationship between domain and the IP;
reference 👈
reference1 👈
- First the web find host file if it has existed.The host file records IP infomation.If not,web will send the request of DNS(domain name server) to local DNS server .If local DNS server also can't find the demain name is correspond to the IP .The local DNS server also sends request to root server to get infomation.If not,the root will send request to the com server 。Finally,the com server will return the request service address to the local DNS server and the local DNS server get the relationship between domain and the IP;
- The web get IP address now.It can send a request to website to get information users want.Yes,there is also a course . The web will send a request with a random port to website web programmer to request the TCP connection.
- The connection request arrives at remote server by crossing physical machine and network card 。The request arrives at the TCP/IP agreement stack 。Finally, web programmer accept the request and build the connection of the TCP/IP 。
- Now,the connection is ok. And the client can send a request to the remote server to get information.
A picture is worth a thousand words
Now,the websocket Protocol is useful. You can see this article1 article2 👈
Other Reference
the threads of web
When web work,there are five threads to work .
The threads are the GUI render thread ;setTimeOut trigger thread ;js engine thread;async http request thread;event trigger thread;
and you should know the javascript engine thread is opposed to the GUI render thread
more infomation