2014-12-18 43 views
3

当我从命令行运行这个长时间运行的进程时,大约需要30秒才能完成。当我将相同的代码部署到tomcat7并从简单的Vaadin Web应用程序中调用相同的功能时,它需要将近150秒。这是执行特定功能的时间,而不是与界面速度有关的任何事情。长时间运行的多线程应用程序在部署到tomcat时运行速度比从命令行运行时运行速度低4倍

这里是一个简单的例子,说明了这个问题:

public static void main(String[] args) { 

    try { 
     multiCounter(800); 
    } catch (InterruptedException | ExecutionException e) {e.printStackTrace();} 

    System.out.println("Completed"); 
} 


public static void multiCounter(int numberOfCounters) throws InterruptedException, ExecutionException 
{ 

    //estimate the number of available processors 
    int maxNumberOfThreads = Runtime.getRuntime().availableProcessors(); 

    //create thread pool and queue for future jobs 
    ExecutorService pool = Executors.newFixedThreadPool(maxNumberOfThreads); 

    ArrayList<Future<Integer>> futureJobs = new ArrayList<Future <Integer>>(); 

    for(int index=0; index<numberOfCounters; index++)  
    { 
     Callable<Integer> callable = new dummyCalculator(); 
     Future<Integer> future = pool.submit(callable); 
     futureJobs.add(future); 
    } 

    //placeholder for results 
    ArrayList <Integer> results= new ArrayList <Integer>(0); 

    //pull completed jobs from queue and extract results, 
    //adding to results container 
    for(Future<Integer> future : futureJobs) 
    {results.add(future.get());} 

    for(Integer res : results) 
    {System.out.println("Count:" + res);} 

    //close thread pool 
    pool.shutdown(); 
} 

final static class dummyCalculator implements Callable<Integer> 
{ 
    @Override 
    public Integer call() throws Exception { 

     Integer counter = 0; 

     for(int p1Index=0; p1Index<800; p1Index++) 
     { 
      for(int p2Index=p1Index; p2Index<800; p2Index++) 
      { 
       for(int markerIndex=0; markerIndex<200; markerIndex++) 
       {counter++;} 
      } 
     } 

     return(counter); 
    } 
} 

我上运行该服务器有48个可用的核心。当我从命令行运行这段代码时,我得到48个线程,每个线程运行〜98%。当我通过tomcat运行它时,每个线程按顶部大约82-86%排列。如果我减少了tomcat代码中的线程数量,每个线程的%增加。每个线程有12个线程会让我92%。 1线程99%...

是否tomcat做某些事情以某种方式限制这个胎面池,或者有一些我不知道的线程的额外开销?或者在这种情况下是否有更好的多线程方式?一些开销显然是可以接受的,但是在这里看起来不太合适,因为它通过tomcat需要更长的时间。

+1

Tomcat似乎不可能对发布的特定行的运行时间产生任何影响。您可以提供LongRunningProcess的高级概述吗?对我而言,他们所做的事情似乎更有可能放缓,而不是您发布的内容。或者你是否有理由知道它是你发布的控制逻辑而不是任务逻辑? – Pace

+0

感谢@Pace我已经用更完整的代码更新了这个问题,这些代码在我的tomcat服务器上导致了这个问题。即使有这个微不足道的循环,在运行tomcat时似乎也会花费更长的时间。 –

+0

Tomcat忙于尝试完成任何工作吗?通过发出HTTP请求,你是否将任何负载放在Tomcat上? –

回答

1

您是否试图确保Tomcat和您的作业使用的线程总数总计系统上的核心数?我敢说,你可能会接近这些线程的99%执行。

我的另一个问题是如果Tomcat线程比你的工作线程具有更高的优先级。如果是这种情况,那么你应该在一致的基础上看到这种差异。我不记得是否可以使用jconsole或visualvm查看线程优先级,但是可能增加线程池的线程优先级应该提供更多的cpu周期,但这会影响tomcat处理Web请求的能力。

相关问题