0

When I make a website using javascript, do I have the opportunity to take advantage of multiple threads on the client's computer?

I know web programming can give you access to multiple asynchronous http or networking requests. I'm wondering about actual in-browser processing.

3

4 Answers 4

3

Web Workers is the way to go... It is a HTML 5 feature which allows running multiple threads (workers) on the client. This feature is currently a working draft.

You can start any number of workers for a page, and each worker can 'post' their state or the result to the main thread.

Have a peek at this MDN post https://developer.mozilla.org/En/Using_web_workers

Also, the link posted by SRN is also very useful ( http://www.html5rocks.com/en/tutorials/workers/basics/)

Also mind that the browser support is still not good. http://caniuse.com/webworkers

It's best you have a fall-back method in case you hit a browser that's un-supported. Also note that Chrome used to have a bug where the web worker can can actually hang the Chrome UI. May be it's now fixed, but look out.

Sign up to request clarification or add additional context in comments.

Comments

3

There are things called "WebWorkers" that provide some degree of concurrency. They interoperate with "normal" code via a message passing paradigm kind-of like Erlang processes (though not nearly as sophisticated).

It's a new-ish HTML5 thing, and not supported in old browsers of course.

1 Comment

Would it be in Chrome and the latest FireFox?
2

Web Workers is the technology.

A web worker -- as defined by the World-Wide Web Consortium (W3C) and the Web Hypertext Application Technology Working Group (WHATWG) -- is a JavaScript script -- executed from an HTML page -- that runs in the background, independently of other, user-interface scripts that may also have been executed from the same HTML page

See on usage on MDN too:

Dedicated Web Workers provide a simple means for web content to run scripts in background threads. Once created, a worker can send messages to the spawning task by posting messages to an event handler specified by the creator.

Comments

1

there is multithreaded web programming but there is no multithreaded javascript.

when javascript executes in a browser on the client it is interpreted line by line and won't render anything while it is executing.

you can tap into open source libraries to "imitate" multithreading but basically each javascript needs a page to live and run on.

some tricks are to pass long running functions to run inside of an iframe or to use a setTimeout function to do some work for 50 milliseconds at a time until some flag you use to keep track of the job says the work is done like isFinished == true

the latest versions of flash player allows multithreading in it but it is limited to very basic usage across a single domain.

html 5 web workers are another option but won't work in all browsers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.