@PostConstruct
public void start() {
handleXXXThreadStart();
}
/**
* 系统启动后启动线程
*/
private void handleXXXThreadStart() {
log.info("处理XXX数据线程启动........");
Thread t = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
int maxCount = 100;
List<DataType> temp = new ArrayList();
while (runFlag) {
try {
if (queue.isEmpty()) {
continue;
}
if (queue.size() < maxCount) {
TimeUnit.SECONDS.sleep(1);
}
temp.clear();
int count = queue.drainTo(temp, maxCount);
if (count > 0) {
dealData(temp);
}
} catch (Exception e) {
log.error("handle XXX error", e);
} finally {
try {
TimeUnit.MILLISECONDS.sleep(maxCount);
} catch (InterruptedException e) {
log.error(e);
}
}
}
log.info("XXX-handle-thread stoped... ");
}
});
t.setName("dadata-handle-thread");
t.start();
}
@PreDestroy
public void stop() {
log.info("handle-thread stop ......");
this.runFlag = false;
}
Spring实现程序启动后开启线程并持续处理数据
最新推荐文章于 2023-06-07 14:17:55 发布