我正在开发一个项目,其中需要测量程序所花费的总时间和程序所花费的平均时间。该程序是一个多线程程序。总共花费的时间和所有线程所花费的平均时间
在该程序中,每个线程都在特定范围内工作。输入参数是Number of Threads
和Number of Task
。
如果number of threads is 2
和number of tasks is 10
那么每个线程将执行10 tasks
。所以这意味着2线程将做20 tasks
。
使指─
第一线应使用1 and 10
及第二线程之间的ID应该是使用11 and 20
之间的ID。
我得到了上述方案的工作。现在我想测量所有线程所花费的总时间和平均时间。所以我在我的程序中得到了下面的设置。
问题陈述: -
谁能告诉我我将尝试所有的线程来衡量Total time and Average time taken
的方法是在我下面的程序正确与否?
//create thread pool with given size
ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
long startTime = 0L;
try {
readPropertyFiles();
startTime = System.nanoTime();
// queue some tasks
for (int i = 0, nextId = startRange; i < noOfThreads; i++, nextId += noOfTasks) {
service.submit(new XMPTask(nextId, noOfTasks, tableList));
}
service.shutdown();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} finally {
long estimatedTime = System.nanoTime() - startTime;
logTimingInfo(estimatedTime, noOfTasks, noOfThreads);
}
private static void logTimingInfo(long elapsedTime, int noOfTasks, int noOfThreads) {
long timeInMilliseconds = elapsedTime/1000000L;
float avg = (float) (timeInMilliseconds)/noOfTasks * noOfThreads;
LOG.info(CNAME + "::" + "Total Time taken " + timeInMilliseconds + " ms. And Total Average Time taken " + avg + " ms");
}
仅供参考,上面的程序,我将运行在一台专门用于我们的LNP测试的机器上。 – AKIWEB 2013-03-16 21:07:09