PDA

View Full Version : In a browser, how many threads are supported



Elijah
26 Feb 2010, 6:01 AM
Guys, I had been told that the browser only supports X number of threads. Does anyone know if this is accurate? I am curious because if there is a limit then it would make sense to create a datathread array that would manage a queue of threads that are allowed to run at a time in order to prevent any issues where that number of threads is not exceeded.

Thanks.

realjax
26 Feb 2010, 6:38 AM
Talking about Javascript 'threads' ?
In that case, forget about it. It's single threaded.

Elijah
26 Feb 2010, 7:32 AM
Okay, I dont mean in the sense like with java of multithreaded processes to distribute the load. I mean if you load a page and it has 100 AJAX calls trying to be made whats the limit the browser can handle in the sense of requests that are being made.

realjax
26 Feb 2010, 1:23 PM
Ah I see.. has nothing to do with threads then. The max number of simultaneous request *to a single hostname* depends on two things.
- the browser
- possible altered settings by the user for a browser.

For some browsers you could go as high as a 100 I guess (effectively bringing your web- and application server to its knees quite quickly with multiple users) but, you *can't* affect these settings from your script, which is logical.

edit: You don't have to worry though. The queue manager you'd want to build is already part of a browser.

Elijah
1 Mar 2010, 1:02 PM
You don't have to worry though. The queue manager you'd want to build is already part of a browser.

WOW I was not aware of this.

But in dreaming of the issue last night another thought occured to me. If trying to make modules that can operate on their own or if dropped into the application that we are building how best to initiate that modules code if the main application is utilizing Ext.onReady(function...

To me we would not want each module to be running Ext.onReady(function... as it would conflict so how best to approach this?

rbastic
1 Mar 2010, 1:12 PM
make an init() method and call it for each module?

Elijah
1 Mar 2010, 4:09 PM
Which in turn goes back to the main file knowing what init() methods to call.
i.e.
MainPage.html (Ext.onReady(function() {
MP.init();
MP1.init();
MP2.init();
MP3.init();
}); //end onReady)
MainPage.js (MP.init(){...})
mod01.js (MP1.init(){...})
mod02.js (MP2.init(){...})
mod03.js (MP3.init(){...})

Am I wrong on how this is to be implemented? If not the question then is should I be using a mechanism to register the various init() so the onReady will know what inits are to be executed?

babsjr77
2 Mar 2010, 7:17 AM
Yes. In our case I have written a simple module manager with a register function. Each module is contained in a separate js file (all the js files are minimized for production) and calls the manager's register function.


ModuleManager.register(new Module({
init : function() { }
});


Then the main js file containing Ext.onReady loops through all registered modules and calls their init functions. Adding a module is as simple as adding another javascript file to the html.

rbastic
2 Mar 2010, 7:23 AM
How would you detect that you added another script tag to your code? :p

Honestly, that seems a little too magicky. I'd prefer simplicity over any obscure hacks.

Animal
2 Mar 2010, 7:28 AM
Each module just has its own Ext.onReady call to kick itself off.

rbastic
2 Mar 2010, 7:30 AM
....I thought you couldn't have more than one Ext.onReady() function?

Animal
2 Mar 2010, 7:32 AM
Why? It just executes the passed function when the DOM is ready.

And if the DOM is already ready, it calls the passed function immediately.

mschwartz
2 Mar 2010, 7:33 AM
You can boost performance by having multiple hostnames pointing at your WWW server.

For example, img1.example.com, img2.example.com, etc. If you are clever about using these alternate names, the browser will use additional socket connections to your server and thus your XHR won't have to queue up behind image and other static item fetches. It should work for loading .js files, too.

mschwartz
2 Mar 2010, 7:36 AM
Of course you can have multiple Ext.onReady() in your code.

Correct me if I'm wrong, though, there's no way to truly control the order they're executed - like if you want a certain one to run first. You might think the order of the script tags would control it, but if you're dynamically loading scripts they may load in an arbitrary order, or if you are loading the scripts in an iframe in IE by document.write() they are also loaded asynchronously/arbitrary order.

Animal
2 Mar 2010, 7:39 AM
They will be called in the order that they are added.

Header scripts are evaluated serially, so each Ext.onReady adds a listener to the listeners Array.

Then the docReadyEvent.fire call just goes through the listeners Array, calling the handlers.

babsjr77
2 Mar 2010, 7:49 AM
How is that hacky? It's a simple registration system to track all the modules. In my case each module has more than an init function. It also has various methods to deal with lifecycle type issues such as creating a component to display the module. I control the html file so it's not like random modules are added.

rbastic
2 Mar 2010, 8:07 AM
It's not. I retract my previous statement. Didn't know you could have numerous Ext.onReady()'s spread out. :)

Juanito
2 Mar 2010, 10:40 AM
It is fine to have multiple calls to Ext.onReady, but relying on them being called in order is bad practice. Though Animal knows (me too) that's what the code does, there is no guarantee from the contract and it won't change in the future. If ext puts that into their documentation, then I would retract my statement. I specially take this position because you're not supposed to rely on the order of regular DOM events being called.

My suggestion is that things that need to be initialized in order should be called from one function; therefore, in your case, create one function that gets passed to Ext.onReady. For everything where order is not a concern, it is better to put it into a separate Ext.onReady to avoid dependencies.

On the other hand, script tags are evaluated in the order they appear in the source code. Everything else halts while a script tag's content is being evaluated.

Having said all that, if an object requires a DOM object to be initialized (hence waiting for onReady), I don't think the file itself should do the initialization.

mschwartz
3 Mar 2010, 6:11 AM
They will be called in the order that they are added.

Header scripts are evaluated serially, so each Ext.onReady adds a listener to the listeners Array.

Then the docReadyEvent.fire call just goes through the listeners Array, calling the handlers.

http://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/

In IE8 in quirks mode and IE7 compatibility mode, if you use document.write() to put HTML in an iframe, the scripts get loaded in random order. In fact, I had to figure out a nasty bug where jquery plugins were failing in this scenario because jquery itself hadn't been loaded and instantiated yet, even though the script tag for jquery was in the head and before the plugins.