ExecutorService in Java Servlet

后端 未结 2 1058
你的背包
你的背包 2021-01-12 15:53

I need to perform some tasks(Mostly Call multiple External URL\'s with request parameters and read data) concurrently in java servlet and send response to user within a few

2条回答
  •  梦毁少年i
    2021-01-12 16:31

    Creating and destroying a thread pool for each request is a bad idea : too expensive.

    If you have some way to remember which HTTP request each URL fetching task is related to, I'd go for a CachedThreadPool. Its ability to grow and shrink on-demand will do wonders, because the URL fetching tasks are totally independant and network-bound (as opposed to CPU or memory-bound).

    Also, I would wrap the ThreadPool in a CompletionService, which can notify you whenever a job is done, regardless of its submission order. First completed, first notified. This will ensure you don't block on a sloooow job if faster ones are already done.

    CompletionService is easy to use : wrap it around an existing ThreadPool (newCachedThreadPool for example), submit() jobs to it, and then take() the results back. Please note that the take() method is blocking.

    http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CompletionService.html

提交回复
热议问题