当前位置:网站首页>promise处理js多线程全部获得结果后同一处理结果
promise处理js多线程全部获得结果后同一处理结果
2022-06-13 04:36:00 【AF01】
html 代码
<!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('未开启多线程单个耗时:',+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('开启多线程耗时:',+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();
};
边栏推荐
猜你喜欢

CTFSHOW SQL注入篇(211-230)

Collection of wrong questions in soft test -- morning questions in the first half of 2010

VGA display based on de2-115 platform

用navicat将mysql数据转化为postgresql

How to implement a custom jdbc driver in only four steps?

Read paper 20 together: spatiotemporal prediction of PM2.5 concentration by idw-blstm under different time granularity

Redis

Analysis of the implementation principle of an open source markdown to rich text editor

Simple static web page + animation (small case)

2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets
随机推荐
Tita:新锐集团采用一对一面谈推动绩效变革成功
SS selector
How to implement a custom jdbc driver in only four steps?
String full summary
SQL advanced challenge (1 - 5)
CTFSHOW SQL注入篇(211-230)
Redis
Ionic Cordova command line
February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
Message scrolling JS implementation
120. 三角形最小路径和-动态规划
2022氯化工艺操作证考试题库及模拟考试
VGA display based on de2-115 platform
Gets or sets the content in an object
MVP framework for personal summary
Uni app Ali font icon does not display
Small program input element moving up
Summary of webdriver API for web automated testing
[sword finger offer] interview question 25 Merge two ordered linked lists
php开发14 友情链接模块的编写