Friday, November 24, 2006

Java Script multi-threading

We all know that it is not possible,...but... using timers along with path-through routines that don't block might sound like a cool alternative. I have tried it and it works - picture this:

function doLongTask(x,y,z, callback)
this.X = x;
this.Y = y;
this.Z = z;

doLongTask.prototype.threadSpin = _longtask_threadspin;

function _longtask_threadspin()
//instead of long loops save your intermediate state in this.{vars}

if (this.X%100==0) this.Z = this.Y - 100; //some logic that would have been in a tight loop

if (this.X>1000) this.doneCallback();


And now we can easily:

var task = new doLongTask(1,2,3,doneLongTask);
var tmr = setInterval(task.threadSpin, 10/*time slice!!!*/);

function doneLongTask()
task.Z // result of task
clearInterval(tmr);// kill 'thread'


Anonymous said...

COOL, this should be ok - no closures problems either

itAdapter said...

Yes, speaking of closures I am trying to avoid this pattern:
this.X = function(){.....}
as much as I can because
eventually this will crash IE

Anonymous said...

helped, thanks

Anonymous said...

such a waste

Anonymous said...

I heard Chrome v8 compiles js to bin maybe those guys give access to real thing threads _xeh_

Anonymous said...

I'm newbie here, I hope to get friends at this forum

mmjiaxin said...

ferragamo shoes
michael kors outlet
rolex watches outlet
tory burch outlet online
michael kors handbags
coach outlet store
mac cosmetics
michael kors outlet online
wellensteyn outlet
michael kors outlet
canada goose outelt
air max 2014
ray ban,rayban,occhiali ray ban,ray-ban,ray ban occhiali,ray ban sunglasses
woolrich outlet store
fred perry polo shirts
michael kors outlet
tiffany outlet
canada goose jackets
oakley outlet store
mizuno running shoes

Unknown said...

ravens jerseys
michael kors handbags
hermes belt
fitflops shoes
abercrombie and fitch
michael kors outlet store
michael kors handbags
nike air huarache
bears jerseys
michael kors handbags

KIMI HIME said...

Thank you for this very useful health article

Cara Mengobati Radang Amandel
Cara Mengatasi Perut Kembung
Cara Mengobati Radang Sendi
Obat Penghilang Benjolan Bernanah

Unknown said...

2018531 leilei3915
canada goose
ray ban sunglasses
pandora outlet
coach outlet store online
pandora charms
kate spade outlet online
michael kors handbags
christian louboutin shoes
louis vuitton outlet online
true religion

Susan said...

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. Wondering where to go in 2020? Things to do has ranked as the best include a remote, idyllic island and the design capital.