Java Servlet中的ExecutorService [英] ExecutorService in Java Servlet

查看:150
本文介绍了Java Servlet中的ExecutorService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在java servlet中同时执行一些任务(多数调用多个外部URL与请求参数和读取数据),并在几秒钟内向用户发送响应。我试图使用ExecutorService来实现相同的。我需要在doGet方法中的每个用户请求中创建四个FutureTasks。每个任务运行约5-10秒,对用户的总响应时间约为15秒。

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 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 runs for around 5-10 sec and the total response time to the user is around 15 sec.

您可以建议使用以下设计ExecutorService in a Java servlet?

Can you please suggest which of the following design is better while using ExecutorService in a Java servlet?

1)(为每个请求创建newFixedThreadPool并尽快关闭) b $ b

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();
     }

}

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();
     }

}

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();
      }

}


推荐答案

第一个不应该是一个选项。线程池(和可能的任何池)的想法是最小化构建池成员(在这种情况下,工作线程)所需的开销和内存。因此,一般来说,当您的应用程序启动时,应启动池,并在关闭时销毁。

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.

至于2和3之间的选择,请检查接受的答案下面的帖子。答案解释了差异,然后您可以决定哪一种更适合您的需求: newcachedthreadpool-vs-newfixedthreadpool

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

这篇关于Java Servlet中的ExecutorService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆