当前位置:网站首页>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();
};
边栏推荐
猜你喜欢

The data obtained from mongodb query data command is null

Ctfshow SQL injection (211-230)

剑指 Offer 56 - I. 数组中数字出现的次数

2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets
![C#获取WebService接口的所有可调用方法[WebMethod]](/img/44/4429b78c5b8341ed9a4a08d75a683e.png)
C#获取WebService接口的所有可调用方法[WebMethod]

Converting MySQL data to PostgreSQL with Navicat

Redis master-slave replication, sentinel mode, cluster

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

Sword finger offer 56 - I. number of occurrences in the array

The differences between the four startup modes of activity and the applicable scenarios and the setting methods of the two startup modes
随机推荐
H5 the blue background color appears when clicking the picture
Solve the problem of running server nodemon reporting errors
It's the Caesar code. (*‘▽‘*)*
[untitled]
February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
Internet people a few years ago vs Internet people today
Vercel uses HTTP caching
ACM ICPC
Suffix Automaton
一致性哈希的简单认识
General communication protocol for industrial Internet
Tita绩效宝:远程一对一面谈的问题
Introduction to applet Basics (dark horse learning notes)
Basic syntax example for go
【Flutter 問題系列第 67 篇】在 Flutter 中使用 Get 插件在 Dialog 彈窗中不能二次跳轉路由問題的解决方案
C#获取WebService接口的所有可调用方法[WebMethod]
Click change color to change subscript
第三方评论插件
E - Lucky Numbers
力扣刷题338.比特位计数