需要一个基于Java的可中断定时器线程
我有一个主程序,它正在目标设备(智能手机)上运行脚本,并在一个 while 循环中等待标准输出消息。然而,在这种特殊情况下,标准输出上的一些心跳消息可能会间隔近 45 秒到 1 分钟。
例如:
stream = device.runProgram(RESTORE_LOGS, new String[] {});
stream.flush();
String line = stream.readLine();
while (line.compareTo("") != 0) {
reporter.commentOnJob(jobId, line);
line = stream.readLine();
}
所以,我希望能够在从标准输出读取行并需要睡眠窗口后启动一个新的可中断线程。在能够读取新行后,我希望能够中断/停止(无法终止进程)、处理标准输出文本的换行符并重新启动进程。
如果我无法在计时器窗口(比如 45 秒)内读取一行,我也想找到一种方法来摆脱 while 循环。
我已经尝试过 thread.run、thread.interrupt 方法。但在杀死并启动新线程时遇到困难。
这是最好的出路还是我错过了一些明显的事情?
I have a Main Program which is running a script on the target device(smart phone) and in a while loop waiting for stdout messages. However in this particular case, some of the heartbeat messages on the stdout could be spaced almost 45secs to a 1minute apart.
something like:
stream = device.runProgram(RESTORE_LOGS, new String[] {});
stream.flush();
String line = stream.readLine();
while (line.compareTo("") != 0) {
reporter.commentOnJob(jobId, line);
line = stream.readLine();
}
So, I want to be a able to start a new interruptible thread after reading line from stdout with a required a sleep window. Upon being able to read a new line, I want to be able to interrupt/stop(having trouble killing the process), handle the newline of stdout text and restart a process.
And it the event I am not able to read a line within the timer window(say 45secs) I want to a way to get out of my while loop either.
I already tried the thread.run, thread.interrupt approach. But having trouble killing and starting a new thread.
Is this the best way out or am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来 System.in 的实现在不同平台上差异很大,特别是并不总是提供可中断性或异步关闭。
这是一种不依赖于这些功能的解决方法,但代价是无法正确清理;如果在超时到期之前未收到输入,
Consumer
线程将处于阻塞的read()
状态。It looks like the implementation of
System.in
varies considerably across platforms and, in particular, doesn't always offer interruptibility or asynchronous closure.Here is a workaround that doesn't rely on those features, but at the cost of failing to clean up properly; if input isn't received before the timeout expires, the
Consumer
thread is left in a blockingread()
.这确实看起来是一个更优雅的解决方案,特别是考虑到我以前没有使用过 ScheduledExecutorService 。但我仍然在努力将所有的碎片拼凑在一起!我不确定工作人员是否以及何时调用其 45 秒倒计时?另外,我的目的是让这样的工作人员在遇到一行标准输出时重新启动倒计时,实质上将倒计时重置为新的 45 秒窗口。这有助于澄清吗?
当我努力将 ScheduledExecutorService 合并到我的解决方案中时,这里是我用来使用线程复制它的完整示例代码。让我知道你是否能比我更早到达。我能够在遇到的每个 stdout 换行符上调用一个线程,但在声明的时间窗口内没有发生中断时无法优雅地处理这种情况:( 希望代码中的注释足够详细地传达我的意图,否则请让我我知道并且我可以澄清:
that sure seems like a more elegant solution, especially given I haven't used ScheduledExecutorService before. But I'm still struggling to put all the pieces together though! I'm not sure if and when worker invoked for its 45sec countdown? Also, my intention is for such a worker to restart the countdown once it encounters a line of stdout, essentially resetting the countdown to a fresh 45sec window. Does that help clarify.
While I work to incorporate ScheduledExecutorService into my solution, here's the entire sample code i've been using to replicate it using the threads. Lemme know if I you get it to sooner than I can. I am able to invoke a thread on every newline of stdout I come across, but cannot gracefully handle the case when no interruption occurs for the declared time window :( Hope the comments in the code detailed enough to convey my intentions, else pls let me know and I could clarify: