Is javascript multi threaded?

M

Mark Smith

If I am performing 2 ajax requests with callback functions at the same
time, can I be guaranteed that the callbacks will not be executed at
the same time, or is there a chance that the statements could be
interleaved?

Thanks
 
M

Michael J. Ryan

If I am performing 2 ajax requests with callback functions at the same
time, can I be guaranteed that the callbacks will not be executed at
the same time, or is there a chance that the statements could be
interleaved?

Javascript run inside a single window/frame is generally serially executed in
a single thread. IIRC, there's been discussions of extending/changing this to
allow parallelization. outside the browser, depends.
 
T

Thomas 'PointedEars' Lahn

Mark said:
If I am performing 2 ajax requests with callback functions at the same
time, can I be guaranteed that the callbacks will not be executed at
the same time,
No.

or is there a chance that the statements could be interleaved?

Yes.

But to answer the question in your Subject that really should have been
in the message body (how many times do you need to be told?): No.


PointedEars
 
M

Matthias Reuter

Mark said:
If I am performing 2 ajax requests with callback functions at the same
time, can I be guaranteed that the callbacks will not be executed at
the same time, or is there a chance that the statements could be
interleaved?

You can be sure that the callbacks will not be executed at the same time.
You cannot, however, be sure which callback will be executed first. The
order of execution depends on the order the requests are finished, which
depends on many things beyond your control.

Matt
 
J

Jorge

If I am performing 2 ajax requests with callback functions at the same
time, can I be guaranteed that the callbacks will not be executed at
the same time, or is there a chance that the statements could be
interleaved?

A week ago I would have said no, they can't ever run "interleaved".
But I've learned recently that certain events are not queued but
handled synchronously (read:inmediatly, instantaneously, as-they-
happen) instead... one of such (synchronous) events occurs at the
exact moment you call xhr.open() -a synchronous onreadystatechange
event-, and I have not yet experimented with nor found info about
wether or not it can be expected for them to be handled synchronously
as well under any other circumstances...

For example, once you've sent both xhr requests, let's say a first
onreadystatechange event happens, and, if they're being handled
synchronously, ISTM that a second onreadystatechange event could very
well interrupt the previous one. At least that's what ISTM given the
observed (synchronous) behaviour of DOM mutation events. But I might
be perfectly wrong. And they might be handled asynchronously in all
cases except @ the xhr.open() call time. I don't know for sure.
 
N

nickfitz

If I am performing 2 ajax requests with callback functions at the same
time, can I be guaranteed that the callbacks will not be executed at
the same time, or is there a chance that the statements could be
interleaved?
As has been pointed out, the order in which the responses will be
received cannot be guaranteed (hence the term "asynchronous").
However, the callback function that processes the first-received-
response will run to completion before the second-received-response's
callback function is executed, due to the single-threaded nature of
JavaScript implementations.

However, another possible complication arises if either or both of
your callback functions are doing something that uses setInterval or
setTimeout, such as animating the display of updated content received
in the response. As the triggering of the timer functions is
independent of the main execution context from which they are
initiated, it would be possible to end up with some such sequence of
events as:

Make request A;
Make request B;
....
Response to request B received: setInterval i started;
....
setInterval i does something;
....
setInterval i does something;
....
Response to request A received; setInterval ii started;
....
setInterval ii does something;
....
setInterval i does something;
....
setInterval i does something;
....
setInterval ii does something;

and so on. So although JavaScript isn't threaded, in that each of the
individual "chunks" of execution delimited by "..." above will run to
completion before any other of the chunks is allowed to commence, it
is still possible for separate components of your overall application
to have their execution interleaved in an unpredictable manner.

Regards,

Nick.

(omitting .sig as apparently Google Groups mucks it up ;-)
 
J

Jorge

(...)
As has been pointed out, the order in which the responses will be
received cannot be guaranteed (hence the term "asynchronous").
However, the callback function that processes the first-received-
response will run to completion before the second-received-response's
callback function is executed, due to the single-threaded nature of
JavaScript implementations.

That was all good and well before synchronous events appeared in
scene... Run the code in this post and let me know what you think: how
is it that var ctr gets incremented several times from *within the
middle of a call* to alert() ?

http://groups.google.com/group/comp...6f7041b3c21/82572b69d602d315#28c8e3429211cf60
 
T

Thomas 'PointedEars' Lahn

nickfitz said:
As has been pointed out, the order in which the responses will be
received cannot be guaranteed (hence the term "asynchronous").
However, the callback function that processes the first-received-
response will run to completion before the second-received-response's
callback function is executed, due to the single-threaded nature of
JavaScript implementations.

You can _not_ be sure of that either. The callback is called by the UA's
DOM implementation and _not_ by the script engine. The only way to
guarantee that response #2 will not be handled before request-response chain
#1 is, of course, to make request #2 in the `onreadystatechange' listener of
request-response chain #1 after the response has been fully received (or
there has been an error).


PointedEars
 
J

Jorge

That was all good and well before synchronous events appeared in
scene... Run the code in this post and let me know what you think: how
is it that var ctr gets incremented several times from *within the
middle of a call* to alert() ?

http://groups.google.com/group/comp.lang.javascript/browse_thread/thr...

See these synchronous events: in FF there are 2: the first one at
xhr.open() and the second one at xhr.send() : in other browsers
there's just one @ .open() (as per the w3c recommendation) : the
remaining events until XHR completion seem to be handled
asynchronously (in the latest versions of the current browsers that
I've tested it in)

http://jorgechamorro.com/cljs/065/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top