HTML5 Web Workers Simple pool of workers

Within this example, the browser would develop a pool of workers and delivering the command to every workerand begin the task. not so difficult, you may still find many complication coming, stay tune.

var workers = new Array(5);
// creating a pool of workers
for(var i=0; i<5; ++i) {
  workers[i] = new Worker('worker001.js');
  // getting the message from the worker
  workers[i].onmessage = function(event) {
  }
}
for(var i=0; i<5; ++i) {
  // sending the message to the worker
  workers[i].postMessage(i);
}

and here is code of the worker001.js

var ID = null;

onmessage = function(event) {
  ID = event.data;
  postMessage('worker: ID ('+ID+') assigned and start to run');
}

nevertheless, when you look into the implementation of the chrome, you will find it is a separate process!

Incoming search terms:

  • tutorial webworker html5
  • web workers html5 example
  • web workers tutorial
  • web worker html5 examples
  • html5 web workers tutorial
  • webworker role html5 samples
  • web worker sample
  • webworkers pool
  • basic tutorial in web worker
  • pooling web workers
Share
Get Adobe Flash player
12 visitors online now
3 guests, 9 bots, 0 members
Max visitors today: 18 at 04:12 am UTC
This month: 141 at 05-02-2012 11:23 am UTC
This year: 141 at 05-02-2012 11:23 am UTC
All time: 141 at 05-02-2012 11:23 am UTC