ExecutorService in Java Servlet

I need to perform some tasks (most often call several external URLs with request parameters and read data) at the same time in the Java servlet and send a response to the user within a few seconds. I am trying to use ExecutorService to achieve the same. I need four FutureTasks created in each user request in the doGet method. Each task takes about 5-10 seconds, and the total response time for the user is about 15 seconds.

Can you suggest which of the following options is best used when using ExecutorService in a Java servlet?

1) (Creating newFixedThreadPool for each request and closing it as soon as possible)

public class MyTestServlet extends HttpServlet
{

    ExecutorService myThreadPool = null;

    public void init()
    {
          super.init();

    }
    protected void doGet(HttpServletRequest request,HttpServletResponse response)
    {

        myThreadPool = Executors.newFixedThreadPool(4);
        taskOne   = myThreadPool.submit();
        taskTwo   = myThreadPool.submit();        
        taskThree = myThreadPool.submit();
        taskFour  = myThreadPool.submit();

        ...
        ...

        taskOne.get();
        taskTwo.get();
        taskThree.get();
        taskFour.get();

        ...

        myThreadPool.shutdown();


    }

     public void destroy()
     {

         super.destroy();
     }

}

2) ( newFixedThreadPool )

public class MyTestServlet extends HttpServlet
{

    ExecutorService myThreadPool = null;

    public void init()
    {
      super.init();
          //What should be the value of fixed thread pool so that it can handle multiple   user requests without wait???
          myThreadPool = Executors.newFixedThreadPool(20);

    }
    protected void doGet(HttpServletRequest request,HttpServletResponse response)
    {


        taskOne   = myThreadPool.submit();
        taskTwo   = myThreadPool.submit();        
        taskThree = myThreadPool.submit();
        taskFour  = myThreadPool.submit();

        ...
        ...

        taskOne.get();
        taskTwo.get();
        taskThree.get();
        taskFour.get();

        ...



    }

     public void destroy()
     {

          super.destroy();
          myThreadPool.shutdown();
     }

}

3) ( newCachedThreadPool )

public class MyTestServlet extends HttpServlet
{

      ExecutorService myThreadPool = null;

      public void init()
      {
        super.init();
            myThreadPool = Executors.newCachedThreadPool();

      }
      protected void doGet(HttpServletRequest request,HttpServletResponse response)
      {


          taskOne   = myThreadPool.submit();
          taskTwo   = myThreadPool.submit();        
          taskThree = myThreadPool.submit();
          taskFour  = myThreadPool.submit();

          ...
          ...

          taskOne.get();
          taskTwo.get();
          taskThree.get();
          taskFour.get();

          ...




     }

     public void destroy()
     {

            super.destroy();
            myThreadPool.shutdown();
      }

}
+5
2

. (, , ) , , ( ). , , .

2 3, , . , , : newcachedthreadpool-v-s-newfixedthreadpool

+1

- : .

, HTTP- URL-, CachedThreadPool. , URL- ( ).

, ThreadPool CompletionService, . , . , sloooow, .

CompletionService : ThreadPool (, newCachedThreadPool), submit(), (). , take() .

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

0

All Articles