我需要测试一个应用程序,以了解多线程应用程序中线程数量和执行总持续时间之间的关系。这就是我目前正在尝试的:
//Start time of thread executions.
long start = System.currentTimeMillis();
//Here, I am initializing the thread list which will store each thread.
MyThreadClass[] threads = new MyThreadClass[numOfThreads];
//Start each thread.
for (int i = 0; i < threads.length; i++) {
threads[i] = new MyThreadClass(/*Constructor parameters here*/);
//Start the thread.
threads[i].start();
//There is a variable assignment to change the parameter of a constructor on next iteration.
}
//Join all the threads.
for (MyThreadClass thread : threads) {
thread.join();
}
//After all threads are done, compute the elapsed time to get computation time.
long finish = System.currentTimeMillis();
long duration = finish - start;
System.out.println("Total duration of the computation with " + threads.length + " thread is " + duration + " milliseconds.");但是,当我通过使用一些for循环包装main()中的代码来测试应用程序时,问题就出现了。我相信,这些数字一定是不合理的。
Total duration of the computation with 1 thread is 50 milliseconds.
Total duration of the computation with 2 thread is 25 milliseconds.
Total duration of the computation with 3 thread is 25 milliseconds.
Total duration of the computation with 4 thread is 15 milliseconds.
Total duration of the computation with 5 thread is 19 milliseconds.
Total duration of the computation with 6 thread is 16 milliseconds.
Total duration of the computation with 7 thread is 15 milliseconds.
Total duration of the computation with 8 thread is 25 milliseconds.
Total duration of the computation with 9 thread is 20 milliseconds.
Total duration of the computation with 10 thread is 20 milliseconds.
Total duration of the computation with 11 thread is 30 milliseconds.
Total duration of the computation with 12 thread is 34 milliseconds.
Total duration of the computation with 13 thread is 34 milliseconds.
Total duration of the computation with 14 thread is 18 milliseconds.
Total duration of the computation with 15 thread is 18 milliseconds.
Total duration of the computation with 16 thread is 18 milliseconds.
Total duration of the computation with 17 thread is 15 milliseconds.
Total duration of the computation with 18 thread is 19 milliseconds.
Total duration of the computation with 19 thread is 17 milliseconds.
Total duration of the computation with 20 thread is 13 milliseconds.
Total duration of the computation with 21 thread is 13 milliseconds.
Total duration of the computation with 22 thread is 13 milliseconds.
Total duration of the computation with 23 thread is 12 milliseconds.
Total duration of the computation with 24 thread is 12 milliseconds.
Total duration of the computation with 25 thread is 14 milliseconds.
Total duration of the computation with 26 thread is 15 milliseconds.
Total duration of the computation with 27 thread is 18 milliseconds.
Total duration of the computation with 28 thread is 20 milliseconds.
Total duration of the computation with 29 thread is 20 milliseconds.
Total duration of the computation with 30 thread is 15 milliseconds.
Total duration of the computation with 31 thread is 22 milliseconds.
Total duration of the computation with 32 thread is 23 milliseconds.
Total duration of the computation with 33 thread is 17 milliseconds.
Total duration of the computation with 34 thread is 33 milliseconds.
Total duration of the computation with 35 thread is 20 milliseconds.
Total duration of the computation with 36 thread is 27 milliseconds.
Total duration of the computation with 37 thread is 20 milliseconds.
Total duration of the computation with 38 thread is 23 milliseconds.
Total duration of the computation with 39 thread is 26 milliseconds.
Total duration of the computation with 40 thread is 20 milliseconds.
Total duration of the computation with 41 thread is 30 milliseconds.
Total duration of the computation with 42 thread is 22 milliseconds.
Total duration of the computation with 43 thread is 20 milliseconds.
Total duration of the computation with 44 thread is 20 milliseconds.
Total duration of the computation with 45 thread is 20 milliseconds.
Total duration of the computation with 46 thread is 21 milliseconds.
Total duration of the computation with 47 thread is 26 milliseconds.
Total duration of the computation with 48 thread is 22 milliseconds.
Total duration of the computation with 49 thread is 22 milliseconds.
Total duration of the computation with 50 thread is 16 milliseconds.
Total duration of the computation with 51 thread is 14 milliseconds.
Total duration of the computation with 52 thread is 18 milliseconds.
Total duration of the computation with 53 thread is 12 milliseconds.
Total duration of the computation with 54 thread is 20 milliseconds.
Total duration of the computation with 55 thread is 21 milliseconds.
Total duration of the computation with 56 thread is 30 milliseconds.
Total duration of the computation with 57 thread is 30 milliseconds.
Total duration of the computation with 58 thread is 30 milliseconds.
Total duration of the computation with 59 thread is 25 milliseconds.
Total duration of the computation with 60 thread is 15 milliseconds.
Total duration of the computation with 61 thread is 10 milliseconds.
Total duration of the computation with 62 thread is 18 milliseconds.
Total duration of the computation with 63 thread is 12 milliseconds.
Total duration of the computation with 64 thread is 15 milliseconds.
Process finished with exit code 0例如,执行7个线程的任务所需的时间与64个线程的时间相同。里面有点问题,但我没办法把它修好。我已经在没有迭代的情况下对程序进行了测试,即通过手动提供线程总数的一些输入,并且没有任何变化。run()函数只执行一次for迭代,而MyThreadClass扩展了Thread类。我想,我需要某种机制在线程完成后杀死它们,但我想加入它们已经做到了这一点。请指出您认为有问题的地方。提前谢谢。
编辑:run()中的循环迭代了一百万次。你可能会说,这是实验的固定变量。
发布于 2020-05-07 21:44:04
您可以创建一个自定义ThreadPoolExecutor来跟踪每个任务的执行时间,如下所示:
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
public class TimeTrackingExecutor extends ThreadPoolExecutor {
private AtomicLong totalExecutionTime = new AtomicLong();
ThreadLocal<Long> times = new ThreadLocal<>();
public TimeTrackingExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
@Override
protected void beforeExecute(Thread t, Runnable r) {
times.set(System.nanoTime());
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
long delta = System.nanoTime() - times.get();
totalExecutionTime.addAndGet(delta);
}
public long getTotalExecutionTime() {
return totalExecutionTime.get();
}
}在每次任务启动之前和之后调用的方法beforeExecute和afterExecute:https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html
通过更改线程池核心/最大大小,您可以看到与线程数量的相关性。
https://stackoverflow.com/questions/61643289
复制相似问题