ExecutorService in Java Servlet

后端 未结 2 967
你的背包
你的背包 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条回答
  • 2021-01-12 16:31

    The first should not be an option. The idea of a thread pool (and probably any pool) is to minimize the overhead and memory required for the construction of the pool members (in this case, the worker threads). so In general the pools should be inited when your application is started and destroyed when it shuts down.

    As for the choice between 2 and 3, please check the accepted answer in the following post. The answer explains the difference and you can then decide which one suits your needs better : newcachedthreadpool-v-s-newfixedthreadpool

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题