Java - 等待最长时间或事件(异步回调),这是最先出现的

时间:2014-01-19 18:40:09

标签: java multithreading thread-synchronization

所以标题是解释性的,我想在一个线程上等待最长时间说1秒,如果另一个线程接收到响应那么它在1秒之内然后罚款,否则在1秒后是否响应是收到它应该停止等待并继续其工作,稍后我可以等待另外2秒,看看是否已经到达

主类:

atiReq.Now(); //asynchronous method returns immediatly
                firstATIState = atiReq.getState(1000); // Wait for a max of 1000 ms to get the response
            }
            handleIDP();
            if (isMobSub && firstATIState == null) {
                //If ati response was not recived previously wait for another maximum 2000 ms now
                firstATIState = atiReq.getState(2000);
            }

AtiRequest类:

 /**
     * Does an ATI Request asynchrounously
     */
    public void Now() {
        new Thread() {
            public void run() {
                atiReq.run();
                synchronized (atiReq) {
                    try {
                        atiReq.wait(3000);
                        rawResponse = atiReq.ReturnXML;
                        logger.info("[" + refID + "] ATI Response recieved: " + rawResponse);
                        atiResponseRecieved = true;
                        atiReq.notify();
                    } catch (InterruptedException ex) {
                        logger.error("Error waiting on atiReq", ex);
                    }
                }
            }
        }.start();
    }

    public String GetRawResponse() {
        return rawResponse;
    }

    /**
     * Gets the state of the ATI performed, waits for the ATI response for max
     * period of timeout specified, returns null if response not arrived in
     * specified time
     *
     * @param timeout the time to wait for response in milliseconds
     * @return
     */
    public ATIState getState(long timeout) {
        synchronized (atiReq) {
            while (!atiResponseRecieved) {
                try {
                    atiReq.wait(timeout);
                } catch (InterruptedException ex) {
                    logger.error("Error waiting on atiReq while trying to get aitState", ex);
                }
            }
        }
        if ("".equals(this.rawResponse)) {
            //Response not recieved yet
            logger.info("[" + refID + "] ATI Response not recived yet");
            return null;
        }

        ATIState atiState = new ATIState(this.rawResponse);
        return atiState;
    }

问题:

firstATIState = atiReq.getState(1000); 

在主类中,此行在1秒后不会终止,因为您可以在getState(long timeout)方法中看到相应的代码

 while (!atiResponseRecieved) {
                    try {
                        atiReq.wait(timeout);
                    } catch (InterruptedException ex) {
                        logger.error("Error waiting on atiReq while trying to get aitState", ex);
                    }
                }

处于一个循环中,所以我相信即使atiReq.wait(timeout)在1秒内返回并且如果atiResponseRecieved不为真,它仍然循环,直到Now()方法的超时耗尽并且它将atiResponseRecieved设置为true;

问题:

  1. 我该如何解决这个问题? 我已经尝试将它从循环中移除但是然后“虚假唤醒”不要让它等待完整的1秒。

  2. 还有其他解决方法吗?

1 个答案:

答案 0 :(得分:2)

您可以使用Futuretask(或仅使用Future)。为了这。它有一个get()方法,允许您指定超时。