Try open the following page in two different tabs in your browser. hit the refresh button to avoid getting the page from browser cache :
protected void Page_Load(object sender, EventArgs e)
{
Thread.Sleep(10000);
Response.Write(DateTime.Now.ToString());
}
As you can see, it seems that there is a thread created for every request, and they did not wait for each other there is no lock acquired.
Now Create the following pages, and GlobalCustomClass
GlobalCustomClass
public class GlobalCustomClass
{
public static string GlobalVariable
{
get;
set;
}
}
Default.aspx
protected void Page_Load(object sender, EventArgs e)
{
GlobalCustomClass.GlobalVariable = "Default page";
Thread.Sleep(10000);
Response.Write(DateTime.Now.ToString());
Response.Write("<br />");
Response.Write(GlobalCustomClass.GlobalVariable);
}
Page2.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString());
Response.Write("<br />");
GlobalCustomClass.GlobalVariable = "Page2";
Response.Write(GlobalCustomClass.GlobalVariable);
}
Refresh Default page, and before 10 secs elapses, refresh Page2....Default.aspx is rendering "Page2". Yes it is not thread safe.
Now try this, open the following page in two browser tabs:
protected void Page_Load(object sender, EventArgs e)
{
Session["x"] = "ABC";
Thread.Sleep(10000);
Response.Write(DateTime.Now.ToString());
}
Now it seems that first thread lock on the session object, and the other have to wait for the whole 10 secs!!
Now try First case but put Global.ascx in the project...and say what, first thread locks on something!!!!!
In real application, it is common to access global state variables that need a thread safety like Sessions.
If you have a Backend application that contains a report that need 30 secs to render. and if 60 users opens that report, then user number 61 will wait for half hour till page load into his browser!! That is a Typical thread starvation!! and I will be probably fired :(
1) Every user is waiting till another finish his request. that should make any heavy page a problem. because it can make the whole web application in-responsive. Agree?
2) How to fix that situation?
3) Under which topic I should research about that? Do you know a good book?
Thanks