gpt4 book ai didi

javascript - JavaScript 中的并行编程

转载 作者:行者123 更新时间:2023-11-29 17:36:10 25 4
gpt4 key购买 nike

我正在使用 javascript 进行顺序和并行编程,我能够解决顺序编程问题,但不知道如何使用并行编程来解决同样的问题。

对于顺序问题是:

This is an example of sequential processing where it will start at 1 and go till 3 and add 21 to it. Then it will start at 1 and go till 2 and add 10 to it. In the end, start at 1 and go till 4 and add 1 to it.

对于输入:1 3*21#2*10#4*1输出将是:

22
23
24
11
12
2
3
4
5

我用下面的代码解决了

function solution(inputData) { 

var first = inputData.substring(0, 1);
if(first == 1)
{
//sequential
var strArr = inputData.split(" "); //3*21#2*10#4*1
var strHashSplitArr = strArr[1].split("#"); //3*21 #2*10# 4*1
for(var i=0;i<strHashSplitArr.length;i++)
{
var loopInp = strHashSplitArr[i].split("*");
var maxVal = parseInt(loopInp[0]);
var addVal = parseInt(loopInp[1]);

for(var k=1;k<=maxVal;k++)
{
console.log(k+addVal);
}
}
}
}

但现在的问题是并行编程

问题:

For example 2, there are 3 processes to start in parallel and numbers are 1, 2 and 3 with delays 100, 20 and 50 consecutively. Here all the processes will start together but a number with less delay will be printed first. Here the number with less delay is 2.So it will print 21,22 in the meantime 50 ms will be achieved and it will print 51 from 3rd number. Now it is mixed with number 1 and prints 101 and so on.

输入:2 1*100#2*20#3*50

输出应该是:

21
22
51
101
52
53

我没有尝试使用并行,而是使用毫秒排序,但无法获得预期的输出。

这是 2nd 的 JSfiddle 代码 .. 它给出了错误的输出(我没有使用并行方法):https://jsfiddle.net/mahajan344/0u2ka981/

如何使用并行 JavaScript 编程实现相同的输出?

最佳答案

我认为使用 setTimeout 解决问题的方法可能是:

function printParallel(val, delay)
{
setTimeout(function()
{
console.log(val);
}, delay);
}

function solution(inputData) {
var first = inputData.substring(0, 1);
var strArr = inputData.split(" ");
var strHashSplitArr = strArr[1].split("#");

for (var i = 0; i < strHashSplitArr.length; i++)
{
var loopInp = strHashSplitArr[i].split("*");
var maxVal = parseInt(loopInp[0]);
var modifier = parseInt(loopInp[1]);

if (first == 1)
{
for (var j = 1; j <= maxVal; j++)
{
console.log(j+modifier);
}
}
else if (first == 2)
{
for (var j = 1; j <= maxVal; j++)
{
printParallel(j+modifier, modifier);
}
}
}
}

因此您将调用 solution("1 3*21#2*10#4*1");solution("2 1*100#2*20#3* 50"); 来执行示例,但这不会按预期输出,因为数字 1 的 100 的延迟太大而无法与数字 3 的打印混合。

编辑:

我想现在我明白了目标:你需要在每个 console.log 之间设置一个超时。这将按预期工作:

function printParallel(value, maxVal, modifier)
{
setTimeout(function()
{
console.log(value+modifier);
if (value < maxVal)
{
printParallel(++value, maxVal, modifier)
}
}, modifier);
}

function solution(inputData) {
var first = inputData.substring(0, 1);
var strArr = inputData.split(" ");
var strHashSplitArr = strArr[1].split("#");

for (var i = 0; i < strHashSplitArr.length; i++)
{
var loopInp = strHashSplitArr[i].split("*");
var maxVal = parseInt(loopInp[0]);
var modifier = parseInt(loopInp[1]);

if (first == 1)
{
for (var j = 1; j <= maxVal; j++)
{
console.log(j+modifier);
}
}
else if (first == 2)
{
printParallel(1, maxVal, modifier);
}
}
}

关于javascript - JavaScript 中的并行编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56422042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com