简体   繁体   English

如何捕获正在运行的 java 进程的经过时间以引发中间警报

[英]How to capture elapsed time of a running java process to raise intermediate alerts

I have a java process which could either complete sooner or take longer time based on the volume of data its processing.我有一个 java 进程,根据其处理的数据量,它可以更快地完成或花费更长的时间。 However I need a way to capture the elapsed time after certain time continuously and need to log an alert message concurrently without interrupting the main process.但是,我需要一种方法来连续捕获特定时间后的经过时间,并且需要同时记录警报消息而不中断主进程。 I have explored Future option with ExecutorService but it would terminate the process after the set timeout and raises Timeout exception.我已经使用 ExecutorService 探索了 Future 选项,但它会在设置超时后终止进程并引发超时异常。 Please suggest if you have any solution to achieve this requirement.请建议您是否有任何解决方案来实现此要求。

just some hints:只是一些提示:

  • you need to have 2 separate threads: one for main logic and one for checking the progress您需要有 2 个单独的线程:一个用于主逻辑,一个用于检查进度
  • the thread that checks the progress should be able to know if the main thread has finished or not (the simplest solution volatile boolean flag)检查进度的线程应该能够知道主线程是否完成(最简单的解决方案 volatile boolean 标志)
  • the thread that checks the progress can be invoked by timer with some period检查进度的线程可以由定时器调用一段时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM