当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

Asp.net的异步如何提高服务器的吞吐量

 Code
public class Handler1 :IHttpAsyncHandler
{
IHttpAsyncHandler 成员#region IHttpAsyncHandler 成员
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
context.Response.Write("<p>Begin IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread+Thread.CurrentThread.ManagedThreadId + "</p>\r\n");
AsyncHandler hand = new AsyncHandler(cb,context,extraData);
hand.StartAsyncWork();
return hand;
}
public void EndProcessRequest(IAsyncResult result)
{

}
#endregion
IHttpHandler 成员#region IHttpHandler 成员

public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
throw new NotImplementedException();
}
#endregion
}
public class AsyncHandler : IAsyncResult
{
private bool _completed;
private Object _state;
private AsyncCallback _callback;
private HttpContext _context;
public AsyncHandler(AsyncCallback callback, HttpContext context, Object state)
{
_callback = callback;
_context = context;
_state = state;
_completed = false;
}

IAsyncResult 成员#region IAsyncResult 成员
public object AsyncState
{
get
{
return _state;
}
}

public System.Threading.WaitHandle AsyncWaitHandle
{
get { throw new NotImplementedException(); }
}

public bool CompletedSynchronously
{
get { return false; }
}

public bool IsCompleted
{
get { return _completed; }
}

#endregion

public void StartAsyncWork()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);
}
private void StartAsyncTask(Object workItemState)
{
Thread.Sleep(5000);//模拟IO
_context.Response.Write("<p>Completion IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>\r\n");
_context.Response.Write("Hello World from Async Handler!" + Thread.CurrentThread.ManagedThreadId);
_completed = true;
_callback(this);
}
}

    这是Asp.net中的异步处理。Asp.net的异步处理目的就是要搞高服务器的接受请求的处理能力,但有一点不明白

    使用异步处理时,在BeginProcessRequest时,会将处理当前请求的线程放回到线程池中。放回到线程池中的此

    线程继续接受新的请求,但是在StartAsyncWork这个时候,同样会从当前线程池中获取一个线程继续处理这个IO,

    这样并不能提高服务器的吞吐量,相反会在线程间切换浪费CPU时间和资源,Asp.net异步处理就没有意义了

    (其实是有意义的)。

    问题:Asp.net的异步能够提高服务器的吞吐量,

    但是我始终无法理解他是怎么提高其处理能力的。a

相关内容
赞助商链接