当前位置:网站首页>Promise processing JS multithreads get the same processing result after all the results are obtained
Promise processing JS multithreads get the same processing result after all the results are obtained
2022-06-13 04:40:00 【AF01】
html Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function feb(n) {
if (n == 1 || n == 2) {
return 1
}
return feb(n - 1) + feb(n - 2)
}
let a = +new Date();
let n1 = 30;
let data2 = feb(n1)
console.log(data2);
console.log(' Multithreading not turned on single time consuming :',+new Date() - a);
let time1 = +new Date();
let promiseArr = []
for (let index = 0; index < 13; index++) {
let worker = new Worker('worker.js')
let p = new Promise((resolve, reject) => {
worker.postMessage(n1)
worker.onmessage = e => {
resolve(e.data)
}
worker.onerror = error => {
reject(error)
}
})
promiseArr.push(p);
n1++;
}
console.log(promiseArr);
Promise.all(promiseArr).then(data => {
console.log(data);
console.log(' Time consuming to turn on Multithreading :',+new Date() - time1);
})
</script>
</body>
</html>
worker.js
function feb(n) {
if (n==1||n==2) {
return 1
}
return feb(n-1)+feb(n-2)
}
let a = +new Date();
let n= 2;
self.onmessage = function(event) {
n = event.data;
console.log("onmessage:",event.data);
let data1 = feb(n)
console.log(+new Date()-a);
self.postMessage(data1)
self.close();
};
边栏推荐
- [LeetCode]-二分查找
- 剑指 Offer 56 - I. 数组中数字出现的次数
- 2022年氧化工艺操作证考试题库及模拟考试
- 力扣刷题647.回文子串
- NodeJS 解析 GET 请求 url 字符串
- PowerShell: because running scripts is prohibited on this system, the solution
- This Sedata uses multiple methods to dynamically modify objects and values in arrays. Object calculation properties
- Normal distribution (Gaussian distribution)
- 2022道路运输企业安全生产管理人员操作证考试题库及答案
- Tita performance treasure: remote one-on-one discussion
猜你喜欢
利用Javeswingjdbc基於mvc設計系統
Read paper 20 together: spatiotemporal prediction of PM2.5 concentration by idw-blstm under different time granularity
Colab tutorial (super detailed version) and colab pro/pro+ evaluation
How to implement a custom jdbc driver in only four steps?
[chapter 67 of the flutter problem series] the solution to the problem that the get plug-in cannot jump to the route twice in the dialog pop-up window in flutter
Express scaffold creation
Ctfshow SQL injection (211-230)
Express framework knowledge - Art template template, cookie, session
C#获取WebService接口的所有可调用方法[WebMethod]
Li Kou brush question 338 Bit count
随机推荐
【JS解决】leedcode 200. 岛屿数量
C盘无损移动文件
2022道路运输企业安全生产管理人员操作证考试题库及答案
Tita: Xinrui group uses one-to-one talk to promote the success of performance change
Ladder race
在线音频调节技术汇总
Get verification code
Ctfshow common postures (821-830)
php开发博客系统的首页头部功能实现
php开发16退出模块
【剑指Offer】面试题24.反转链表
Li Kou brush question 647 Palindrome substring
Tita绩效宝:远程一对一面谈的问题
CTFSHOW SQL注入篇(211-230)
ACM ICPC
General communication protocol for industrial Internet
C disk lossless move file
Third party comment plugin
Uni app Ali font icon does not display
php安全开发15用户密码修改模块