我一直在对我的应用程序进行一些 cpu 分析,我注意到需要大量时间的一件事是确保我每秒发送 mo 多于查询到 web 服务的代码。相比之下,实际查询本身和结果处理只需要很少的时间,当然有一个 I/O 组件在等待结果,但我想做的是减少 cpu,因为应用程序有时必须在单个 cpu 机器上运行
使用YourKit Profiler使用大量 cpu 的调用是
java.util.concurrent.locks.AbstractQueuedSynchronizer.aquireQueued()
我的延迟方法如下
public class SearchServer
{
private static java.util.concurrent.locks.Lock delayLock = new ReentrantLock();
private static AtomicInteger queryQueue = new AtomicInteger();
private static AtomicLong queryDelay = new AtomicLong();
static void doDelayQuery()
{
delayLock.lock();
try
{
if(isUserCancelled())
{
return;
}
//Ensure only send one query a second
Date currentDate = new Date();
long delay = currentDate.getTime() - querySentDate.getTime();
if (delay < delayInMilliseconds)
{
try
{
long delayBy = delayInMilliseconds - delay;
queryDelay.addAndGet(delayBy);
Thread.sleep(delayBy);
logger.info(Thread.currentThread().getName() + ":Delaying for " + delayBy + " ms");
}
catch (InterruptedException ie)
{
Thread.currentThread().interrupt();
throw new UserCancelException("User Cancelled whilst thread was delay sleeping");
}
}
}
finally
{
//We set before unlocking so that if another thread enters this method before we start query we ensure they
//do not skip delay just because the query that this thread has delayed for has started
querySentDate = new Date();
delayLock.unlock();
}
}
}
缥缈止盈
相关分类