Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Wait and Timeout

Reply
Thread Tools

Wait and Timeout

 
 
m
Guest
Posts: n/a
 
      04-24-2007
Hi All

I have a task, I need to execute this task only if I meet a condition.
I wrote the function

didIMeetCondition()


which returns boolean.

I have a threshold value of time to which I can wait till my condition
returns true.
After the threshold I need to throw exception if y condition return
false.

How do I write code to do this. Just psuedo code is sufficient.
Any ideas, please let me know.

Thanks
Bib

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      04-25-2007
On 24 Apr 2007 16:07:13 -0700, m wrote:
> I have a threshold value of time to which I can wait till my
> condition returns true. After the threshold I need to throw
> exception if y condition return false.
>
> How do I write code to do this. Just psuedo code is sufficient.



long now = System.currentTimeMillis();
long limit = now + timeoutMillis;

synchronized (obj) {
while ((!obj.didIMeetCondition()) &&
((now=System.currentTimeMillis()) < limit)) {
obj.wait(limit - now);
}
}

if (now >= limit) {
throw new MyException("condition not met");
}


The code that sets the condition should be synchronized on the same
"obj", and must call obj.notify() after making the change.

/gordon

--
 
Reply With Quote
 
 
 
 
Jeffrey Schwab
Guest
Posts: n/a
 
      04-25-2007
m wrote:
> Hi All
>
> I have a task, I need to execute this task only if I meet a condition.
> I wrote the function
>
> didIMeetCondition()
>
>
> which returns boolean.
>
> I have a threshold value of time to which I can wait till my condition
> returns true.
> After the threshold I need to throw exception if y condition return
> false.


You might want Future.get(long timeout, TimeUnit unit), or one of the
other classes that throw TimeoutException.

http://java.sun.com/j2se/1.5.0/docs/...Exception.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Work around for wait with timeout and count_waiters Sharat Mohan Ruby 0 02-11-2009 03:59 PM
idle timeout and max wait time in j2ee db connection pool Tiwkiz Java 0 01-27-2007 01:19 AM
Timeout::timeout and Socket timeout Mark Probert Ruby 1 10-06-2004 09:30 AM
Wait Delay TimeOut Sleep LL HTML 7 08-31-2004 01:22 AM
How to make "fork/wait" to WAIT longer? Huey C Programming 1 03-01-2004 02:01 PM



Advertisments