分类: 系统运维
2009-06-15 16:37:41
Code
public class Handler1 :IHttpAsyncHandler
{
IHttpAsyncHandler 成员#region IHttpAsyncHandler 成员
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
context.Response.Write("
Begin IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread+Thread.CurrentThread.ManagedThreadId + "
\r\n");}
#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("
Completion IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "
\r\n");这是Asp.net中的异步处理。Asp.net的异步处理目的就是要搞高服务器的 接受请求的处理能力,但有一点不明白,使用异步处理时,在BeginProcessRequest时,会将处理当前请求的线程放回到线程池中。放回到线程 池中的此线程继续接受新的请求,但是在StartAsyncWork这个时候,同样会从当前线程池中获取一个线程继续处理这个IO,这样并不能提高服务器 的吞吐量,相反会在线程间切换浪费CPU时间和资源,Asp.net异步处理就没有意义了(其实是有意义的)。
问题:Asp.net的异步能够提高服务器的吞吐量,但是我始终无法理解他是怎么提高其处理能力的。