The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

victorp (3) [Avatar] Offline
#1
I have a few questions regarding multithreading in client code:

1. Is it possible to use (create, run etc.) more then one thread ? If yes, how?
2. We have asynchronous callbacks and timers, may we assume that all the code is running in one thread or should we take care about data synchronization?
3. Can the client behavior be changed depending on running modes or web browsers regarding the multithreading issue?

Thanks
adam.tacy (269) [Avatar] Offline
#2
Re: Multithreading in client ???
Hi

Hope the answers below help:

>
> 1. Is it possible to use (create, run etc.) more then one thread ? If yes, how?

No, JavaScript is single threaded, and that's what your code will eventually become.

> 2. We have asynchronous callbacks and timers, may we
> assume that all the code is running in one thread or
> should we take care about data synchronization?

Code will run in one thread, but since everything is asynchronous, you always need to take care about data synchronisation if one action depends on the result of another - one thread doesn't mean it will execute one asynchronous call and callback before the next one starts.

> 3. Can the client behavior be changed depending on
> running modes or web browsers regarding the
> multithreading issue?
>

You could potentially be caught out by hosted mode looking more multi-threaded than web-mode since hosted mode runs as Java - but in the end it will be JavaScript.

//Adam
victorp (3) [Avatar] Offline
#3
Re: Multithreading in client ???
Hi Adam,

Thanks for a very detailed and helpful answers.

About q.3:

>> 3. Can the client behavior be changed depending on
> >running modes or web browsers regarding the
> >multithreading issue?
>>

answer:
>You could potentially be caught out by hosted mode looking more multi-threaded than >web-mode since hosted mode runs as Java - but in the end it will be JavaScript.

So, as I understood this leads to some cases when multithreading in hosted mode may cause some problems with data synchronization while the same code will run perfectly in production. Am I right?