当前位置:网站首页>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();
};
边栏推荐
- D 小红的构造题
- [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
- Ionic Cordova command line
- Basic syntax example for go
- Common terms of electromagnetic compatibility
- Online audio adjustment technology summary
- Get verification code
- [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
- Test question bank and online simulation test for special operation certificate of construction scaffolder (special type of construction work) in 2022
- Vercel uses HTTP caching
猜你喜欢
【JS解决】leedcode 117. 填充每个节点的下一个右侧节点指针 II
Tree array explanation
[LeetCode]-二分查找
Collection of wrong questions in soft test -- morning questions in the first half of 2011
EMC rectification outline
【Flutter 问题系列第 67 篇】在 Flutter 中使用 Get 插件在 Dialog 弹窗中不能二次跳转路由问题的解决方案
Catalan number
C盘无损移动文件
Redis
Small program input element moving up
随机推荐
剑指 Offer 56 - I. 数组中数字出现的次数
【剑指Offer】面试题25.合并两个有序的链表
Go/golang connection to database
利用Javeswingjdbc基于mvc设计系统
2022年建筑架子工(建筑特殊工种)特种作业证考试题库及在线模拟考试
php 18 首页的文章列表功能实现
【Try to Hack】upload-labs通关(暂时写到12关)
在线音频调节技术汇总
【JS解决】leedcode 117. 填充每个节点的下一个右侧节点指针 II
是“凯撒密码”呀。(*‘▽‘*)*
C disk lossless move file
Collection of wrong questions in soft test -- morning questions in the first half of 2011
Redis主从复制、哨兵模式、集群
Use service worker to preferentially request resources - continuous update
力扣刷题338.比特位计数
CreateAnonymousThreadX给匿名线程传递参数
php开发博客系统的首页头部功能实现
Message scrolling JS implementation
Read paper 20 together: spatiotemporal prediction of PM2.5 concentration by idw-blstm under different time granularity
Implementation of homepage header function in PHP development blog system