Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Javascript webworker problem

Jeremi Roivas

New Coder
Hi, I try creating 7 webworkers to simultanously fetch week data from remote server over internet. How ever I can get working 1 or 2 workers fine but if I try combine the result set to main() function with variable that listeners listen only 2 counters increse counter and main() will never get fired:

JavaScript:
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>i4ware - Timesheet for Jira</title>
    <link type="text/css" rel="stylesheet" href="./extjs/resources/ext-theme-neptune/ext-theme-neptune-all.css" media="all">
    <link type="text/css" rel="stylesheet" href="./custom.css" media="all">
    <script type="text/javascript" src="./extjs/ext-all.js"></script>
    <script type="text/javascript" src="./js/WebWorker.js"></script>
    <script type="text/javascript" src="./js/custom.js"></script>
    <script type="text/javascript">
   
    var day1 = new Worker('worker.js');
    var day2 = new Worker('worker.js');
    var day3 = new Worker('worker.js');
    var day4 = new Worker('worker.js');
    var day5 = new Worker('worker.js');
    var day6 = new Worker('worker.js');
    var day7 = new Worker('worker.js');
    var c = 0;
    var a = '';
    var b = '';
    var c = '';
    var d = '';
    var e = '';
    var f = '';
    var g = '';
   
    function tulosta() {
       
        day1.postMessage('24/4/2022|Matti Kiviharju');
        day2.postMessage('25/4/2022|Matti Kiviharju');
        day3.postMessage('26/4/2022|Matti Kiviharju');
        day4.postMessage('27/4/2022|Matti Kiviharju');
        day5.postMessage('28/4/2022|Matti Kiviharju');
        day6.postMessage('29/4/2022|Matti Kiviharju');
        day7.postMessage('30/4/2022|Matti Kiviharju');
        //console.log('Ollaan Main funtiossa');
        //console.dir(tuloste);
        //document.getElementById('tulostus').innerHTML = 'Vittu';
    };  
   
    /*myWorker.onerror = function (e) {
        console.log(e);
        alert(e.message);
    }*/
    day1.onmessage = function (y) {
        a = y.data;
        console.log('Tulostetaan day1: ' + y.data);
        c++;
        console.log(c);
        if (c == 7) {
            main();
        };
    };
    day2.onmessage = function (y) {
        b = y.data;
        console.log('Tulostetaan day2: ' + y.data);
        c++;
        console.log(c);
        if (c == 7) {
            main();
        };
    };
    day3.onmessage = function (y) {
        c = y.data;
        console.log('Tulostetaan day3: ' + y.data);
        c++;
        console.log(c);
        if (c == 7) {
            main();
        };
    };
    day4.onmessage = function (y) {
        d = y.data;
        console.log('Tulostetaan day4: ' + y.data);
        c++;
        console.log(c);
        if (c == 7) {
            main();
        };
    };
    day5.onmessage = function (y) {
        e = y.data;
        console.log('Tulostetaan day5: ' + y.data);
        c++;
        console.log(c);
        if (c == 7) {
            main();
        };
    };
    day6.onmessage = function (y) {
        f = y.data;
        console.log('Tulostetaan day6: ' + y.data);
        c++;
        console.log(c);
        if (c == 7) {
            main();
        };
    };
    day7.onmessage = function (y) {
        g = y.data;
        console.log('Tulostetaan day7: ' + y.data);
        c++;
        console.log(c);
        if (c == 7) {
            main();
        };
       
    };
    function main () {
   
       
            if (c == 7) {
                console.log('Tulostetaan ruudulle');
                document.getElementById('tulostus').innerHTML = a + b + c + d + f + g;
            };
       
    };
    </script>
</head>
<body>
    <button id="alota" value="Start" onclick="tulosta()">
    <div style="border: 1px black solid;width:800px; height: 600px;background-color:white;"><p id="tulostus"></p><div>
<body>
<html>

Console gets C counter added to 2 or 3 but then C says NAN on console and c never gets to 7 to fire main(). I suspect that localserver id too fast and simultanous data saving makes corruption happen.
 
Last edited:
I'm not familiar specifically with JavaScript worker threads, but have some experience with multithreaded programming. If multiple threads need to work on the same global data you'll need some form of thread synchronization (aka thread locking). Otherwise you are in for unpredictable results. Is there a good reason to want to use 7 worker threads for this task ?
 
Hi, Atlasian server is located in australia and our app in finland. Authetication and large dataset take over 5 seconds to print 7 tables. Thats too long for customers to wait start editing table fields. this why we want download smaller dataset multithreaded to reduce downloand time to print 7 tables. Everything works fast if we use local mysql tables but fetching data from atlasian servers takes too long and makes huge wait time to print 7 day workhour tables.
 
Now it works.
JavaScript:
day7.onmessage = function (y) {
        g = y.data;
        console.log('Tulostetaan day7: ' + g);
        gg = true;
        console.log(gg);
        adder();
        
    };
    function adder () {
        console.log ('adder jauhaa...');
        var counter = 0;
        var teksti = [aa , bb , cc, dd, ee, ff, gg];
        for (let i = 0; i < teksti.length; i++) {
            if (teksti[i] == true) counter = counter + 1;
        };
        console.log('Forin jälkeen: ' + counter);
        if (counter == 7) main();
    };

This adder functions counts how many true events have arrived and starts main if all 7 workers have done and changed their variable to true.
 

New Threads

Buy us a coffee!

Back
Top Bottom