当前位置:网站首页>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();
};
边栏推荐
- Use service worker to preferentially request resources - continuous update
- NodeJS 解析 GET 请求 url 字符串
- Tita: Xinrui group uses one-to-one talk to promote the success of performance change
- Test question bank and online simulation test for special operation certificate of construction scaffolder (special type of construction work) in 2022
- PHP security development 15 user password modification module
- Ultra quicksort reverse sequence pair
- Get verification code
- Crawler scrapy framework learning 1
- Applet - uniapp realizes the functions of two-dimensional code picture pop-up and picture saving
- Online audio adjustment technology summary
猜你喜欢

Colab tutorial (super detailed version) and colab pro/pro+ evaluation

Ladder race

一致性哈希的简单认识

Introduction to applet Basics (dark horse learning notes)

【Try to Hack】upload-labs通关(暂时写到12关)

Redis主从复制、哨兵模式、集群

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

第007天:go语言字符串

2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY

前几年的互联网人vs现在的互联网人
随机推荐
Applet waterfall flow
Collection of wrong questions in soft test -- morning questions in the first half of 2010
MySQL索引
C盘无损移动文件
前几年的互联网人vs现在的互联网人
第007天:go语言字符串
Implementation of homepage header function in PHP development blog system
如何只用4步,实现一个自定义JDBC驱动?
Record a troubleshooting process - video call cannot be picked up
A simple understanding of consistent hash
How to handle async/await error messages gracefully
Conception d'un système basé sur MVC avec javaswing JDBC
Clear timer failure
Ultra quicksort reverse sequence pair
CTFSHOW SQL注入篇(211-230)
工业互联网通用通信协议
Analyse du principe de mise en œuvre d'un éditeur de texte open source markdown - to - rich
Powershell 加域 Add-Computer模块
C disk lossless move file
Applet - uniapp realizes the functions of two-dimensional code picture pop-up and picture saving