由于某种原因,我们计划在我们的项目中使用kestrel队列。我们做一些恶魔,主要的问题是如何从CPU利用率低的队列中获取数据,并且有效。我们实现获取的方式是,如果我们无法从队列中获取数据超过5次,我们会将线程休眠100ms以降低CPU利用率。如何从kestrel队列有效提取数据
while (running) {
try {
LoginLogQueueEntry data = kestrelQueue.fetch();
if (null != data && data.isLegal()) {
entryCacheList.add(data); //add the data to the local caceh
resetStatus();
} else {
failedCount++;
//if there is no data in the kestrel and the local cache is not empty, insert the data into mysql database
if (failedCount == 1 && !entryCacheList.isEmpty()) {
resetStatus();
insertLogList(entryCacheList); // insert current data into database
entryCacheList.clear(); //empty local cache
}
if (failedCount >= 5 && entryCacheList.isEmpty()) {
//fail 5 times. Sleep current thread.
failedCount = 0;
Thread.sleep((sleepTime + MIN_SLEEP_TIME) % MAX_SLEEP_TIME);
}
}
//Insert 1000 rows once
if (entryCacheList.size() >= 1000) {
insertLogList(entryCacheList);
entryCacheList.clear();
}
} catch (Exception e) {
logger.warn(e.getMessage());
}
有没有其他的好办法呢?我认为完美的方式是通过队列可以通知工作人员我们获取数据并获取数据。