当前位置:网站首页>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 differences between the four startup modes of activity and the applicable scenarios and the setting methods of the two startup modes

PowerShell plus domain add computer module
![[flutter problem Series Chapter 67] the Solution to the problem of Routing cannot be jumped again in in dialog popup Using get plug - in in flutter](/img/59/0d95619ee3bba1f8992d90267d45c2.png)
[flutter problem Series Chapter 67] the Solution to the problem of Routing cannot be jumped again in in dialog popup Using get plug - in in flutter

Colab使用教程(超级详细版)及Colab Pro/Pro+评测

2019 Blue Bridge Cup
![[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](/img/59/0d95619ee3bba1f8992d90267d45c2.png)
[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

CreateAnonymousThreadX给匿名线程传递参数

2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets

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

Analyse du principe de mise en œuvre d'un éditeur de texte open source markdown - to - rich
随机推荐
2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets
February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY
工业互联网通用通信协议
Collection of wrong questions in soft test -- morning questions in the first half of 2010
PHP security development 15 user password modification module
Nodejs parsing get request URL string
Ctfshow SQL injection (211-230)
Crawler scrapy framework learning 1
This Sedata uses multiple methods to dynamically modify objects and values in arrays. Object calculation properties
Implementation of homepage header function in PHP development blog system
Normal distribution (Gaussian distribution)
Test question bank and online simulation test for special operation certificate of construction scaffolder (special type of construction work) in 2022
一款开源的Markdown转富文本编辑器的实现原理剖析
php安全开发15用户密码修改模块
【剑指Offer】面试题24.反转链表
推荐的图片临时在线压缩工具
ES6 learning
2022 chlorination process operation certificate examination question bank and simulation examination
PHP development 14 compilation of friendship link module