Java:计算每秒的性能
我有一个线程池,它同时执行一些繁重的操作,下面是我的代码 ExampleCompute.javaJava:计算每秒的性能,java,Java,我有一个线程池,它同时执行一些繁重的操作,下面是我的代码 ExampleCompute.java class ExampleCompute { private List<long> allData = new ArrayList<long>(); ExecutorService executor = Executors.newFixedThreadPool(1000); public calculate() { for(int i=0; i
class ExampleCompute {
private List<long> allData = new ArrayList<long>();
ExecutorService executor = Executors.newFixedThreadPool(1000);
public calculate() {
for(int i=0; i<myData.size; i++) {
MyWorker worker = new MyWorker(myData.get(i));
Future<long> currentDataPoint = executor.submit(worker);
allData.add(currentDataPoint);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
}
}
实现这一点的最佳方法是什么?您可以使用before和after方法来跟踪时间
public class TrackingThreadPool extends ThreadPoolExecutor {
private final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
private volatile long totalTime;
private volatile long totalTasks;
public TrackingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, ThreadFactory factory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, factory);
}
protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
startTime.set(new Long(System.currentTimeMillis()));
}
protected void afterExecute(Runnable r, Throwable t) {
long time = System.currentTimeMillis() - startTime.get().longValue();
synchronized (this) {
totalTime += time;
++totalTasks;
}
super.afterExecute(r, t);
}
}
公共类TrackingThreadPool扩展ThreadPoolExecutor{
private final ThreadLocal startTime=new ThreadLocal();
私人时间长;
私人任务;
公共跟踪线程池(int corePoolSize、int maximumPoolSize、long keepAliveTime、TimeUnit、,
BlockingQueue工作队列,ThreadFactory工厂){
super(corePoolSize、maximumPoolSize、keepAliveTime、unit、workQueue、factory);
}
执行前受保护的void(线程t,可运行r){
超级执行前(t,r);
set(新的Long(System.currentTimeMillis());
}
执行后受保护的无效(可运行的r、可丢弃的t){
long time=System.currentTimeMillis()-startTime.get().longValue();
已同步(此){
总时间+=时间;
++总体任务;
}
super.afterExecute(r,t);
}
}
有简单方法和复杂方法。一种简单的方法是让工作人员在作业完成后设置更新。但是,由于时间或作业状态与运行任务时的业务逻辑无关,因此可以使用AOP切入点拦截完成事件。AOP方法可能有些过分,但我认为这是正确的前进方向。AOP将影响性能。对于时间度量,它不应增加任务执行的所有复杂性。您希望时间是平均时间还是瞬时时间。如果是平均时间,则在作业完成时,您可以计算该瞬时的平均时间。如果我想要平均值,我可能会记录数据并稍后运行查询。
public class TrackingThreadPool extends ThreadPoolExecutor {
private final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
private volatile long totalTime;
private volatile long totalTasks;
public TrackingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, ThreadFactory factory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, factory);
}
protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
startTime.set(new Long(System.currentTimeMillis()));
}
protected void afterExecute(Runnable r, Throwable t) {
long time = System.currentTimeMillis() - startTime.get().longValue();
synchronized (this) {
totalTime += time;
++totalTasks;
}
super.afterExecute(r, t);
}
}