java java使一个方法等待另一个进程的响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7713786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:06:05  来源:igfitidea点击:

java make a method wait for a response of another process

javaprocessresponsewait

提问by zhujik

In my program, I connect to an interpreter process for another language. I need the program sometimes to ask the interpreter several things and use it's response.

在我的程序中,我连接到另一种语言的解释器进程。我有时需要该程序向解释器询问几件事并使用它的响应。

The process is stored in a IProcess variable and the communication is done via the IStreamsProxy of the process. to recieve the response, I added an IStreamListener to the IStreamsProxy.

进程存储在 IProcess 变量中,通信通过进程的 IStreamsProxy 完成。为了接收响应,我向 IStreamsProxy 添加了一个 IStreamListener。

However, when I write to the IStreamsProxy (with the write() method), I need the code to wait for the response, e.g. the streamAppend-method of the listener to be called.

但是,当我写入 IStreamsProxy(使用 write() 方法)时,我需要等待响应的代码,例如要调用的侦听器的 streamAppend 方法。

I tried to use the wait() and notify() method, but I don't know on what objects I should call them.

我尝试使用 wait() 和 notify() 方法,但我不知道应该在哪些对象上调用它们。

The query class which makes the communication calls a method like this:

进行通信的查询类调用如下方法:

/**
 * 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());
    }
}

and the listener:

和听众:

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();
    }
}

The streamAppend()method calls the notify()-method on the query class. However, this does not work. "Waiting for reply"is the last response I get.

streamAppend()方法调用notify()查询类上的 -method。但是,这不起作用。“等待回复”是我得到的最后一个回复。

How can I do this? Or are there any other methods I could use to achieve this automatically?

我怎样才能做到这一点?或者我可以使用其他任何方法来自动实现这一目标吗?

回答by MasterCassim

You could use Object#wait& Object#notifiy.

你可以使用Object#wait& Object#notifiy

// @ IStreamsProxy
synchronized( someMonitor ) {
  someMonitor.wait( /* time? */ );
}

// @ Listener
synchronized( someMonitor ) {
  someMonitor.notify();
}

-> Javadoc Link

-> Javadoc 链接

回答by Thom

Use a semaphore like boolean waitingForResponse. Set this to true when you make your call, then go into

使用像 boolean waitingForResponse 这样的信号量。拨打电话时将此设置为 true,然后进入

while(waitingForResponse){ sleep(99) //should be your average response time from your call }

while(waitingForResponse){ sleep(99) // 应该是您呼叫的平均响应时间 }

In your listener, set waitinForResponse to false.

在您的侦听器中,将 waitinForResponse 设置为 false。