当我从命令行运行这个长时间运行的进程时,大约需要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需要更长的时间。
Tomcat似乎不可能对发布的特定行的运行时间产生任何影响。您可以提供LongRunningProcess的高级概述吗?对我而言,他们所做的事情似乎更有可能放缓,而不是您发布的内容。或者你是否有理由知道它是你发布的控制逻辑而不是任务逻辑? – Pace
感谢@Pace我已经用更完整的代码更新了这个问题,这些代码在我的tomcat服务器上导致了这个问题。即使有这个微不足道的循环,在运行tomcat时似乎也会花费更长的时间。 –
Tomcat忙于尝试完成任何工作吗?通过发出HTTP请求,你是否将任何负载放在Tomcat上? –