2011-02-16 37 views
2

我已經通過IHttpAsyncHandler在C#中使用.Net異步編程模型實現了異步http請求處理。java模擬到C#APM/IHttpAsyncHandler

我是新來的java,但想完成相同的目標 - 開始一個請求,讓它放棄請求處理線程池線程並異步處理,當所有處理完成時發出信號,觸發處理程序的結束請求回調並將處理結果寫入響應流。我覺得這肯定存在,我不需要推出我自己的解決方案,但搜索異步http處理只會出現以AJAX爲中心的解決方案(我希望在服務器端進行異步處理)。

在java中有IHttpAsyncHandler的模擬嗎?

回答

0

在Java Servlets中,每個請求都獲得它自己的線程,並且不會限制其他請求的處理。因此,原則上他們已經是異步的:http://www.codestyle.org/java/servlets/faq-Threads.shtml

據我所知,在IHttpAsyncHandler淨應該是海格性能,但不是每個人都同意:http://geekswithblogs.net/SanjayU/archive/2009/01/06/ihttphandler-vs-ihttpasynchandler.aspx

更新:

要啓動多個並行任務並等待它們全部完成,最好使用ExecutorService。你可以做這樣的事情你的servlet方法內:

ExecutorService executor = Executors.newCachedThreadPool(numThreads); 
    for (int i = 0; i < numParallelTasks; i++) { 
     Runnable worker = new MyRunnable(); 
     executor.execute(worker); 
    } 
    // This will make the executor accept no new threads 
    // and finish all existing threads in the queue 
    executor.shutdown(); 
    // Wait until all threads are finish 
    while (!executor.isTerminated()) { 

    } 
    // all tasks done 
+0

在準備我的回覆時,我希望能夠並行運行多個可能長時間運行的非CPU綁定操作。 – rollinwiththepunches 2011-02-16 16:09:53