【发布时间】:2017-10-05 14:52:13
【问题描述】:
在我的程序中,我连接到另一种语言的解释器进程。有时我需要程序向解释器询问几件事并使用它的响应。
进程存储在 IProcess 变量中,通信是通过进程的 IStreamsProxy 完成的。为了接收响应,我向 IStreamsProxy 添加了一个 IStreamListener。
但是,当我写入 IStreamsProxy(使用 write() 方法)时,我需要代码来等待响应,例如要调用的侦听器的 streamAppend 方法。
我尝试使用 wait() 和 notify() 方法,但我不知道应该调用什么对象。
进行通信的查询类调用这样的方法:
/**
* Sends a command to the interpreter. Makes the current thread wait for an answer of the interpreter.
* @throws IOException
*/
private synchronized void send(String command) throws IOException{
this.s48Proxy.write(command);
try {
System.out.println("waiting for reply");
this.wait();
} catch (InterruptedException e) {
System.out.println("interruption exception: "+e.getMessage());
}
}
和听众:
private class Listener implements IStreamListener {
private Query query;
public Listener(Query query) {
super();
this.query = query;
}
@Override
public void streamAppended(String arg0, IStreamMonitor arg1) {
System.out.println("got interpreter answer");
query.setCurrentOutput(arg0.substring(0, arg0.lastIndexOf("\n")).trim());
query.notify();
}
}
streamAppend() 方法调用查询类的notify()-方法。但是,这不起作用。 “等待回复” 是我得到的最后一个回复。
我该怎么做?或者我可以使用其他任何方法来自动实现这一点?
【问题讨论】:
标签: java process response wait