知道了什么是線程。
線程是進(jìn)程中獨立執(zhí)行的單位也榄。線程是操作系統(tǒng)進(jìn)行運算調(diào)度的最小單位尖飞,是進(jìn)程中的一個實體杉编,由cpu調(diào)度的分派的基本單位。也是進(jìn)程中的一個獨立執(zhí)行單元JS是單線程的,因為設(shè)計成這樣的,其在某一時間只能執(zhí)行一個任務(wù),會阻塞其他任務(wù)進(jìn)行蛛倦。當(dāng)新的任務(wù)加入的時候,會放在任務(wù)隊列的最后面啦桌,當(dāng)其前面的都執(zhí)行完了溯壶,才會執(zhí)行這個任務(wù)。突然甫男,有一個任務(wù)耗時很長且改,而后面的任務(wù)又在等待執(zhí)行,那這樣就會造成瀏覽器“假死”板驳,這一現(xiàn)象就相當(dāng)于你平常在瀏覽網(wǎng)站的時候又跛,突然怎么點擊都沒有反應(yīng)一樣。為了避免假死若治,過了好久慨蓝,瀏覽器會給你個彈框感混,會提示要能不能結(jié)束該進(jìn)程。
這個時候礼烈,你就會想弧满,當(dāng)我進(jìn)行I\O(輸入\輸出)操作的時候,JS豈不是要等著我輸入此熬,才會進(jìn)行下面的操作嗎庭呜?
其實不然,在設(shè)計者發(fā)現(xiàn)當(dāng)用戶要進(jìn)行輸入還未輸入的時候犀忱,將這個任務(wù)先掛起來募谎,當(dāng)用戶真的開始輸入的時候,在來執(zhí)行這個任務(wù)阴汇。這個會掛起任務(wù)数冬,在等待輸入,然后將掛起的任務(wù)開始執(zhí)行的這個機制搀庶,被稱為Event Loop拐纱!JS在運行的時候,其的引擎除了提供一個運行線程(因為JS是單線程地来,但是引擎并不是,引擎可以提供多線程熙掺,js腳本在線程上跑未斑,別的線程在后臺進(jìn)行配合),還提供一個消息隊列币绩。這個相當(dāng)于你在掛號一樣的蜡秽,你掛好號了,然后去看醫(yī)生缆镣,醫(yī)生那邊知道你的消息芽突,但是還沒有叫你去看病一樣。那你現(xiàn)在就在等待醫(yī)生叫到你的號碼董瞻,讓你去看病寞蚌。在程序中,就表現(xiàn)為你的號碼钠糊,就是你掛的號相當(dāng)于消息挟秤,而當(dāng)醫(yī)生叫到你,然后你在去醫(yī)生那邊看病這一行為為消息的響應(yīng)抄伍,也就是程序中的回調(diào)函數(shù)艘刚。可以簡單來說截珍,你就是個回調(diào)函數(shù)攀甚,而觸發(fā)你去醫(yī)生那邊看病箩朴,是不是因為醫(yī)生叫到你的號碼,你才去的秋度,不然你只能排隊炸庞。同時也要記得當(dāng)醫(yī)生叫到你的號碼的時候,你沒有去静陈,醫(yī)生是不是過一會就會叫下一個號燕雁。是不是說明你這個號碼沒有什么用處,因為這個號碼的作用是讓你去看病鲸拥,但是你沒有去看拐格,說明這號碼沒有發(fā)揮自己的用處。這個對應(yīng)在JS就是消息的遺失刑赶,一般消息跟回調(diào)函數(shù)是一起存在的捏浊,不然消息沒有什么作用。
setTimeout(),會在指定時間向消息隊列插入一條消息撞叨,這個相當(dāng)于我們在看醫(yī)生的時候金踪,是不是看見有時候自己被插隊一樣。當(dāng)醫(yī)生手上還在看病人的時候牵敷,是不是看完這個病人再看他胡岔,在JS中就是當(dāng)消息隊列中所有消息都處理完,在處理setTimeout的消息一樣枷餐。當(dāng)然醫(yī)生沒有病人再看靶瘸,你說讓他插隊看,也是跟排隊看一樣毛肋。
Event Loop機制:內(nèi)部的一個循環(huán)怨咪,一輪一輪地處理消息隊列中的消息,相當(dāng)于執(zhí)行消息的回調(diào)函數(shù)润匙。
update:雖然之前寫過诗眨。但是發(fā)現(xiàn)自己并不是完全搞明白。所以又重寫了一篇孕讳。查了一天的資料在寫的匠楚。11.23/2017
Importent thing
- 我這兩天瀏覽文章的時候,才發(fā)現(xiàn)厂财。js是單線程的油啤,但是瀏覽器并不是的。以前就知道頁面是如何在網(wǎng)頁中展示的過程蟀苛。簡單提一下
- 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 ??
DNS work.jpg
- 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