[英]Can I abuse the browser timeout to avoid busy-polling the server for the result of a long-running task
[英]How can I have a callback on stdout/stderr readiness instead of busy polling?
我发现从Process
stdout读取的所有讨论都以阻塞的方式进行,例如:
Process process=Runtime.getRuntime().exec(cmd);
BufferedReader in=new BufferedReader(
new InputStreamReader(process.getInputStream()));
for(String line;(line=in.readLine())!=null;)
{
useText(line);
}
如果我尝试避免阻塞,我的选择是使用in.ready()
轮询流。
但是我真正需要的是让流程继续运行并做一些有用的事情,并且仅在数据实际可用时才尝试读取任何内容。 所以我正在寻找类似Qt的QProcess::readyReadStandardOutput
信号。 请注意, BufferedReader
的nethod ready
不是我要寻找的:它只是我必须反复检查的一种方法,而不是任何种类的信号。
Java中有类似的东西吗?
如果有事件循环,则可以启动后台线程将事件添加到该事件循环。
例如
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true); // capture errors as well.
Process process = pb.start();
BufferedReader in = new BufferedReader(
new InputStreamReader(process.getInputStream()));
Thread t = new Thread(() -> {
try {
for (String line; (line = in.readLine()) != null; ) {
Activity.runOnUiThread(() -> useText(line));
}
} catch (Exception e) {
logger.log(e);
} finally {
Activity.runOnUiThread(() -> noMoreText());
}
});
t.setDaemon(true);
t.start();
您可以有一个仅执行queue.add(in.readLine())
的线程,然后您的主线程可以轮询此队列。 这样,所有的多线程问题就变得简单且包含在内。
static final String EOF = new String(); // use for == comparison later
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true); // capture errors as well.
Process process = pb.start();
BufferedReader in = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int capacity = 1024;
BlockingQueue<String> lines = new ArrayBlockingQueue<>(capacity);
Thread t = new Thread(() -> {
try {
for (String line; (line = in.readLine()) != null; ) {
lines.add(line);
while (lines.remainingCapacity() < 2) // don't run out of memory if too much.
Thread.sleep(50);
}
} catch (Exception e) {
logger.log(e);
} finally {
lines.offer(EOF);
}
});
t.setDaemon(true);
t.start();
// later in a loop
String line = lines.poll();
if (line == null) // no data yet.
else if (line == EOF) // we have the EOF marker.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.