混淆工作进程线程 [英] Confusion about Worker Process threading

查看:223
本文介绍了混淆工作进程线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在浏览器中的两个不同的标签页打开下面的页面。击中刷新按钮,以避免浏览器缓存中获取页面:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        Thread.sleep代码(10000);
        的Response.Write(DateTime.Now.ToString());
    }

正如你所看到的,似乎没有为每个请求创建一个线程,而且他们也没有等到对方没有获得锁。

现在创建以下页面,并GlobalCustomClass

GlobalCustomClass

 公共类GlobalCustomClass
    {
        公共静态字符串GlobalVariable
        {
            得到;
            组;
        }
    }

Default.aspx的

 保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            GlobalCustomClass.GlobalVariable =默认页;
            Thread.sleep代码(10000);
            的Response.Write(DateTime.Now.ToString());
            的Response.Write(< BR />中);
            的Response.Write(GlobalCustomClass.GlobalVariable);
        }

Page2.aspx

 保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            的Response.Write(DateTime.Now.ToString());
            的Response.Write(< BR />中);
            GlobalCustomClass.GlobalVariable =第2页;
            的Response.Write(GlobalCustomClass.GlobalVariable);        }

刷新默认页,而之前10秒过后,刷新2页.... Default.aspx的被渲染第2页。是的,它不是线程安全的。

现在试试这个,两个浏览器标签打开以下页面:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
         会话[X] =ABC;
         Thread.sleep代码(10000);
         的Response.Write(DateTime.Now.ToString());
    }

现在看来,对会话对象第一个线程锁,其他要等整个10秒!

现在尝试第一种情况,但把Global.ascx项目......说什么,第一个线程锁的东西!!!!!

在实际应用中,它是常见的访问需要像会话线程安全全局状态变量。

如果你有一个包含需要30秒来呈现报表后端应用程序。如果60个用户打开该报告,然后用户号码61将等待半小时,直到页面加载到他的浏览器!这是一个典型的线程匮乏!我将可能触发:(

1)每个用户在等待,直到另一个完成了他的请求。应该作出任何重页的一个问题。因为它可以使敏感的整个Web应用程序。 同意?

2)如何解决这种情况?

3)在哪一个主题,我应该考证一下呢?你知道一本好书?

感谢


解决方案

  

如果60个用户打开该报告,然后用户号码61将等待
  半小时,直到页面加载到他的浏览器!


这是不正确的,因为锁只有每个会话但并非全球所有用户/会话。

由于您是使用相同的浏览器调用两个页面,您也使用相同的ASP.NET会话(因为这两个选项卡共享ASP.NET会话cookie)。如果使用两个不同的浏览器来模拟两个用户,你会看到第二个用户不会被阻塞20秒。

另外,当然锁定,如果你使用的是会话状态才会发生。如果你想创建一个高性能的Web应用程序(有很多用户同时使用,或同时/异步请求的),那么你应该尽量避免会话状态尽可能的。

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

解决方案

and if 60 users opens that report, then user number 61 will wait for half hour till page load into his browser!!

That's not true, because the lock is only per-session but not global for all users/sessions.

Since you were using the same browser to call the two pages, you were also using the same ASP.NET session (because both tabs share the ASP.NET session cookie). If you use two different browsers to simulate two users, you will see that the second user will not be blocked for 20 seconds.

Also, the locking of course only happens if you are using session state. If you want to create a high-performance web app (with lots of simultaneous users, or simultaneous/asynchronous requests), then you should try to avoid session state as much as possible.

这篇关于混淆工作进程线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆