Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>1. How would I run several web workers? I tried a for-loop looking like that:</p> </blockquote> <p>There's no problem with creating more than one worker, even if you don't keep track of them in an array. See below.</p> <blockquote> <p>2. How would I control that all have finished their work? (I need to reassembly the array and work with it later)</p> </blockquote> <p>They can post a message back to you when they're done, with the results. Example below.</p> <blockquote> <p>3. How many web workers really bring an improvement?</p> </blockquote> <p>How long is a piece of string? :-) The answer will depend entirely on the target machine on which this is running. A lot of people these days have four or more cores on their machines. Of course, the machine is doing a lot of other things as well. You'll have to tune for your target environment.</p> <blockquote> <p>4. Is there any advanced tutorial, besides the MDN-entry?</p> </blockquote> <p>There isn't a lot "advanced" about web workers. :-) I found <a href="http://www.html5rocks.com/en/tutorials/workers/basics/" rel="noreferrer">this article</a> was sufficient.</p> <p>Here's an example running five workers and watching for them to be done:</p> <p>Main window:</p> <pre><code>(function() { var n, worker, running; display("Starting workers..."); running = 0; for (n = 0; n &lt; 5; ++n) { workers = new Worker("worker.js"); workers.onmessage = workerDone; workers.postMessage({id: n, count: 10000}); ++running; } function workerDone(e) { --running; display("Worker " + e.data.id + " is done, result: " + e.data.sum); if (running === 0) { // &lt;== There is no race condition here, see below display("All workers complete"); } } function display(msg) { var p = document.createElement('p'); p.innerHTML = String(msg); document.body.appendChild(p); } })(); </code></pre> <p><code>worker.js</code>:</p> <pre><code>this.onmessage = function(e) { var sum, n; sum = 0; for (n = 0; n &lt; e.data.count; ++n) { sum += n; } this.postMessage({id: e.data.id, sum: sum}); }; </code></pre> <p>About the race condition that doesn't exist: If you think in terms of true pre-emptive threading, then you might think: I could create a worker, increment <code>running</code> to <code>1</code>, and then before I create the next worker I could get the message from the first one that it's done, decrement <code>running</code> to <code>0</code>, and mistakenly think all the workers were done.</p> <p>That can't happen in the environment web workers work in. Although the environment is welcome to start the worker as soon as it likes, and a worker could well finish before the code starting the workers finished, all that would do is <em>queue</em> a call to the <code>workerDone</code> function for the main JavaScript thread. There is no pre-empting. And so we know that all workers have been started before the first call to <code>workerDone</code> is actually executed. Thus, when <code>running</code> is <code>0</code>, we know they're all finished.</p> <p>Final note: In the above, I'm using <code>onmessage = ...</code> to hook up event handlers. Naturally that means I can only have one event handler on the object I'm doing that with. If you need to have multiple handlers for the <code>message</code> event, use <code>addEventListener</code>. All browsers that support web workers support <code>addEventListener</code> on them (youdon't have to worry about the IE <code>attachEvent</code> thing).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload