ret=result; result=null; hasResult=false; if(timeout_occurred) throw new TimeoutException(); else return ret;
finally { notifyAll(); }
    public Object getResultWithTimeout(long timeout) throws TimeoutException {        Object  ret=null;        long    time_to_wait=timeout, start;        boolean timeout_occurred=false;        synchronized(this) {            start=System.currentTimeMillis();            while(hasResult == false) {                if(timeout <= 0) {                    doWait();                }                else {                    if(time_to_wait <= 0) {                        timeout_occurred=true;                        break; // terminate the while loop                    }                    else {                        doWait(time_to_wait);                        time_to_wait-=System.currentTimeMillis()-start;                    }                }            }            ret=result;            result=null;            hasResult=false;            if(timeout_occurred)                throw new TimeoutException();            else                return ret;        }    }